这已经被问过好几次了——但没有一个解决方案对我有用。
下面的代码有效(因为没有错误),但它没有看到任何将新数据导入外键类的内容。如果数据已经存在于外键中,它只会导入数据。
那有意义吗?
模型.py(片段)
...
class Store(models.Model):
store_name = models.CharField(max_length=30)
def __unicode__(self):
return self.store_name
#etc
class Product(models.Model):
Store = models.ForeignKey(Store)
Category = models.ForeignKey(Category)
first_name = models.CharField(max_length=30)
second_name = models.CharField(max_length=30)
...
管理员.py
admin.site.register(Category)
admin.site.register(Store)
class ProductResource(resources.ModelResource):
store_name = fields.Field(column_name='store_name', attribute='Store',
widget=ForeignKeyWidget(Store, 'store_name'))
def __unicode__(self):
return self.store_name.name
class Meta:
model = Product
fields = ('id', 'first_name', 'second_name','store_name')
export_order = ('id', 'second_name', 'first_name')
skip_unchanged = False
report_skipped = False
widgets = {
'published': {'format': '%d.%m.%Y'},
}
class ProductAdmin(ImportExportModelAdmin):
resource_class = ProductResource
list_display = ('first_name', 'second_name')
admin.site.register(Product, ProductAdmin)