Django / Sqlite3 add a row for model with a foreign key Django / Sqlite3 add a row for model with a foreign key shell shell

Django / Sqlite3 add a row for model with a foreign key


Person.objects.create(name='Adam', person_type='Appellant') 

Here, as person_type argument, create() method expects to get person_type instance not string

So just prowide:

pers_type = Person_Type.objects.get(pers_type='Appelant') # assuming pers_type is uniquePerson.objects.create(name='Adam', pers_type=pers_type) 

or, taking into account case when 'Appellant' not present in db:

try:    pers_type = Person_Type.objects.get(pers_type='Appelant')except Person_Type.DoesNotExists:    person_type = Person_Type.objects.create(pers_type='Appellant')Person.objects.create(name='Adam', pers_type=pers_type)