0👍
✅
You can add a method get_families
to your model Brand
class Brand(models.Model):
title = models.CharField()
def get_families(self):
return self.families.filter(num_cars__gt = 0)
And then do something like this in your views.
{% for family in brand.get_families %}
{{ family }} {{ family.num_cars }}
{% endfor %}
1👍
You can achieve this kind of behavour with custom managers in Django.
https://docs.djangoproject.com/en/1.10/topics/db/managers/
for example
class FamilyWithCarManager(models.Manager):
def get_query_set(self):
return super(FamilyWithCarManager, self).get_query_set().filter(num_cars__gte=0)
and then in your Family model:
class Family(models.Model):
name = models.CharField(max_length=50)
with_cars = FamilyWithCarManager()
then you should be able to write a query like this:
Family.with_cars.all()
- Django M2M with addititional data with through
- Paragraph element prepending not appending to table in django html
- How to design Django url pattern to avoid 301
- Django endless pagination on scroll in case of overflowed container
Source:stackexchange.com