Django DatabaseInlineFormSet Database Administrator after POST

Look for information on how django formsets based validation works, although it is more complicated than it sounds. I have a set of forms with values, some of these values ​​can be inserted there javascript (this means that they do not exist in the database yet).

class RequireOneFormSet(BaseInlineFormSet): def clean(self): if any(self.errors): return form_count = len([f for f in self.forms if f.cleaned_data]) if form_count < 1: raise ValidationError(_('At least one %(object)s is required.') % {'object': _(self.model._meta.object_name.lower())}) class VariantInline(admin.StackedInline): model = Variant extra = 1 formset = RequireOneFormSet class ProductAdmin(admin.ModelAdmin): class Meta: model = Product class Media: js = (os.path.join(STATIC_URL, 'js', 'admin_utils.js'), ) exclude = ('slug',) filter_horizontal = ('category',) inlines = [ImageInline, DetailInline, VariantInline] manufacturer = ModelChoiceField(Manufacturer.objects.all()) list_filter = ('name', 'manufacturer', 'category') list_display = ('name', 'manufacturer') search_fields = ('name',) save_as = True 

Further, based on these records, I would like to create objects during form validation. Django complains that there is no such object in the database when the "Save" button is clicked.

I tried to override the clean model method, clear ModelAdmin, save_formset from the form set, but no luck, since these values ​​created by javascript were filtered out earlier in the process. I'm looking for information on which method will take care of this, and can it be overestimated?

EDIT: The added code, the view used, is common from Django.

+1
source share
1 answer

I managed to solve it. The key was to create my own field and override the clean () method. As you can see in the django / forms / models.py file in the ModelMultipleChoiceField class clean () is responsible for checking the submission values.

 class DetailsField(ModelMultipleChoiceField): def clean(self, value): (code here) class VariantForm(ModelForm): details = DetailsField(queryset=Detail.objects.all(), widget=FilteredSelectMultiple('details', False)) class VariantInline(admin.StackedInline): model = Variant extra = 1 formset = RequireOneFormSet form = VariantForm 
0
source

All Articles