What I would like to do is show a table listing all authors, and for each author list their respective books. I'm using a form as some of the fields will be modifiable from the page.
models.py
class Author(models.Model):
author_id = models.CharField(max_length=80, primary_key=True)
first_name = models.CharField(max_length=80)
last_name = models.CharField(max_length=80)
class Book((models.Model):
book_id = models.CharField(primary_key=True)
title = models.CharField(max_length=200)A
author_id = models.ForeignKey(Author, on_delete=models.CASCADE)
forms.py
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'
So in the view, I'm passing context which contains the two formsets to the template.
views.py
if request.method == 'GET':
author_dbrecords = Author.objects.all()
author_fields = [ f.name for f in Author._meta.get_fields() if not f.is_relation ]
authors_detail_list = [{ field:getattr(author, field) for field in author_fields } for author in author_dbrecords ]
book_dbrecords = Book.objects.all()
book_fields = [ f.name for f in Book._meta.get_fields() if not f.is_relation ]
books_detail_list = [{ field:getattr(book, field) for field in book_fields } for book in book_dbrecords ]
author_formset = AuthorFormSet(initial=authors_detail_list, prefix='author')
book_formset = AuthorFormSet(initial=book_detail_list, prefix='book')
context = {
'author_formset':author_formset,
'book_formset':book_formset,
}
return render(request, 'index.html' context)
In the template I can loop through the individual formsets, but I can't work out how to show all book titles for each author. Just using dot notation as you would for a regular model doesn't work.
index.html
<form action="{% url 'index' %}" method="post">
{{ author_formset.management_form }} {{ book_formset.management_form }}
{% for author in author_formset %}
<tr>
{% for author_field in author %}
<td> {{ author_field }}</td>
{% endfor %}
{{% for book_field in book.author %}
<td> {{ book_field }}</td>
{% endfor %}
I was able to resolve this by effectively accessing the underlying model (rather than form) in the template using ".instance", and using _set for the reverse relationship. I wrote a custom method in order to retrieve all fields from the Book model.
{% for author_field in author %}
<td> {{ author_field }}</td>
{% endfor %}
{% for book in author.instance.book_set.all %}
{% for book_field in book.get_fields %}
<td> {{ book_field }}</td>
{% endfor %}
{% endfor %}
New method in models.py:
def get_fields(self):
return [(field.value_to_string(self)) for field in Book._meta.fields]
Only downside at the moment is that I'm unable to check BookForm's hidden fields in the template.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.