Skip to content Skip to sidebar Skip to footer

Saving Formset With Drop-down-menu Foreignkey: Integrityerror Xxx_id May Not Be Null

I am trying to have a formset where each form (PropertySelector) has a drop-down menu (PropertySelector.property) whereas each item of that menu is ForeignKey reference to another

Solution 1:

The property_id isn't saved because you don't include it in your form.Meta. Leave away the fields, and it works:

defPropertySelForm():
    PropertyQueryset = Property.objects.filter(Q(basic=True))

    classPropertySelectorForm(ModelForm):
        property = ModelChoiceField(
            queryset=PropertyQueryset,
            widget=Select(attrs={'class': 'property'})
        )

        def__init__(self, *args, **kwargs): 
            super(ModelForm, self).__init__(*args, **kwargs)
            self.css_class = "prop_sel"classMeta:
            model = PropertySelector
            #fields = ("property_set", "title")
            widgets = {"title" : TextInput(attrs={"class" : "title"})}

    return PropertySelectorForm

I'd rename PropertySelForm to property_selectorform_factory, but that's just me. :)

Post a Comment for "Saving Formset With Drop-down-menu Foreignkey: Integrityerror Xxx_id May Not Be Null"