Django custom manager and queryset narrowed result -


i want able in template:

request.user.notifications.unseen 

and show user unseen notifications has.

so far, based on few posts found here, able develop this:

class notificationqueryset(models.queryset):     def unseen(self):         return self.filter(is_read=false)   class notificationmanager(models.manager):     use_for_related_fields = true      def get_query_set(self):         return notificationqueryset(self.model)      def unseen(self, *args, **kwargs):         return self.get_query_set().unseen(*args, **kwargs) 

it works fine, shows 'unseen' notifications users.

what thought specifying object relation, such user.notifications, argument passed function narrowing results user requesting it.

so, how narrow results 1 user , not of them?

edit

models.py

class usernotification(models.model):      user = models.foreignkey(user, related_name='notifications')     advertisement = models.foreignkey(advertisement)     creation_date = models.datetimefield(editable=false, default=timezone.now)     notification_type = models.charfield(max_length=3, choices=notification_type)     is_read = models.booleanfield()      objects = notificationmanager() 

class notificationmanager(models.manager):     def unseen(self,user):       queryset = super(notificationmanager,self).get_queryset()       queryset = queryset.filter(user=user,is_read=false)       return queryset  class usernotifications(models.model):      ... same fields ...     objects = notificationmanager() 

in view:

queryset = usernotifications.objects.unseen(self.request.user) 

you queryset can further filter, use all(), get() etc, other queryset. add args, kwargs , more methods manager, while generic objects manager doesn't change.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -