Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm new to both Django and SQlite3. I have a model (Person), with a foreign key to (Person_Type):

class Person(models.Model):
    name = models.CharField(max_length=500)
    pers_type = models.ForeignKey(Person_Type)

    def __unicode__(self):
        return self.name

class Person_Type(models.Model):
    pers_type = models.CharField(max_length=40)

    def __unicode__(self):
        return self.pers_type

I am trying to add entries to Person using the python manage.py shell.

So far, I have tried:

import sqlite3
from trials.models import *

conn = sqlite3.connect('privy.db')
print Person #this returns <class 'privy.trials.models.Person'>
cur = conn.cursor()
fields = ['name', 'pers_type']
row = ['Adam', 'Appellant']
Person.objects.create(**dict(zip(fields, row)))

But this returns an error: ValueError: Cannot assign "'Appellant'": "Person.pers_type" must be a "Person_Type" instance.

The string "Appellant" is already being stored as one of the values in the "Person_Type.pers_type" table. What do I need to change to get this to reference the pers_type field?

Happy to provide more details if needed. Thanks very much for your time.

like image 512
Adam Avatar asked Sep 06 '11 14:09

Adam


People also ask

Does sqlite3 support foreign keys?

SQLite has supported foreign key constraint since version 3.6. 19. The SQLite library must also be compiled with neither SQLITE_OMIT_FOREIGN_KEY nor SQLITE_OMIT_TRIGGER. To check whether your current version of SQLite supports foreign key constraints or not, you use the following command.

How do I add a foreign key to an existing table in SQLite?

How to Add a Foreign Key to an Existing Table. You can not use the ALTER TABLE statement to add a foreign key in SQLite. Instead you will need to rename the table, create a new table with the foreign key, and then copy the data into the new table.

How do foreign keys work in Django?

Introduction to Django Foreign Key. A foreign key is a process through which the fields of one table can be used in another table flexibly. So, two different tables can be easily linked by means of the foreign key. This linking of the two tables can be easily achieved by means of foreign key processes.

Can a model have two foreign keys Django?

Your intermediate model must contain one - and only one - foreign key to the source model (this would be Group in our example). If you have more than one foreign key, a validation error will be raised.


1 Answers

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 unique
Person.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)
like image 137
Pill Avatar answered Sep 25 '22 15:09

Pill