django foreign key Cannot assign must be a instanc

2019-07-26 21:57发布

问题:

I am developing a web app using Django. I have created the table in MySQL database and then generated the models.py using inspectdb. I am able to fetch details and connect to the database without any issues. But while saving the values to the particular table, below error is shown

sav_list = List(id=4, item_name ='name1', item_desc='desc1', location='location', reason='rfp',  pid=3)

Cannot assign "3": "List.id" must be a "Order" instance.

my models

class List(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    item_name = models.CharField(db_column='Item_Name', max_length=255)  # Field name made lowercase.
    item_desc = models.CharField(db_column='Item_Desc', max_length=300)  # Field name made lowercase.
    location = models.CharField(db_column='Location', max_length=100, blank=True, null=True)  # Field name made lowercase.
    reason = models.CharField(db_column='Reason', max_length=100, blank=True, null=True)  # Field name made lowercase.
    pid = models.ForeignKey('Order', models.DO_NOTHING, db_column='PID')  # Field name made lowercase.

class Order(models.Model):
    poid = models.IntegerField(db_column='POID', primary_key=True)  # Field name made lowercase.
    po = models.CharField(db_column='PO', unique=True, max_length=20)  # Field name made lowercase.
    quote = models.CharField(db_column='Quote', unique=True, max_length=20)  # Field name made lowercase.
    condition = models.CharField(db_column='Condition', max_length=15)  # Field name made lowercase.

I have tried relate_name for foreign key but still same behaviour. Same values can be stored on database without any issues. Only Django throws an error.

Please someone help me!!!

回答1:

You should paste the actual error; it is presumably that "List.pid" must be an "Order" instance.

The error should be clear. pid is a ForeignKey field, it expects an instance of the related model. You can either get the item and set it:

pid = Order.objects.get(pk=3)
List(...., pid=pid)

or use the underlying id field:

List(...., pid_id=3)


回答2:

The pid column at the Django level is not an id (integer), yes on the database level it is. But in Django it is a foreign key, and thus it refers to an Order instance. You thus should pass it an Order instance (the one that corresponds to poid=3).

But we are lucky, in case you construct such ForeignKey, Django automatically makes a field_id column that stores the id, and those two fields act like twins. We can thus assign 3 to pid_id:

sav_list = List(
    id=4,
    item_name ='name1',
    item_desc='desc1',
    location='location', reason='rfp',
    pid_id=3
)

Note however that for most database backend, foreign key constraints will check if there is an Order instance with as referenced column (in Django the primary key) 3 exists. If not, it is impossible to create such List. I would therefore advice to ensure that such Order exists before trying to save it to the database.