0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Thomas.Q
  • 377
  • 1
  • 4
  • 12

1 Answers1

0

Edit: I misunderstood the requirements.

To get a list of lists of entries, with each list containing the entries for a specific topic:

entry_values = [
    [entry.value for entry in topic.entries.all()]
    for topic in Topic.objects.filter(owner=request.user).order_by("date_added")
]

This assumes you have set related_name to "entries" in your ForeignKey definition for Entry.topic.

kungphu
  • 4,592
  • 3
  • 28
  • 37
  • Thx. But I don't need to show such data, I only need to have such entries group by topics in the the list format (like: [[(u'apple',)], [(u'pear',), (u'banana',)], [(u'car',), (u'plane',)]]) for further processing. The a_content_list is the result for next function processing. Is my edition right? – Thomas.Q Oct 15 '18 at 05:35
  • I've updated the answer to return that format. If you actually need each value to be inside its own tuple, replace `entry.value` with `(entry.value,)`. – kungphu Oct 15 '18 at 05:41
  • I just found the error as: 'Topic' object has no attribute 'entries'. How could I deal with it? – Thomas.Q Oct 17 '18 at 03:29
  • That's what the last part of the answer is about. Check out the link I provided to see how to use the `related_name` argument. – kungphu Oct 17 '18 at 04:25
  • Thx! I just updated the question by adding what I set up in models.py. Can you take a look? – Thomas.Q Oct 17 '18 at 04:39
  • You should review the last sentence in my answer. It tells you exactly what to do to enable `topic.entries`. If that combined with the documentation link is not clear enough, [here is a question](https://stackoverflow.com/questions/2642613/what-is-related-name-used-for-in-django) that has good information about this attribute. – kungphu Oct 17 '18 at 04:44
  • I'm sorry. The outputs should be like [[(u'apple',), (u'pear',), (u'banana',)], [(u'car',), (u'plane',)]], I just changed the question – Thomas.Q Oct 17 '18 at 09:34