Foreign key widget finds more than 1 value, how should i approach this

我只是一个虾纸丫 提交于 2019-12-08 18:38:27

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!