[Answer]-Django forms get field output in view

1๐Ÿ‘

โœ…

If you form has instance associated to it, you can try

post = EditPost.objects.get(id=id)
form1 = EditPostForm(instance=post)
form1.instance.about

Based on your comment below if you are using ManyToMany relation you can get the value as

>>> bf = BookForm(instance=book)
>>> bf.instance.authors
<django.db.models.fields.related.ManyRelatedManager object at 0x0000000004658B38>
>>> bf.instance.authors.all() #which returns a query set of related objects
[<Author: Kotian>]
>>> bf.instance.authors.all()[0]
<Author: Kotian>
>>> bf.instance.authors.all()[0].name
u'Kotian'

or based on how you have defined the ManyToMany

>>> af = AuthorForm(instance=author)
>>> af.instance.name
u'MyName'
>>> af.instance.book_set
<django.db.models.fields.related.ManyRelatedManager object at 0x0000000004658C18>
>>> af.instance.book_set.all() # returns queryset
[<Book: Book object>, <Book: Book object>]
>>> af.instance.book_set.all()[0] #accessing first object here
<Book: Book object>
>>> af.instance.book_set.all()[0].name
u'Lepord'
๐Ÿ‘คakotian

Leave a comment