In my model there is model method I'd like to use for filtering.
Is it possible? I have put it in form of property, but when I use it
with field lookup syntax I get an error.
This is a minimal sample model that should explain what I mean:
class Category(models.Model):
name = models.CharField(maxlength=16)
class Content(models.Model):
title = models.CharField(maxlength=32)
category = models.ForeignKey(Category)
pub_date = models.DateTimeField()
priv = BooleanField(default=True)
def _visible(self):
return (self.priv == False and self.pub_date < datetime.now())
visible = property(_visible)
>>> c = Content.objects.get(pk=1)
>>> c.visible
False
>>> Category.objects.filter(visible=False)
Traceback (most recent call last):
...
TypeError: Cannot resolve keyword 'visible' into field
Filtering in this way would come handy in a custom template tag, where
I'd like to ask things in this way:
Category.objects.get_categories(user), where get_categories is a
Category's custom manager method that returns different results
depending on user's authentication status.
def get_categories(self, user=None):
c_qs = Category.objects.all()
if hasattr(user, 'is_staff') and user.is_staff:
res = [(c, c.content_set.count()) for c in c_qs]
else:
res = [(c, c.content_set.filter(visible=True).count()) for c
in c_qs]
return res
Any suggestion?
Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---