0

尝试将 csv 数据导入到我的 django 应用程序模型中,我遇到了麻烦......我尝试使用脚本并通过 django-import-export。我将只详细说明后面的内容,以避免在整个空间中运行。

我的绘画模型有几个外键,我无法映射到我的模型,因为我还将它们导入 CSV 文件,即它们尚未创建。我怎样才能克服它?这就是我如何将绘画实例映射到外键名称而不是尚未创建的 ID?

为了测试,我手动为 eht 外键创建了一些条目,并将它们的 ID 整数添加到我的 CSV 中。我注意到我只能导入第一个外国 hey 值。其他不出现,例如在预览和最终导入中显示画家ID,但类别不显示。这是我的第二个问题:如何从同一个 CSV 文件中导入多个外键?

这是我的代码:

class Painter(models.Model):
    """Model representing a painter."""
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    date_of_birth = models.DateField(null=True, blank=True)
    date_of_death = models.DateField('died', null=True, blank=True)

    def __str__(self):
        """String for representing the Model object."""
        return f'{self.last_name}, {self.first_name}'

class Painting(models.Model):
    '''Model representing a painting'''
    title = models.CharField(max_length=200)
    painter = models.ForeignKey('Painter', on_delete=models.CASCADE, default=1)
    medium = models.ForeignKey(Medium, on_delete=models.SET_NULL, blank=True, null=True)
    support = models.ForeignKey(Support, on_delete=models.SET_NULL, blank=True, null=True)
    category = models.ForeignKey(Category, related_name='categories', on_delete=models.SET_NULL, blank=True, null=True)
....

lass Category(models.Model):
    name = models.CharField(
        max_length=200,
        help_text="Enter the painting's category (e.g. Sketch, Study, Drawing, Painting, etc.)"
    )

    def __str__(self):
...

Admin.py

from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from import_export import resources, fields
from import_export.widgets import ManyToManyWidget, ForeignKeyWidget
from .models import Painter, Painting, Medium, Support, Category, Location, Tag
from django_admin_inline_paginator.admin import TabularInlinePaginated



admin.site.register(Medium)
admin.site.register(Support)
admin.site.register(Category)
admin.site.register(Location)
admin.site.register(Tag)


class PaintingsAdminInline(TabularInlinePaginated):
    # fields = (...)
    per_page = 3
    model = Painting


class PainterResource(resources.ModelResource):
    class Meta:
        model = Painter


class PainterAdmin(ImportExportModelAdmin):
    resources_class = PainterResource
    list_display = ('last_name', 'first_name', 'date_of_birth', 'date_of_death', 'image')
    fields = [('first_name', 'last_name'), ('date_of_birth', 'date_of_death'), 'biography', 'image']
    inlines = (PaintingsAdminInline,)

admin.site.register(Painter, PainterAdmin)
class PaintingResource(resources.ModelResource):
    class Meta:
        model = Painting


class PaintingAdmin(ImportExportModelAdmin):
    resources_class = PaintingResource
    list_display = ('id', 'title', 'painter', 'category', 'medium', 'display_tag', 'motif',)
    list_filter = ('title', 'painter', 'category', 'medium', 'motif')
....

admin.site.register(Painting, PaintingAdmin)

示例数据:id,title,Description,motif,image,date,largeur,hauteur,signature,remark,reference,category_id,location_id,medium_id,first_name,support_id,,a,0 100,Tence jouors,Tence, paysage, ,,,44.0,26.0,oui,,M26,1,Tense,huile ,Marius,bois,,,

任何输入,任何了解更多信息的机会都将受到高度赞赏。

4

0 回答 0