tags:

views:

41

answers:

1

I allow users to view and edit a few fields of a database record represented by a ModelForm. Here is a snippet of the code from the view:

def edit(request, id):
    obj = get_object_or_404(Record, pk=record_id)
    if request.method == 'POST':
        form = forms.RecordForm(request.POST, instance=obj)
        if form.is_valid(): 
            form.save()

The problem is that because I don't pass all the fields to the template, form.is_valid() fails with a missing values error. How can I update an existing record with just the subset of record fields I display to the user?

+3  A: 

Use the fields tuple in the form's Meta definition to make sure the form only includes the fields you need - or use exclude to remove the ones you don't want.

Daniel Roseman
That worked. Thank you.
swisstony
No problem. Why do I always want to say "You know, saving a form is very much like making love to a beautiful woman..." whenever I see one of your questions?
Daniel Roseman
I guess the phrase works in the world of programming too :-)
swisstony