2

I have a django model

models.py

class Item(models.Model):
        author = models.ForeignKey(User)
        name = models.CharField('Brief summary of job', max_length=200)
        created = models.DateTimeField('Created', auto_now=True,auto_now_add=True)
        description = models.TextField('Description of job')

I would like to update the description field in a modelform using UpdateView. I would like to see the other fields (author,name etc) but want editing and POST disabled

forms.py

from django.forms import ModelForm
from todo.models import Item

class EditForm(ModelForm):
    class Meta:
        model = Item
        fields = ['author', 'job_for', 'name', 'description']

urls.py

urlpatterns = patterns('',
              # eg /todo/
              url(r'^$', views.IndexView.as_view(), name='index'),
              #eg /todo/5
              url(r'^(?P<pk>\d+)/$', views.UpdateItem.as_view(), name='update_item'),                
 )

views.py

class UpdateItem(UpdateView):
    model = Item
    form_class = EditForm
    template_name = 'todo/detail.html'
    # Revert to a named url
    success_url = reverse_lazy('index')

I have looked at the solution suggesting form.fields['field'].widget.attrs['readonly'] = True but I am unsure where to put this or how to implement

moadeep
  • 3,988
  • 10
  • 45
  • 72
  • 1
    Maybe this [post](http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-b) can help you. Especially `christophe31`'s answer. – Salvatore Avanzo Apr 15 '15 at 15:26

1 Answers1

0

Field.disabled New in Django 1.9. The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in favor of the value from the form’s initial data.

def MyForm(forms.ModelForm):
 class Meta:
  model = MyModel
 def __init__(self):
  self.fields['field'].disabled = True
Samamba
  • 113
  • 9