我不明白django-import-export模块如何处理ForeignKeys。 这是一个简单的例子: models.py
class TFamilies(models.Model):
id_fam = models.AutoField(primary_key=True, unique=True)
name_fam = models.CharField(max_length=1024, blank=True,verbose_name='Famille')
class TGenus(models.Model):
id_genus = models.AutoField(primary_key=True, unique=True)
name_genus = models.CharField(max_length=1024,verbose_name='nom de genre')
id_fam = models.ForeignKey(TFamilies, null=True, db_column='id_fam', blank=True, verbose_name='Famille')
我想允许人们添加与家庭相关的属!只有name_genus和name_fam的CSV / XLS ...(并且id留空)。
大多数时候家庭已经存在于DB中,Django juste必须找到正确的id号...
admin.py
class TGenusResource(resources.ModelResource):
name_fam = fields.Field(widget=widgets.ForeignKeyWidget(TFamilies, 'name_fam'))
class Meta:
model = TGenus
import_id_fields = ['id_genus']
class TGenusAdmin(ImportExportActionModelAdmin):
form = TGenusAdminForm
resource_class = TGenusResource
pass
此配置导致导入界面出错:
Line number: 1 - 'NoneType' object has no attribute 'name_fam'
Traceback (most recent call last):
File "/....../lib/python2.7/site-packages/import_export/resources.py", line 348, in import_data
row_result.object_repr = force_text(instance)
File "......./lib/python2.7/site-packages/django/utils/encoding.py", line 85, in force_text
s = six.text_type(s)
AttributeError: 'NoneType' object has no attribute 'name_fam'
我不明白...... 我也尝试过回答:django-import-export resource definition for foreignkey field? 有点像:Foreign Key in django migration using django-import-export
我是否必须使用before_import来找到自己的比赛?
答案 0 :(得分:1)
我发现你必须找到自己的比赛!在Tablib数据集中更改值是不可能的,因此您必须记录条目进行更改并将它们放回到新行中然后删除旧行。
我的Excel模板包含由家庭名称填充的列id_genus
(空),name_genus
和id_fam
!
对于任何降落在这里的人,我都会发布:
def before_import(self, dataset, dry_run):
"""
Make standard corrections to the dataset before displaying to user
"""
i = 0
last = dataset.height - 1
# for all lines, search for id of family given and add a new line at the bottom with it then delete the first one
while i <= last:
# Check if the Genus exist in DB
if (TGenus.objects.filter(name_genus=dataset.get_col(2)[0].capitalize())):
id_genus = TGenus.objects.filter(name_genus=dataset.get_col(2)[0].capitalize())[0].id_genus
else :
id_genus = ''
# Check if the family exists in DB
try:
TFamilies.objects.get(name_fam=dataset.get_col(2)[0].capitalize())
except TFamilies.DoesNotExist:
raise Exception("Family not in DB !")
except TFamilies.MultipleObjectsReturned:
pass
# use of "filter" instead of "get" to prevent duplicate values, select the first one in all cases
dataset.rpush((id_genus,
dataset.get_col(1)[0],
TFamilies.objects.filter(name_fam=dataset.get_col(2)[0].capitalize())[0].id_fam))
dataset.lpop()
i = i + 1
我的django管理员可以被非sys-admin使用,因此,他们可以复制不属于DB的属或家庭... 如果有人想要更好地处理错误,我想阅读它! 此外,我想在预览中保留家庭的名字,不仅仅是她的身份......如果您知道如何,我已经发布了另一个问题:Is-it possible to customize the template of preview in django import-export?
答案 1 :(得分:0)
我认为您需要根据模型 TGenus 中的字段名称重命名:
id_fam = fields.Field(widget=widgets.ForeignKeyWidget(TFamilies, 'name_fam'))
instead of:
name_fam = fields.Field(widget=widgets.ForeignKeyWidget(TFamilies, 'name_fam'))