2

I have created two models in Django. The one is Test_DB and the other is ForeignKey_Test. The later models has a foreign key field which is referring to the Test_DB. When I tried to enter data from the the Django's default admin panel, I got the following error.

enter image description here

The above exception ( Keyword: FAILED SQL: SELECT %(0)s AS "a" FROM "testapp_test_db" WHERE "testapp_test_db"."id" = %(1)s LIMIT 1 Params: (1, 1) Version: 1.3.6 Sub SQL: None FAILED SQL: None Params: None Version: None) was the direct cause of the following exception: 

What I am concluding that while parsing the SQL, Djongo expects two parameters to be passed. But currently it is being only one passed and that is the id. Can somebody tell me what's wrong here?

I can share any of the snippet of my project here.

Models

class Test_DB(models.Model):
name = models.CharField(max_length=50)


class ForeignKey_Test(models.Model):
    name = models.CharField(max_length=50)
    test_id = models.ForeignKey(to='Test_DB', on_delete=models.CASCADE)

My database is in MongoDb and I am using Djongo.

pip freeze

asgiref==3.5.2
backports.zoneinfo==0.2.1
certifi==2022.9.24
charset-normalizer==2.1.1
coreapi==2.3.3
coreschema==0.0.4
Django==4.1.1
django-cors-headers==3.13.0
djangorestframework==3.14.0
djangorestframework-simplejwt==5.2.0
djongo==1.3.6
drf-yasg==1.21.3
idna==3.4
inflection==0.5.1
itypes==1.2.0
Jinja2==3.1.2
MarkupSafe==2.1.1
packaging==21.3
PyJWT==2.5.0
pymongo==3.12.3
pyparsing==3.0.9
pytz==2022.2.1
PyYAML==6.0
requests==2.28.1
ruamel.yaml==0.17.21
ruamel.yaml.clib==0.2.6
sqlparse==0.2.4
uritemplate==4.1.1
urllib3==1.26.12 
  • I didn't get you but you can do that without custom SQL, mean use python – shah sawood Sep 27 '22 at 10:23
  • I am not using custom SQL. I am using Django's admin panel to insert data. But while inserting it's giving me this error. I only assumed that the SQL that Django's admin panel is parsing is creating an error because it is not getting the field name in the params list. – Shashank Shekhar Sep 27 '22 at 11:33

1 Answers1

-1

its not test_id = models.ForeignKey(to='Test_DB', on_delete=models.CASCADE) its test_id = models.ForeignKey(Test_DB, on_delete=models.CASCADE)

hope you get it :)

Reuben L.
  • 2,806
  • 2
  • 29
  • 45