外键小部件找到多个值,我该如何处理 [英] Foreign key widget finds more than 1 value, how should i approach this
问题描述
我正在使用django-import-export库,并且正在尝试实现ForeignKey小部件,该小部件可用于使用Author.name而不是Author.pk查找相关对象.现在,这是给定日历年的棘手部分,我只有一位名字相同的作者,但是明年的作者姓名将相似.当然,当我尝试导入时,它带来的问题是发现的不仅仅是Author.name.
I am using django-import-export library and I am trying to implement ForeignKey widget which is available to lookup related objects using Author.name instead of Author.pk. Now, the here is the tricky part for the given calendar year I only have one author with the same name, however, next year the author name will be similar. When i try to import, of course, it brings the issue saying that more than Author.name was found.
是否有解决此问题的建议?
Is there a suggestion to solve the issue?
推荐答案
I've used before_save_instance()
to do something similar to this. Here's some pseudo-code for how this might work:
class MyModelResource(ModelResource):
# Specify fields and Meta information here
def before_save_instance(self, instance, using_transactions, dry_run):
# Replace the below with your actual code
year = instance.year
author = Author.objects.filter(year=year)
instance.author = author
return instance
这假设您要获取正确作者的信息在您要导入的行中可用.
This assumes that the information you need to get the correct author is available in the row you're importing.
这篇关于外键小部件找到多个值,我该如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!