Open marcinn opened 1 week ago
Will take a look. One thing I notice, check that you have the correct definition of after_init_instance()
.
New definition is:
def after_init_instance(self, instance, new, row, **kwargs):
This changed in v4. Please see release notes for more information.
The example app uses a Book import example, with 'author' as a selectable field. I just tried to reproduce your issue with the example app and found that the 'author' field is present after the 'confirm' step.
I wonder if your method declaration is the issue. Can you see if you can reproduce with the example app?
The root cause of the comes from missing status
field in confirm_form
. So there is necessity to redeclare both forms: import_form and confirm_form, when confirmation step is enabled, like this:
class VehicleImportForm(ImportForm):
status = forms.ChoiceField(
choices=Vehicle.Status.choices,
required=True,
label="Status",
help_text="Wybierz status dla importowanych rekordów",
)
class VehicleConfirmImportForm(ConfirmImportForm):
status = forms.ChoiceField(
choices=Vehicle.Status.choices,
required=True,
label="Status",
help_text="Wybierz status dla importowanych rekordów",
)
@admin.register(Vehicle)
class VehicleAdmin(ImportExportMixin, admin.ModelAdmin):
resource_classes = [VehicleResource]
import_form_class = VehicleImportForm
confirm_form_class = VehicleConfirmImportForm
skip_import_confirm = False
It was not so straightforward for me. I understood this after inspecting process_import()
and import_action()
with debugger.
I think there is no easy way to handle this case automatically... Of course I have an idea of finding "non-standard" fields in import_form_class
during process_import
, and add them as hidden fields to dynamically created confirm_form
(based on generic ConfirmForm
class), but this should be handled carefully and described in docs.
Sorry, I replied from my 2nd account.
Describe the bug Define custom import form and add field, like this
Admin:
Resource:
To Reproduce
Steps to reproduce the behavior:
after_init_instance
'status' in kwargs
, will beTrue
'status' in kwargs
, will beFalse
- i.e. nostatus
inkwargs
.Versions (please complete the following information):
Expected behavior In
after_init_instance
keystatus
should be inkwargs
. Import should set model's status field to a selected value from the input form.Screenshots
Additional context