Issue
I was wondering if there was a way to use Django's filter() on query sets using a dynamically generated python property using property()
. I have first_name
and last_name
of every user, and I want to filter based on their concatenated name first_name last_name
. (The reason behind this is that when I do autocomplete I search to see if the query matches first name, last name, or part of the concatenation. I want John S
to match John Smith
, for example.
I created a property of name
:
def _get_name(self):
return self.first_name + " " + self.last_name
name = property(_get_name)
This way I can call user.name
to get the concatenated name.
However, if I try to do User.objects.filter(name__istartswith=query)
I get the error Cannot resolve keyword 'name' into field.
Any ideas on how to do this? Do I have to create another field in the database to store the full name?
Solution
filter()
operates on the database level (it actually writes SQL), so it won't be possible to use it for any queries based on your python code (dynamic property in your question)
.
This is an answer put together from many other answers in this department : )
Answered By - Yuji 'Tomita' Tomita
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.