Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django how to get count for manytomany field

I have model for question:

class Question(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=120)
    description = models.TextField()
    answers = models.ManyToManyField('Answer',related_name='answer_name', blank=True)
    post_date = models.DateTimeField(auto_now=True)

def __unicode__(self):
    return self.title

And I have model for answer:

class Answer(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)
    ans_body = models.TextField()
    post_date = models.DateTimeField(auto_now=True)

def __unicode__(self):
    return self.ans_body

Question creation and answer submission are working perfectly. I cant correctly show answer for particular question. But when I try to get the count of answer for particular question its not showing. It displays 0 count.

In my view I am getting the list of the answer by:

context["question_list"] = Question.objects.all()

And in my template

{% for question in question_list %}
   {{ question.title }}
    Ans:{{question.answers.count}}
{% endfor %}

When I do this I get the count 0 if there are answers. How can I get the count of the answers for particular questions.

like image 796
gamer Avatar asked Nov 26 '14 13:11

gamer


People also ask

What is many to many field in Django?

A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.

What is Set_all in Django?

The _set is a reverse lookup class variable django puts in for you. So, given object b - you would do: entries = b.entry_set.all() The reason the reverse is a queryset is, ForeignKey is 1-to-many relationship. Hence, the reverse is a queryset. The _set object is made available when related_name is not specified.

What are Django fields?

Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField.

How do I add a field to a Django model?

To answer your question, with the new migration introduced in Django 1.7, in order to add a new field to a model you can simply add that field to your model and initialize migrations with ./manage.py makemigrations and then run ./manage.py migrate and the new field will be added to your DB. Save this answer.


1 Answers

Why not use: Question.objects.all().count()

For my project, I have a Field in 'Info' Model

users_like = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="%(app_label)s_%(class)s_likes", blank=True)

I use below code to count the number of like then show it in Admin List Page.

# admin.py
from django.contrib import admin

from .models import Info
class InfoAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'like_count',)
    def like_count(self, obj):
        return obj.users_like.all().count()

admin.site.register(Info, InfoAdmin)

The result is: image Hope these can help you!

like image 89
C.K. Avatar answered Oct 01 '22 12:10

C.K.