python - Disable all the options except the default - ChoiceField Django Forms -
so here trying do:
i have way able make of fields readonly depending on user.
so far it's easy here comes problem, need make select input read , well...they don't work read only. thing user still able play though new value not submitted. want select field disabled if that, value not sent via post , there problem form valid method.
so after reading around, understand way disable fields except 1 selected default?
how possible
here have far:
def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') super(teamform, self).__init__(*args, **kwargs) instance = getattr(self, 'instance', none) if instance , instance.pk none: self.fields['division'].initial = 1 user_role = self.user.memberaccount.get_role() if user_role != 'admin':
and here whhere want disable choices....
thanks lot help, ara
i see 2 possible choices here:
create custom widget show disabled select alongside hidden input containing value submit https://docs.djangoproject.com/en/dev/ref/forms/widgets/#customizing-widget-instances
set field required=false , create clean_division() method inside form populate field if hasn't been submitted: https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute
Comments
Post a Comment