开发者

django import with csvimport.py fails when trying to create a reference to a foreign key

开发者 https://www.devze.com 2023-01-23 05:51 出处:网络
current django project is a mini shop thing. im trying to create the concept of having products which could have a foreign key of 开发者_StackOverflow社区a Brand and a Category. however the import im

current django project is a mini shop thing.

im trying to create the concept of having products which could have a foreign key of 开发者_StackOverflow社区a Brand and a Category. however the import im using is this one http://djangosnippets.org/snippets/788/

and it fails when trying to create the object on the foreign key field. it puts the error message ValueError: Cannot assign "'Brand.dubkorps'": "Product.brand1" must be a "Brand" instance.

I get that im not putting the right value in the csv file for the foreign key i'm trying to reference, my question is, how do i work out what the value is for this particular foreign key?

love to hear from anyone who has done something similar with csvimport.


if in the import file you have the ID of an existing brand you have two choiches:

brand = Brand.objects.get(pk= csv_val)
product.brand = brand

or

Product.brand_id = csv_val

hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消