codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

How can I filter a queryset based upon two manytomany relationships having one object in common?


By : , Category : python

As pointed out by danihp, if a People object has a owned_animals field try people_with_a_farm_animal = People.objects.filter( owned_animals__in=farm_animals).distinct().

ReLated :

Why do you want that all in one queryset ? Venue.objects.get(id=<id_venue1>).event_set.all() should return you all the events linked to venue1 in one queryset, and you can have venue1 in another queryset.


You can also use Venue.objects.get(id=<id_venue1>).values('adress', 'phone_number', 'event__name', 'event__description') to get something like the array of dicts bellow:

[
  { 'adress': '<venue1adress>', 'phone_number': '<venue1phone>', 'event__name': 'foo', 'event__description': 'I am a foo event'},
  { 'adress': '<venue1adress>', 'phone_number': '<venue1phone>', 'event__name': 'bar', 'event__description': 'I am a bar event'},
  ...
]

Finaly , I think, you can also rework the default queryset (since it's a python objects, and with python objects you can do anything you want):

events_for_venur1 = Event.objects.filter(venues=venue1)
for event in events_for_venur1:
  for venue in event.venue_set:
    if venue != venue1:
      venue = None
# Here your queryset is ready, but DO NOT try to save it in the DB !!

This can be done using an annotation and a filter:

from django.db.models import Count
idle = Manufacturers.objects.annotate(num_products=Count('product')).filter(num_products=0)

idle will the contain a list of Manufacturers who have no products, but keep in mind they will also have a property num_products, which due to the query will be 0.

Change your queryset like:

def get_queryset(self, request):
    user_grps = request.user.groups.all()
    cat_lst = Category.objects.filter(organizers_group__in=user_grps)

    return queryset = super(TaskAdmin, self).get_queryset(request).filter(
    categories__in=cat_lst
    )

Use __in expression as

b = Box.objects.filter(my_custom_item__id__in=[i,])
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .