[Django]-Django model object with foreign key creation

139👍

What you’re after is:

TestModel.objects.create(testkey_id=1)
👤sneeu

19👍

In my case TestModel.objects.create(testkey__id=1) didn’t work for me so I had to put one underscore instead of two, for example

TestModel.objects.create(testkey_id=1)

13👍

In get_or_create it will fail in get. So to make get_or_create work below is the solution:

TestModel.objects.get_or_create(testkey=TestModel2(id=1))

Reference:
https://code.djangoproject.com/ticket/13915

Leave a comment