How to fix must be an instance
when a foreign key is
referred django python
mysql
Haris NP
haris@baabtra.com
www.facebook....
Inserting data in Django to tables with foreign key
reference.
>>> from baabtramodel.models import Item
>>> obj_item = Ite...
• Error
• Traceback (most recent call last):
– File “<console>”, line 1, in <module>
– File “C:Python27…. base.py”, line 4...
Fix
• Create an instance of the Category.
>>> from baabtramodel.models import Category
>>> category_id = Category.objects....
• From a sample application
views.py
• Please note that we are passing categoryid
directly here. It must be changed to an
instance.
Updated views.py
• Please note that Id is an instance of the class
Category
• The author takes corporate trainings in
Android, Java, JQuery, JavaScript and Python. In case
if your organization needs...
How to fix must be an instance when a foreign key is referred django python mysql
How to fix must be an instance when a foreign key is referred django python mysql
How to fix must be an instance when a foreign key is referred django python mysql
Upcoming SlideShare
Loading in …5
×

How to fix must be an instance when a foreign key is referred django python mysql

4,245 views

Published on

This explains how to fix the error(must be a category instance) when you try to submit a form with foreign key reference in it. This is specific to Django framework which uses Python. Back end is mysql.

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

How to fix must be an instance when a foreign key is referred django python mysql

  1. 1. How to fix must be an instance when a foreign key is referred django python mysql Haris NP haris@baabtra.com www.facebook.com/haris.np 9 twitter.com/np_haris in.linkedin.com/in/harisnp
  2. 2. Inserting data in Django to tables with foreign key reference. >>> from baabtramodel.models import Item >>> obj_item = Item( item_name= 'pen', item_code='p', item_barcode ='bc001', item_description ='Used for writing', item_cost = 10, item_retail_price =15 , item_category_id =1)
  3. 3. • Error • Traceback (most recent call last): – File “<console>”, line 1, in <module> – File “C:Python27…. base.py”, line 403, in __init__ • setattr(self, field.name, rel_obj) – File “C:Python27….related.py”, line 405, in __set__ • Self.field_name, self.field.rel.to._meta.object_name) – ValueError: Cannot assign “1”: “Item.item_category_id” must be a “Category” instance.
  4. 4. Fix • Create an instance of the Category. >>> from baabtramodel.models import Category >>> category_id = Category.objects.get(id=1) >>> from baabtramodel.models import Item >>> obj_item = Item( item_name= 'pen', item_code='p', item_barcode ='bc001', item_description ='Used for writing', item_cost = 10, item_retail_price =15 , item_category_id =category_id ) Please note that category_id is an instance and not the variable inside the object.
  5. 5. • From a sample application
  6. 6. views.py • Please note that we are passing categoryid directly here. It must be changed to an instance.
  7. 7. Updated views.py • Please note that Id is an instance of the class Category
  8. 8. • The author takes corporate trainings in Android, Java, JQuery, JavaScript and Python. In case if your organization needs training, please connect through www.massbaab.com/baabtra. • Baabtra provides both online and offline trainings for candidates who want to learn latest programming technologies , frameworks like codeigniter, django, android, iphone app development • Baabtra also provides basic training in SQL, .NET, PHP, Java and Python who wants to start their career in IT.

×