How can I query on the full name in Django?
To clarify, I essentially want to do create a temporary column, combining first_name and last_name to give a fullname, then do a LIKE on that, like so:
select [fields] from Users where CONCAT(first_name, ' ', last_name) LIKE '%John Smith%";
The above query would return all users named John Smith. If possible I’d like to avoid using a raw SQL call.
The model I’m talking about specifically is the stock django.contrib.auth.models User model. Making changes to the model directly isn’t a problem.
For example, if a user was to search for ‘John Paul Smith’, it should match users with a first name of ‘John Paul’ and last name ‘Smith’, as well as users with first name ‘John’ and last name ‘Paul Smith’.
Unless I’m missing something, you can use python to query your DB like so:
Edit:
To answer your question:
If you need to return ‘John Paul Smith’ when the user searches for ‘John Smith’ then you can use ‘contains‘ which translates into a SQL LIKE. If you just need the capacity to store the name ‘John Paul’ put both names in the first_name column.
This translates to: