Form theory - locking a field

26 views
Skip to first unread message

Clive Bruton

unread,
Jun 24, 2020, 10:37:49 AM6/24/20
to django...@googlegroups.com
I have a form in which, after filing, I would like one field to be
locked for editing, the forms.py looks like this:

**************

class ProfileForm(forms.ModelForm):
status = forms.ChoiceField(choices=Profile.status_choices,
widget=forms.RadioSelect)

class Meta:
model = Profile
fields = (
'status', 'phone', 'display_phone', 'display_address',
'display_email', 'display_loginoverride'
)
widgets = {
'phone': PhoneWidget
}

**************


While the user will be able to come back and change the other
information on the form, I want to be able to lock the "status"
field, so that it just shows the input they selected, not the radio
buttons and is no longer editable by the user.

Is there a way to do this in forms.py, or would I have to construct
the logic in a template (or perhaps some other way)?

Thanks


-- Clive

Chetan Ganji

unread,
Jun 24, 2020, 6:04:16 PM6/24/20
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7A4BCC9C-172C-4511-A422-6E832A72E11D%40indx.co.uk.

Clive Bruton

unread,
Jun 24, 2020, 11:13:02 PM6/24/20
to django...@googlegroups.com

On 24 Jun 2020, at 23:03, Chetan Ganji wrote:

> Try this one
> https://docs.djangoproject.com/en/3.0/ref/forms/fields/#disabled

Ok, well that's a little help, because I can disable the input:

status = forms.ChoiceField(choices=Profile.status_choices,
widget=forms.RadioSelect(attrs={'disabled': True}))

And I can put a "useless" if statement around it to get different
outputs:

if 1 == 1 :
status = forms.ChoiceField(choices=Profile.status_choices,
widget=forms.RadioSelect(attrs={'disabled': True}))
else :
status = forms.ChoiceField(choices=Profile.status_choices,
widget=forms.RadioSelect(attrs={}))

But, I can't work out how to get a meaningful if comparison, like:

if Profile.status == 'personal' :
status = forms.ChoiceField(choices=Profile.status_choices,
widget=forms.RadioSelect(attrs={'disabled': True}))
else :
status = forms.ChoiceField(choices=Profile.status_choices,
widget=forms.RadioSelect(attrs={}))

Any string I put in 'personal' always returns false, so I can switch
the output with "==" or "!=", but I can't assess the output in
reference to what I expect to be in the database fields. Django
accepts "Profile.status" as a relevant property, so I am a bit lost
as to how to continue, or debug this. If I try "print(locals()) or
print(Profile.status) I get no output in the terminal.


-- Clive
Reply all
Reply to author
Forward
0 new messages