0

私は Django の初心者で、Django import-exportを実装しようとして立ち往生しています。私の 3 つの適切なモデルは、オフィサー、インシデント、および詳細です。オフィサーとインシデントは、詳細を通じて M2M 関係にあります。詳細をインポートする以外に、すべての機能が動作するようになりました。インポート ボタンを使用してインポートしようとすると、インポートしている .xls または .csv ファイルの各行に「NOT NULL 制約が失敗しました: police_archive_details.incident_id」が表示されます。

ここに私の(現在の)admin.pyがあります

from django.contrib import admin
from import_export import resources, widgets, fields
from import_export.admin import ImportExportModelAdmin, ImportExportActionModelAdmin
from forms import AdminTextForm, OfficerTextForm

from .models import Officer, Incident, Details, SiteText

class FullNameForeignKeyWidget(widgets.ForeignKeyWidget):
    def get_queryset(self, value, row):
        return self.model.objects.filter(
            first_name__iexact=row["first_name"],
            last_name__iexact=row["last_name"]
        )

class DetailsInlineAdmin (admin.TabularInline):
    model = Details
    extra = 5




class OfficerResource(resources.ModelResource):

    class Meta:
        model = Officer

class OfficerAdmin(ImportExportModelAdmin):
    list_display = ('first_name', 'last_name', 'badge', 'department')
    search_fields = ['first_name', 'last_name']
    inlines = [DetailsInlineAdmin]
    resource_class = OfficerResource

    form=OfficerTextForm

    class Media:
        js = ('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js','/static/admin/js/admin/popup.js')



class IncidentResource(resources.ModelResource):

    officer = fields.Field(
        column_name='officer',
        attribute='officer',
        widget=widgets.ForeignKeyWidget(Officer, 'badge'))

    class Meta:
        fields = ('officer',)
        model = Incident   

class IncidentAdmin(ImportExportModelAdmin):
    list_display = ('office','case_number')
    search_fields = ['case_number']
    inlines = [DetailsInlineAdmin]
    resource_class = IncidentResource




class DetailsResource(resources.ModelResource):

    officer = fields.Field(
        column_name='officer',
        attribute='officer',
        widget=FullNameForeignKeyWidget(Officer))

    incident = fields.Field(
        column_name='incident',
        attribute='incident',
        widget=widgets.ForeignKeyWidget(Incident, 'case_number'))

    class Meta:
        fields = ('id','incident','officer__last_name','officer__first_name','allegation', 'finding', 'action')
        model = Details


class DetailsAdmin(ImportExportModelAdmin):
    list_display=('incident','officer', 'allegation', 'finding', 'action')
    search_fields = ['officer__last_name', 'incident__case_number']
    resource_class = DetailsResource

class SiteTextAdmin(admin.ModelAdmin):
    form=AdminTextForm





admin.site.register(Officer, OfficerAdmin)
admin.site.register(Incident, IncidentAdmin)
admin.site.register(Details, DetailsAdmin)
admin.site.register(SiteText, SiteTextAdmin)

そしてここにmodels.pyがあります

from __future__ import unicode_literals

from django.db import models
from tinymce import models as tinymce_models

class Officer(models.Model):
    first_name = models.CharField(max_length=80, blank=True, null=True)
    last_name = models.CharField(max_length=80, blank=True, null=True)
    badge = models.IntegerField(blank=True, null=True)
    department = models.CharField(max_length=50, blank=True, null=True)
    model_pic = models.ImageField(upload_to = "police_archive/officer_photos", default= 'noimage', blank=True, null=True)
    description = tinymce_models.HTMLField(blank=True, null=True)

    def __str__(self):
        return self.last_name + ', ' + self.first_name

    class Meta():
        ordering = ['last_name']    





class Incident(models.Model):
    officer = models.ManyToManyField(Officer, through='Details')
    case_number = models.CharField(max_length=50, blank=True)

    OFFICE_CHOICES = (
    ('CRA', 'Civilian Review Authority'),
    ('IA', 'Internal Affairs'),
    ('OPCR', 'Office of Police Conduct Review'),
    )
    office = models.CharField(max_length=10,
    choices=OFFICE_CHOICES,
    )

    def __str__(self):
        return self.case_number

    class Meta():
        ordering = ['-case_number']

class Details(models.Model):
    officer = models.ForeignKey(Officer, on_delete=models.CASCADE, blank=True)
    incident = models.ForeignKey(Incident, on_delete=models.CASCADE, blank=True)
    allegation = models.CharField(max_length=50, blank=True)
    finding = models.CharField(max_length=50, blank=True)
    action = models.CharField(max_length=50, blank=True)


    def __str__(self):
            return self.officer.first_name + ' '+ self.officer.last_name+ ', ' + self.incident.case_number

    class Meta():
        verbose_name_plural = "details"
        ordering = ['incident__case_number']


class SiteText(models.Model):
    content1 = tinymce_models.HTMLField()
    content2 = models.TextField(max_length=500, blank=True)
4

1 に答える 1

2

OK、昨日は一日中デバッグに費やし、すべてが適切に機能するようになりました。これが私の admin.py です。models.py は変更されていません。私が投稿した元のエラーを解決した変更を正確に忘れてしまいました。

from django.contrib import admin
from import_export import resources, widgets, fields
from import_export.admin import ImportExportModelAdmin, ImportExportActionModelAdmin
from forms import AdminTextForm, OfficerTextForm

from .models import Officer, Incident, Details, SiteText

class DetailsInlineAdmin (admin.TabularInline):
    model = Details
    extra = 5




class OfficerResource(resources.ModelResource):

    class Meta:
        model = Officer

class OfficerAdmin(ImportExportModelAdmin):
    list_display = ('first_name', 'last_name', 'badge', 'department')
    search_fields = ['first_name', 'last_name']
    inlines = [DetailsInlineAdmin]
    resource_class = OfficerResource

    form=OfficerTextForm

    class Media:
        js = ('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js','/static/admin/js/admin/popup.js')



class IncidentResource(resources.ModelResource):


    class Meta:
        fields = ('officer','case_number', 'office')
        model = Incident
        import_id_fields = ['case_number']

class IncidentAdmin(ImportExportModelAdmin):
    list_display = ('office','case_number')
    search_fields = ['case_number']
    inlines = [DetailsInlineAdmin]
    resource_class = IncidentResource




class DetailsResource(resources.ModelResource):

    class BadgeForeignKeyWidget(widgets.ForeignKeyWidget):
        def get_queryset(self, value, row):
            return self.model.objects.filter(
                badge__iexact=row["badge"]
            )

    officer = fields.Field(
        column_name='officer',
        attribute='officer',
        widget=BadgeForeignKeyWidget(Officer, 'last_name'))

    incident = fields.Field(
        column_name='incident',
        attribute='incident',
        widget=widgets.ForeignKeyWidget(Incident, 'case_number'))

    class Meta:
        fields = ('id','officer','incident', 'allegation', 'finding', 'action')
        model = Details


class DetailsAdmin(ImportExportModelAdmin):
    list_display=('incident','officer', 'allegation', 'finding', 'action')
    search_fields = ['officer__last_name', 'incident__case_number']
    resource_class = DetailsResource

class SiteTextAdmin(admin.ModelAdmin):
    form=AdminTextForm





admin.site.register(Officer, OfficerAdmin)
admin.site.register(Incident, IncidentAdmin)
admin.site.register(Details, DetailsAdmin)
admin.site.register(SiteText, SiteTextAdmin)

もう 1 つの注意点は、スプレッドシートに「id」列を配置する必要があることです。私は空白のままにすることができ、Django は主キーとして使用する ID を生成しました。

于 2016-12-17T14:51:14.720 に答える