首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Django导入-一对一字段的导出导入错误- KeyError:'id‘

Django导入-一对一字段的导出导入错误- KeyError:'id‘
EN

Stack Overflow用户
提问于 2022-07-29 16:50:04
回答 1查看 179关注 0票数 2

使用: Python 3.10.4

Django 4.06

Django-进口-出口2.8.0

我正在尝试将数据作为演示数据导入到django应用程序中。我不断得到一个KeyError。

代码语言:javascript
运行
复制
### models.py
class Reservation(models.Model):
    reservation = models.OneToOneField(Vehicle, on_delete=models.CASCADE, primary_key=True,)
    delivered = models.BooleanField('Delivered',default=False)
    date_reserved = models.DateTimeField('date reserved', default=datetime.datetime.now)
    ...

### admin.py
class ReservationResource(resources.ModelResource):
    class Meta:
        model = Reservation
        exclude = ('id',)
        import_id_fields = ('reservation',)
        fields = (
            'reservation',
            'delivered',
            'date_reserved',
            ...
        )
class ReservationImportExport(ImportExportModelAdmin):
    resource_class: ReservationResource
@admin.register(Reservation)
class ReservationAdmin(SimpleHistoryAdmin, ReservationImportExport):
    fields = ["delivered","date_reserved",...]

### demo-reservations.yaml  (Note: Problem happens using different data file formats)
 - reservation: 50001
   delivered: False
   date_reserved: 7/15/2022T00:00:00+00:00
   ...

这是一个错误(有点模糊)

代码语言:javascript
运行
复制
Traceback (most recent call last):
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 661, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 353, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 340, in get_instance
import_id_fields = [
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 341, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'

已试过:

在调试器中,

  1. 从Admin
  2. 断点中删除了SimpleHistoryAdmin --很明显,它忽略了"import_id_fields“值。如果我在调用get_import_id_fields(self)时手动将值更改为“订票”,我将得到进一步的信息(第二个问题我将单独问--猜测堆栈溢出需要每个问题一个问题。有人可以随时告诉我,如果把它们放在一起效果更好。)我在调试器中确实看到,当self._meta.import_id_fields到达该调用时,“id”是它的值。

我找到的所有其他答案似乎都表明,添加不包括id和import_id_fields的内容应该可以解决这个问题。唯一认为我已经尝试了而不是(而实际上并不想要)的是更改id/foreign key列名。

编辑1:在这一点上,我几乎可以肯定这是django导入导出包中的一个bug。如果模型中有名为"id“的字段,则admin导入将中断。我有点过意不去了,要彻底解决问题。我在GitHub上输入了一个bug。https://github.com/django-import-export/django-import-export/issues/1480

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-04 15:47:17

如果找到了,只想关闭循环。我的代码出错了。

代码语言:javascript
运行
复制
class ReservationImportExport(ImportExportModelAdmin):
    resource_class: ReservationResource

应该是:

代码语言:javascript
运行
复制
class ReservationImportExport(ImportExportModelAdmin):
    resource_class = ReservationResource

=而不是:带来了所有的不同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73168623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档