1👍
✅
It is simpler to just check the list of brand_names
and filter if it contains at least one element:
def some_brand_mobiles(*brand_names):
if brand_names:
return Mobile.objects.filter(brand__name__in=brand_names)
else:
return Mobile.objects.all()
0👍
Try this solution:
def some_brand_mobiles(*brand_names):
queryset = Mobile.objects.all()
if brand_names:
queryset = queryset.filter(brand__name__in=brand_names)
return queryset
In that way you can add more filters based on any other condition over the queryset
.
- [Answered ]-Django API: Upload user files to S3 using Celery to make process async
- [Answered ]-DatabaseError with django-avatar
- [Answered ]-Display objects from inline model in DetailView
- [Answered ]-Django foreign keys cascade deleting and "related_name" parameter (bug?)
Source:stackexchange.com