Wednesday, February 29, 2012

Filtering model searches by a property (as opposed to a field)

Hi all,

I have the following model:

<models.py>
class Todo(models.Model):
name = models.CharField(max_length=200)
due_date = models.DateTimeField()
location = models.CharField(max_length=200)
type = models.CharField(max_length=4, choices=TODO_TYPES)
owner = models.ForeignKey(User, verbose_name='owner')
done = models.BooleanField(default=False)
def _get_due_week(self):
return int(self.due_date.strftime("%W"))
due_week = property(_get_due_week)
def __unicode__(self):
return self.name
</models.py>

and this search code:

<views.py>
...
user = request.user
this_week = int(date.strftime("%W"))
this_weeks_todos =
Todo.objects.filter(owner__id=user.id).filter(due_week=this_week)
...
</views.py>

But I get:

"Cannot resolve keyword 'due_week' into field. Choices are: done,
due_date, id, location, name, owner, type"

Is it not possible to filter based on a property?

What is the correct method for doing this?

Thanks in advance for your help.

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment