4

Would any of you be able to tell me how I can get the data/value of a model-form's fields? I know how to get the initial data but if I've understood correctly, a form's fields also have a data/value value associated with it.

Thanks

4
  • Are you talking about after a user submits a form? Commented Oct 11, 2011 at 18:07
  • 1
    A form only has it's initial value or it's related db value (fi NULL) before someone submits it.. (well except for clientside scripting, javascript etc), so I suppose you mean after it's submitted.. which is just form.field.value()...? Commented Oct 11, 2011 at 18:11
  • HI ArgsKwargs, I managed to do it using your answer. This worked form['fieldsname'].value(). Thanks Commented Oct 11, 2011 at 18:16
  • you should be using cleaned_data as that's the python representation of the object (casts to an integer for forms.IntegerField for example) see my answer and the docs below Commented Oct 11, 2011 at 18:22

1 Answer 1

5

You use cleaned_data https://docs.djangoproject.com/en/dev/topics/forms/#processing-the-data-from-a-form Here's an example:

>> models.py
class Book(models.Model):
    author = models.CharField(max_length=140)

>> forms.py
class BookForm(forms.ModelForm):

    class Meta:
        model = Book

>> views.py

def book_update(request):
    form = BookForm(request.POST or None)
    if form.is_valid():
        print form.cleaned_data['author']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.