Python Forum
Question on returning methods in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on returning methods in a class
#1
As the title states, I'm trying to figure out how to access the return on a query in django.

models.py
class Board(models.Model):
    category = models.CharField(max_length=20, unique=True)
    description = models.CharField(max_length=100)

    def __str__(self):
        return self.category


class Topic(models.Model):
    subject = models.CharField(max_length=100, unique=True)
    board = models.ForeignKey(Board, on_delete=models.CASCADE)
views.py
class HomeView(ListView):
    model = Board
    context_object_name = 'categories'
    template_name = 'boards/forum.html'
    queryset = Board.objects.all()
    titles=queryset

    def get_category_subjects(self):

        for title in self.titles:
            subjects = Topic.objects.filter(board_id=title.id)
        return subjects
for category in categories:
    print(category)
    for title in category.get_category_subjects:
        print(title)
Any help would be great. Thanks.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
Figured it out. Probably not the best approach but works.

Revised views.py
class HomeView(ListView):
    model = Board
    context_object_name = 'categories'
    template_name = 'boards/forum.html'
    queryset = Board.objects.all()


    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['subjects']=[]
        for titles in self.queryset:
            context['subjects'].append(Topic.objects.filter(board_id=titles.id))
        return context
In the html
{% for category in categories %}
    {{category}}
                    {% for titles in subjects %}
                        {% for title in titles %}
                            {% if title.board_id == category.id %}
                            <a href="#">{{title.subject}}</a><br>
                            {% endif %}
                        {% endfor %}
                    {% endfor %}
{% endfor %}
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
class Board(models.Model):
    category = models.CharField(max_length=20, unique=True)
    description = models.CharField(max_length=100)
 
    def __str__(self):
        return self.category
Using this as example, if you want to return a self.catagory you'll need to define it with self. above.

Then if you want access to it I think this is the way, but instead of using Board use the instance name.

what_you_want = Board._str_()
Reply
#4
The approach I took works, I just don't really like having to do a loop inside a loop inside a loop.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Click on unusual class button using mechanize Ask Question Coto 1 3,840 Feb-18-2018, 07:27 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020