Showing custom model validation exceptions in the Django admin site

后端 未结 4 1279
悲&欢浪女
悲&欢浪女 2021-02-07 00:27

I have a booking model that needs to check if the item being booked out is available. I would like to have the logic behind figuring out if the item is available centralised so

4条回答
  •  梦如初夏
    2021-02-07 00:54

    The best way is put the validation one field is use the ModelForm... [ forms.py]

    class FormProduct(forms.ModelForm):
    
    class Meta:
        model = Product
    
    def clean_photo(self):
        if self.cleaned_data["photo"] is None:
            raise forms.ValidationError(u"You need set some imagem.")
    

    And set the FORM that you create in respective model admin [ admin.py ]

    class ProductAdmin(admin.ModelAdmin):
         form = FormProduct
    

提交回复
热议问题