class A:
some fields
class B:
user = models.ForeignKey(User, **CASCADE, related_name='sessions')
DEVICES = (
('android', 'android'),
('ios', 'ios')
)
We are creating a dashboard for front end and there is a search field, user supposed to type the device_type and we used to fetch the records who uses only the IOS or Android.
In this Case the records is stored in the fields like,
Created_at, updated_at, device_type, user_id...
we need to apply two filters now here:
- searched_key : ios or android
- latest_device_type : only latest updated_record
I have done like :
User.objects.filter(Q(id=user_details['id']) & Q(sessions__device_type=self.search_key) & Q(reduce(or_, self.query_filter))).order_by('sessions__user_id', '-sessions__created_at').distinct('sessions__user_id'). \
values('id', 'sessions__device_type', 'first_name')
But am not getting exact output : even i tried [0] putting this after order_by and latest() and first() method will fetch only one record so any 1 has the best solution for this. Note : if anybody try to explain with Prefetch please explain with clarity.