python - django - can't assign a foreign key -


for unknown reasons, cannot assign foreign key instance of item_rarity table detailed_item table. django throws error:

cannot assign "u'basic'": "detailed_item.rarity" must "item_rarity" instance.

... in item_rarity dictionary "basic" record exists - can choose admin panel , create detailed_item record manually.

i have defined models:

class detailed_item(models.model):     item_id = models.integerfield(primary_key=true)     name = models.charfield(max_length=50)     level = models.integerfield()     icon = models.charfield(max_length=150)     rarity = models.foreignkey('item_rarity')     general_type = models.foreignkey('item_type')     detailed_type = models.foreignkey('item_detailed_type')  class item_rarity(models.model):     name = models.charfield(max_length=15, primary_key=true)  class item_type(models.model):     name = models.charfield(max_length=15, primary_key=true)  class item_detailed_type(models.model):     name = models.charfield(max_length=20, primary_key=true) 

in views, try populate in manner (inserting multiple items):

...    items = get_all_items()  #get dict of items         element in items:             tmp_det_type = ''             key, val in element.iteritems():             #get 'detailed type' inner dict                 if key == "type":                     tmp_det_type = val              item = detailed_item(                 item_id=element['id'],                 name=element['name'],                 level=element['level'],                 icon=element['icon'],                 rarity=element['rarity'],  #error                 general_type=element['type'],                 detailed_type=tmp_det_type,                 )              item.save()  ... 

i tried hard code "basic" string, doesn't work either.

* solved * next 2 entries, item_type , item_detailed_type invalid. correct code:

from app.models import detailed_item, item_rarity, item_type, item_detailed_type ...    items = get_all_items()  #get dict of items         element in items:             tmp_det_type = ''             key, val in element.iteritems():             #get 'detailed type' inner dict                 if key == "type":                     tmp_det_type = val                  #create objects string values                 obj_rarity = item_rarity(name=element['rarity'])                 obj_item_type = item_type(name=element['type'])                 obj_item_detailed_type = item_detailed_type(name=tmp_det_type)                  item = detailed_item(                     item_id=element['id'],                     name=element['name'],                     level=element['level'],                     icon=element['icon'],                     rarity=obj_rarity,                     general_type=obj_item_type,                     detailed_type=obj_item_detailed_type,                     )               item.save()  ... 

item_rarity instance should passed while storing detailed_item object since item_rarity foreign key related object in detailed_item.

its might have passed basic string instead of <basic object> itself.

while creating object in django using orm, foreign_key related object should provided instance instead of id(pk) of object, while fetching data database can use either of instance or id(pk) of instance.

class parentmodel(models.model):     model_field = models.charfield(max_length=16)  class mymodel(models.model):     some_field = models.foreignkey('parentmodel')  parent_model = parentmodel.objects.create(model_field='some_data')  my_model = mymodel.objects.create(some_field=parent_model)                                              ^^^^^^^^^^^^ 

note here parent_model object passed instead of id

while fetching data back,

parent_model = parentmodel.objects.get(model_field='some_data')  my_model = mymodel.objects.get(some_field=parent_model)  or  my_model = mymodel.objects.get(some_field=parent_model.id) 

both work in case of data fetch.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -