Skip to content Skip to sidebar Skip to footer

Django: Different Behaviour In Createview And Updateview With Unique Constraint

class Badge(Model): # .... class Meta: unique_together = ('identifier', 'restaurant') Using a CreateView, when creating a Badge object whose identifier already e

Solution 1:

I just realised for validation to work, that all fields need to be specified in the class based view, even if these fields should not be filled by the User.

class BadgesUpdateView(UpdateView):
    model = Badge
    # restaurant field must be included for validation even if the user does NOT fill it.
    fields = ('identifier', 'is_active', 'owner', 'restaurant')

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.fields['restaurant'].widget = forms.HiddenInput()
        return form

Post a Comment for "Django: Different Behaviour In Createview And Updateview With Unique Constraint"