I've got a model of UserCoupon that a user can create and edit. Upon edit, I only want them to be able to edit the 'code' field on the instance of UserCoupon if there are no orders associated with that code. When there are orders associated with that coupon code, rather than outputting {{form.code}} on the edit coupon form, I'm outputting {{form.instance.code}}. When the user attempts to submit the form, I get an error saying that the field is required.
How can I make this field not required or otherwise address this situation so that the user can submit the form when one of the fields defined for the model form shows up in the template as an instance of the field rather than an input box?
Models.py
class UserCoupon(models.Model):
code = models.CharField(max_length=15, unique=True)
amount = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
forms.py
class EditUserCouponForm (forms.ModelForm):
class Meta:
model = UserCoupon
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user',None)
super(EditUserCouponForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(EditUserCouponForm, self).clean()
template
{% if coupon_order_count > 0 %}
{{form.instance.code}}
{% else %}
{{form.code}}
{% endif %}
Thanks!