How can I disable a model field in a django form

Posted by jammon on Stack Overflow See other posts from Stack Overflow or by jammon
Published on 2011-02-09T13:56:03Z Indexed on 2011/02/09 15:25 UTC
Read the original article Hit count: 193

Filed under:
|
|

I have a model like this:

class MyModel(models.Model):
    REGULAR = 1
    PREMIUM = 2
    STATUS_CHOICES = ((REGULAR, "regular"), (PREMIUM, "premium"))
    name = models.CharField(max_length=30)
    status = models.IntegerField(choices = STATUS_CHOICES, default = REGULAR)

class MyForm(forms.ModelForm):
    class Meta:
        model = models.MyModel

In a view I initialize one field and try to make it non-editable:

myform = MyForm(initial = {'status': requested_status})
myform.fields['status'].editable = False

But the user can still change that field.

What's the real way to accomplish what I'm after?

© Stack Overflow or respective owner

Related posts about python

Related posts about django