Mridang Agarwalla
Mridang Agarwalla

Reputation: 44958

Getting the value of a ModelForm's field

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

Upvotes: 4

Views: 8351

Answers (1)

Issac Kelly
Issac Kelly

Reputation: 6359

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']

Upvotes: 5

Related Questions