Wednesday, 25 July 2018

How to count no of child create on parent model??

Step 1 : make child mode with apply Foreign Key on child model with parent model
ex:
first model
class Poll(models.Model):
    poll_id = models.AutoField(primary_key=True)

second model
class Questions(models.Model):
    poll = models.ForeignKey(Poll, models.DO_NOTHING, db_column='poll', blank=True, null=True, related_name="poll")

In poll template:

{{ p.poll.count }}

No comments:

Post a Comment

Django rest api - filter

views.py from django_filters.rest_framework import DjangoFilterBackend class PollList(viewsets.ModelViewSet):     queryset = X.objects...