I have a Django project with the structure like: In the main page, there are many topics. In each topic, there are many entries related to it. The topics and entries information could be the inputs by visitors. Now I need to acquire the data from the entries by groups. For example, for topic "fruit", entries include "apple", "pear", "banada"; topic "transportation", entries include "car", "plane".
I need the acquired data to be:
[[(u'apple',), (u'pear',), (u'banana',)], [(u'car',), (u'plane',)]].
How could I edit the coding in the function of view.py? I proposed like:
def button_view(request):
import sys
topics = Topic.objects.filter(owner=request.user).order_by("date_added")
a_content_list = []
for a in topics
entries = Entry.objects.get(topic = a)
a_content_list.append(entries)
Here's what I set up in models.py:
class Topic(models.Model):
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
def _str_(self):
return self.text
class Entry(models.Model):
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def _str_(self):
return self.text[:50] + "..."
#on_delete=models.CASCADE,
Could the a_content_list be what I want?