tags:

views:

130

answers:

1

I have a requirement where one user creates an 'instance' of an object via a ModelForm. Another user of a different group has access to read all of the fields of the form, but has to update only one field. Think of a student who creates an exam object. Then a teach pulls up the exam and just needs to put in a grade, the rest of the exam is read only.

What's the best way to do that? Should I just query for the object, and display each field individually, then create a form (not a ModelForm?) for just the one field?

+2  A: 

Should I just query for the object, and display each field individually, then create a form (not a ModelForm?) for just the one field?

This is probably the best way to go about it. Note you can use a ModelForm for the teacher form, see the Django documentation on using a subset of fields on a model form. You will have to display all the other fields manually in your template, but you should probably have a separate template for this view (I would use separate views as well).

You could find some code for a read only field on Django Snippets, but generally it's better to be explicit about what fields you are updating from each view. This is likely to be more trouble than it's worth.

Wogan
+1: ModelForm with a subset of fields is best, list this option first. The special-purpose non-model form is a larger pain and not as obvious a solution. It's easy to have multiple model forms for different classes of users or different use cases.
S.Lott
using a subset of the fields will result in the form not displaying the rest of the fields - not optimal if you want them to be able to see the data entered, but not edit it. You can always apply a disabled = True attribute to the widget for each of the form fields. See the docs - http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs
Rishabh Manocha
S.Lott: Good idea, revised my answer slightly.Rishabh: Yes, displaying the fields not in the form was covered in the text I quoted. I have made it more explicit in my revision.
Wogan