自定义管理员导入表单

Customize admin import forms

我想在管理页面的导入表单中添加一个额外的字段。 我做了文档所说的一切,但输入的年份值和 calender_week 不会显示。

resources.py

class ForecastListResource(resources.ModelResource):
    year = fields.Field(column_name="Jahr", attribute="year")
    calender_week = fields.Field(column_name="Kalenderwoche", attribute="calender_week")
    brand = fields.Field(column_name="Marke", attribute="brand")
    material = fields.Field(column_name="Material", attribute="material")
    material_short_text = fields.Field(column_name="Materialkurztext", attribute="material_short_text")
    gmc = fields.Field(column_name="GMC", attribute="gmc")
    gmc_text = fields.Field(column_name="GMC Text", attribute="gmc_text")
    bed_date = fields.Field(column_name="BedTermin", attribute="bed_date")
    bed_amount = fields.Field(column_name="BedMenge", attribute="bed_amount")
    bed_week = fields.Field(column_name="BedWoche", attribute="bed_week")
    code_last_bed_week = fields.Field(column_name="Code letzte KW", attribute="code_last_bed_week")
    fabric_number = fields.Field(column_name="Stoffnummer", attribute="fabric_number")
    print_stage_3 = fields.Field(column_name="Druckstufe 3", attribute="print_stage_3")
    average_filling = fields.Field(column_name="Mittelwert Abfüllung", attribute="average_filling")
    net = fields.Field(column_name="Netto", attribute="net")

    class Meta:
        model = ForeCastList
        use_bulk = True
        skip_unchanged = True

forms.py

class ForecastDoDImportFormMixin(forms.Form):
    calender_week = forms.IntegerField(label="Kalenderwoche", required=True)
    year = forms.IntegerField(label="Jahr", required=True)


class ForecastDoDImportForm(ForecastDoDImportFormMixin, ImportForm):
    pass


class ForecastDoDConfirmImportForm(ForecastDoDImportFormMixin, ConfirmImportForm):
    pass

admin.py

@admin.register(ForeCastList)
class ForeCastList(ImportExportModelAdmin):
    resource_class = ForecastListResource

    def get_import_form(self):
        return ForecastDoDImportForm

    def get_confirm_import_form(self):
        return ForecastDoDConfirmImportForm

    def get_form_kwargs(self, form, *args, **kwargs):
        if isinstance(form, ForecastDoDImportForm):
            if form.is_valid():
                kwargs.update({"calender_week": form.cleaned_data["calender_week"], "year": form.cleaned_data["year"]})
        return kwargs

    def get_import_data_kwargs(self, request, *args, **kwargs):
        print(kwargs)
        return super().get_import_data_kwargs(request, *args, **kwargs)

Import-Form

Result

-> 文档中的相关部分: https://django-import-export.readthedocs.io/en/latest/getting_started.html#customize-admin-import-forms

如果我没理解错的话,您想 select 从下拉列表中获取一个值并将其插入到所有行中。这意味着如果导入文件中有 'Jahr' 的值,将忽略此值,取而代之的是从下拉列表中编辑的值 select。

将导入字段设置为下拉列表中的 selection 可以通过将下拉列表的值分配给要导入的对象的实例来实现,如下所示。 (我使用了单个字段 'year' 而不是 'Jahr' 和 'Kalenderwoche' 但您可以按照相同的方式更新您的实现)。

class ForecastListResource(resources.ModelResource):
    year = fields.Field(column_name="Jahr", attribute="year")
    # (other fields removed for brevity

    def after_import_instance(self, instance, new, row_number=None, **kwargs):
        # override to set the value of the dropdown onto the row instance
        instance.year = kwargs.get("year")

    class Meta:
        model = ForeCastList
        use_bulk = True
        skip_unchanged = True

声明 ModelAdmin class 以从表单中读取 'year' 值:

@admin.register(ForeCastList)
class ForeCastList(ImportExportModelAdmin):
    resource_class = ForecastListResource

    # other methods removed

    def get_import_data_kwargs(self, request, *args, **kwargs):
        form = kwargs.get('form')
        if form:
            return {"year": form.cleaned_data["year"]}
        return dict()

我能够在示例应用程序中完成此操作: