> Also when creating my models I use "id = models.AutoField(primary_key=True)" 
> for my ID field.  This way Django will Auto generate an ID for each record.
This is unnecessary.  Django does this automatically.
https://docs.djangoproject.com/en/2.0/topics/db/models/#automatic-primary-key-fields


From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Gerald Brown
Sent: Wednesday, July 18, 2018 4:51 AM
To: django-users@googlegroups.com
Subject: Re: Django foreign-key cannot assign must be a instance


Also when creating my models I use "id = models.AutoField(primary_key=True)" 
for my ID field.  This way Django will Auto generate an ID for each record.

On Tuesday, 17 July, 2018 08:03 PM, Sainath Bavugi wrote:
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


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


command used

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

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!!!
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3dce2148-ee12-41c5-996f-f21c7b33b4a5%40googlegroups.com<https://groups.google.com/d/msgid/django-users/3dce2148-ee12-41c5-996f-f21c7b33b4a5%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
django-users+unsubscr...@googlegroups.com<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to 
django-users@googlegroups.com<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8e431e64-34d5-6c63-012e-242ed49bef4e%40gmail.com<https://groups.google.com/d/msgid/django-users/8e431e64-34d5-6c63-012e-242ed49bef4e%40gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8799b338bd72424fbd9608814cbd171d%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to