How to exclude django form field from render but still keep it for validation?

前端 未结 4 465
悲&欢浪女
悲&欢浪女 2021-01-19 17:42

If I have a model (extract):

class Coupon(models.Model):
    image = models.ImageField(max_length=64, null=True, upload_to=\"couponimages\")
<
4条回答
  •  醉话见心
    2021-01-19 17:56

    The best way to do what you want to do here is write a custom widget to display the file field differently. However, the best way to answer the exact question you asked, without having to render the form manually in the templates, is to overwrite the as_table method of the form:

    class InsertCoupon(forms.ModelForm):
    
        def as_table(self, *args, **kwargs):
            del self.fields['image']
            return super(InsertCoupon, self).as_table(*args, **kwargs)
    

提交回复
热议问题