models.py
class Category(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Gender(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Post(models.Model):
name = models.CharField(max_length=50)
categories = models.ManyToManyField(Category)
genders = models.ManyToManyField(Gender)
def __unicode__(self):
return self.name
view.py:
def index(request):
posts = Post.objects.filter(genders=1)
categories = Category.objects.filter(post__genders=1)
.exclude(post=None).order_by('-sort')
Why do I get the same category 3 times if there are 3 posts where gender is equal to 1?? I only need the category 1 time i my select like "select distinct" - how can I do so??
Did you try attaching a .distinct()
to the queryset?
categories = Category.objects.filter(post__genders=1).exclude(post=
None).order_by('-sort').distinct()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With