Also, we need to ensure that User relations are looked for using the "correct" field. From the docs:查找字段默认使用主键(pk)作为查找条件,但可以自定义以使用相关模型上的任何字段. The lookup field defaults to using the primary key (pk) as lookup criterion but can be customised to use any field on the related model.将所有内容放在一起,我们的 Field 定义如下:Putting it all together, our Field definition looks like this:userid = fields.Field(column_name='username', attribute='user', widget=widgets.ForeignKeyWidget(User, "username")因此,我认为您的错误原因是您未正确地将属性设置为 username .如果调试了代码,则可能会发现已加载了'testuser' User 实例,并将其分配给 Word.username ,而该实例将被忽略,而 Word.user 将为null,因此出现错误.So I think the source of your error was that you were incorrectly setting attribute to username. If you debugged the code, you would probably find that the 'testuser' User instance had been loaded, and assigned to Word.username, which will just be ignored, and Word.user will be null, hence the error. 更新另一个要纠正的问题: fields 声明应引用要从csv数据中设置的模型属性.The fields declaration should reference the model attributes which are to be set from csv data.因此, username 字段应为 user ,因为这是要更新的模型属性.Therefore the username field should user, because this is the model attribute to be updated.fields = ("user", "target_word",'source_word','example_sentence', 'fluency', 'deck_name',) 这篇关于Django import-export-让用户导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!