Searching name and surname in DjangoDjango queryset filter after a concatenation of two columnsQuerying full name in DjangoCalling a function of a module by using its name (a string)Does Django scale?django - inlineformset_factory with more than one ForeignKeyQuerying full name in DjangoRadio buttons in django adminDjango: Send posted files to a different formIn Django, what's the meaning of allow_blank and style = 'base_template' : 'textarea.html' as CharField attribute?How to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldDjango auto_now=True for Timestamp not working when using normal mysql update query
What is the practical impact of using System.Random which is not cryptographically random?
Can UV radiation be safe for the skin?
Is it possible for a person to be tricked into becoming a lich?
Is Borg adaptation only temporary?
“all of who” or “all of whom”?
Unexpected behavior after assignment of function object to function wrapper
Can copper pour be used as an alternative to large traces?
What should be done with the carbon when using magic to get oxygen from carbon dioxide?
Sandwich Sudoku: First 12 digits of pi
Rapid change in character
An ant walks on a cube over the diagonals of little cubes. Can it visit all little faces exactly once?
My colleague treats me like he's my boss, yet we're on the same level
Why did dark energy play a subdominant role in the radiation and matter dominated era?
Moscow SVO airport, how to avoid scam taxis without pre-booking?
What are ways to record who took the pictures if a camera is used by multiple people?
Was it illegal to blaspheme God in Antioch in 360.-410.?
In Endgame, wouldn't Stark have remembered Hulk busting out of the stairwell?
Can two aircraft be allowed to stay on the same runway at the same time?
Terminology of atomic spectroscopy: Difference Among Term, States and Level
When is it a good idea to capture the fianchettoed bishop?
Sort Associations by its Values (which are nested lists)
Could a complex system of reaction wheels be used to propel a spacecraft?
Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?
GPL Licensed Woocommerce paid plugins
Searching name and surname in Django
Django queryset filter after a concatenation of two columnsQuerying full name in DjangoCalling a function of a module by using its name (a string)Does Django scale?django - inlineformset_factory with more than one ForeignKeyQuerying full name in DjangoRadio buttons in django adminDjango: Send posted files to a different formIn Django, what's the meaning of allow_blank and style = 'base_template' : 'textarea.html' as CharField attribute?How to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldDjango auto_now=True for Timestamp not working when using normal mysql update query
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have this model on Django:
class Profile(models.Model):
name = models.CharField(max_length=50, blank = False)
surname = models.CharField(max_length=100, blank = False)
...
For example, I have 2 profiles in the db:
- John Doe
- John Smith
I want to do a form search, that searches in both name and surname attributes.
I tried:
Q(name__icontains=text) | Q(surname__icontains=text)
But this doesn't work, for example if I search "John Doe" it returns both of them.
Edit:
Basically what I want is something like "joining" both name and surname attributes to search in, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
python django
add a comment |
I have this model on Django:
class Profile(models.Model):
name = models.CharField(max_length=50, blank = False)
surname = models.CharField(max_length=100, blank = False)
...
For example, I have 2 profiles in the db:
- John Doe
- John Smith
I want to do a form search, that searches in both name and surname attributes.
I tried:
Q(name__icontains=text) | Q(surname__icontains=text)
But this doesn't work, for example if I search "John Doe" it returns both of them.
Edit:
Basically what I want is something like "joining" both name and surname attributes to search in, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
python django
I think what you want is an&
instead of|
– dcg
Mar 27 at 22:56
add a comment |
I have this model on Django:
class Profile(models.Model):
name = models.CharField(max_length=50, blank = False)
surname = models.CharField(max_length=100, blank = False)
...
For example, I have 2 profiles in the db:
- John Doe
- John Smith
I want to do a form search, that searches in both name and surname attributes.
I tried:
Q(name__icontains=text) | Q(surname__icontains=text)
But this doesn't work, for example if I search "John Doe" it returns both of them.
Edit:
Basically what I want is something like "joining" both name and surname attributes to search in, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
python django
I have this model on Django:
class Profile(models.Model):
name = models.CharField(max_length=50, blank = False)
surname = models.CharField(max_length=100, blank = False)
...
For example, I have 2 profiles in the db:
- John Doe
- John Smith
I want to do a form search, that searches in both name and surname attributes.
I tried:
Q(name__icontains=text) | Q(surname__icontains=text)
But this doesn't work, for example if I search "John Doe" it returns both of them.
Edit:
Basically what I want is something like "joining" both name and surname attributes to search in, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
python django
python django
edited Mar 27 at 23:23
MakeItFun
asked Mar 27 at 22:54
MakeItFunMakeItFun
247 bronze badges
247 bronze badges
I think what you want is an&
instead of|
– dcg
Mar 27 at 22:56
add a comment |
I think what you want is an&
instead of|
– dcg
Mar 27 at 22:56
I think what you want is an
&
instead of |
– dcg
Mar 27 at 22:56
I think what you want is an
&
instead of |
– dcg
Mar 27 at 22:56
add a comment |
2 Answers
2
active
oldest
votes
Try this,
from django.db.models import Value as V
from django.db.models.functions import Concat
text = "John Doe"
Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Reference
Concat
DB fucntion
Django queryset filter after a concatenation of two columns
Django shell output
In [14]: from django.db.models import Value as V
In [15]: from django.db.models.functions import Concat
In [16]: text = "John Doe"
In [17]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[17]: <QuerySet [<Profile: John Doe>]>
In [18]: text = "john"
In [19]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[19]: <QuerySet [<Profile: John Doe>, <Profile: John Smith>]>
In [20]: text="smith"
In [21]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[21]: <QuerySet [<Profile: John Smith>]>
add a comment |
This is a modified copy/paste from this answer.
I tried to come up with various ways, but they all required counting rows after each query which would make it really time consuming. Instead, the best way seem to be to split up the text by spaces and apply filters to it:
def get_profiles(text):
qs = Profile.objects.all()
for term in text.split():
qs = qs.filter( Q(name__icontains = term) | Q(surname__icontains = term))
return qs
If I search "John" it would not return anything...
– MakeItFun
Mar 27 at 23:09
Yes, this logic will filter on both name and surname. Do you want the second parameter (name or lastname) to be excluded from the search if it's omitted, but use both parameters if both are given? I think you need to expand your examples of what you provide and what you expect in your original question to make it a bit more clearer.
– Johan
Mar 27 at 23:13
Yes, sorry, I will edit to add more examples. Basically what I want is something like "join" both name and surname attributes, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
– MakeItFun
Mar 27 at 23:22
1
@MakeItFun I understand, I have updated my answer now. It's now instead a function that applies the filters which you can try
– Johan
Mar 28 at 0:02
It works! Thank you. So 'qs' only store distinct profiles for every iteration? I don't get how it really works
– MakeItFun
Mar 28 at 10:40
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387699%2fsearching-name-and-surname-in-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this,
from django.db.models import Value as V
from django.db.models.functions import Concat
text = "John Doe"
Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Reference
Concat
DB fucntion
Django queryset filter after a concatenation of two columns
Django shell output
In [14]: from django.db.models import Value as V
In [15]: from django.db.models.functions import Concat
In [16]: text = "John Doe"
In [17]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[17]: <QuerySet [<Profile: John Doe>]>
In [18]: text = "john"
In [19]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[19]: <QuerySet [<Profile: John Doe>, <Profile: John Smith>]>
In [20]: text="smith"
In [21]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[21]: <QuerySet [<Profile: John Smith>]>
add a comment |
Try this,
from django.db.models import Value as V
from django.db.models.functions import Concat
text = "John Doe"
Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Reference
Concat
DB fucntion
Django queryset filter after a concatenation of two columns
Django shell output
In [14]: from django.db.models import Value as V
In [15]: from django.db.models.functions import Concat
In [16]: text = "John Doe"
In [17]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[17]: <QuerySet [<Profile: John Doe>]>
In [18]: text = "john"
In [19]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[19]: <QuerySet [<Profile: John Doe>, <Profile: John Smith>]>
In [20]: text="smith"
In [21]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[21]: <QuerySet [<Profile: John Smith>]>
add a comment |
Try this,
from django.db.models import Value as V
from django.db.models.functions import Concat
text = "John Doe"
Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Reference
Concat
DB fucntion
Django queryset filter after a concatenation of two columns
Django shell output
In [14]: from django.db.models import Value as V
In [15]: from django.db.models.functions import Concat
In [16]: text = "John Doe"
In [17]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[17]: <QuerySet [<Profile: John Doe>]>
In [18]: text = "john"
In [19]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[19]: <QuerySet [<Profile: John Doe>, <Profile: John Smith>]>
In [20]: text="smith"
In [21]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[21]: <QuerySet [<Profile: John Smith>]>
Try this,
from django.db.models import Value as V
from django.db.models.functions import Concat
text = "John Doe"
Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Reference
Concat
DB fucntion
Django queryset filter after a concatenation of two columns
Django shell output
In [14]: from django.db.models import Value as V
In [15]: from django.db.models.functions import Concat
In [16]: text = "John Doe"
In [17]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[17]: <QuerySet [<Profile: John Doe>]>
In [18]: text = "john"
In [19]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[19]: <QuerySet [<Profile: John Doe>, <Profile: John Smith>]>
In [20]: text="smith"
In [21]: Profile.objects.annotate(full_name=Concat('name', V(' '), 'surname')).filter(full_name__icontains=text)
Out[21]: <QuerySet [<Profile: John Smith>]>
answered Mar 28 at 3:08
JPGJPG
22.3k3 gold badges14 silver badges46 bronze badges
22.3k3 gold badges14 silver badges46 bronze badges
add a comment |
add a comment |
This is a modified copy/paste from this answer.
I tried to come up with various ways, but they all required counting rows after each query which would make it really time consuming. Instead, the best way seem to be to split up the text by spaces and apply filters to it:
def get_profiles(text):
qs = Profile.objects.all()
for term in text.split():
qs = qs.filter( Q(name__icontains = term) | Q(surname__icontains = term))
return qs
If I search "John" it would not return anything...
– MakeItFun
Mar 27 at 23:09
Yes, this logic will filter on both name and surname. Do you want the second parameter (name or lastname) to be excluded from the search if it's omitted, but use both parameters if both are given? I think you need to expand your examples of what you provide and what you expect in your original question to make it a bit more clearer.
– Johan
Mar 27 at 23:13
Yes, sorry, I will edit to add more examples. Basically what I want is something like "join" both name and surname attributes, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
– MakeItFun
Mar 27 at 23:22
1
@MakeItFun I understand, I have updated my answer now. It's now instead a function that applies the filters which you can try
– Johan
Mar 28 at 0:02
It works! Thank you. So 'qs' only store distinct profiles for every iteration? I don't get how it really works
– MakeItFun
Mar 28 at 10:40
|
show 1 more comment
This is a modified copy/paste from this answer.
I tried to come up with various ways, but they all required counting rows after each query which would make it really time consuming. Instead, the best way seem to be to split up the text by spaces and apply filters to it:
def get_profiles(text):
qs = Profile.objects.all()
for term in text.split():
qs = qs.filter( Q(name__icontains = term) | Q(surname__icontains = term))
return qs
If I search "John" it would not return anything...
– MakeItFun
Mar 27 at 23:09
Yes, this logic will filter on both name and surname. Do you want the second parameter (name or lastname) to be excluded from the search if it's omitted, but use both parameters if both are given? I think you need to expand your examples of what you provide and what you expect in your original question to make it a bit more clearer.
– Johan
Mar 27 at 23:13
Yes, sorry, I will edit to add more examples. Basically what I want is something like "join" both name and surname attributes, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
– MakeItFun
Mar 27 at 23:22
1
@MakeItFun I understand, I have updated my answer now. It's now instead a function that applies the filters which you can try
– Johan
Mar 28 at 0:02
It works! Thank you. So 'qs' only store distinct profiles for every iteration? I don't get how it really works
– MakeItFun
Mar 28 at 10:40
|
show 1 more comment
This is a modified copy/paste from this answer.
I tried to come up with various ways, but they all required counting rows after each query which would make it really time consuming. Instead, the best way seem to be to split up the text by spaces and apply filters to it:
def get_profiles(text):
qs = Profile.objects.all()
for term in text.split():
qs = qs.filter( Q(name__icontains = term) | Q(surname__icontains = term))
return qs
This is a modified copy/paste from this answer.
I tried to come up with various ways, but they all required counting rows after each query which would make it really time consuming. Instead, the best way seem to be to split up the text by spaces and apply filters to it:
def get_profiles(text):
qs = Profile.objects.all()
for term in text.split():
qs = qs.filter( Q(name__icontains = term) | Q(surname__icontains = term))
return qs
edited Mar 28 at 0:01
answered Mar 27 at 23:01
JohanJohan
2,6181 gold badge7 silver badges19 bronze badges
2,6181 gold badge7 silver badges19 bronze badges
If I search "John" it would not return anything...
– MakeItFun
Mar 27 at 23:09
Yes, this logic will filter on both name and surname. Do you want the second parameter (name or lastname) to be excluded from the search if it's omitted, but use both parameters if both are given? I think you need to expand your examples of what you provide and what you expect in your original question to make it a bit more clearer.
– Johan
Mar 27 at 23:13
Yes, sorry, I will edit to add more examples. Basically what I want is something like "join" both name and surname attributes, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
– MakeItFun
Mar 27 at 23:22
1
@MakeItFun I understand, I have updated my answer now. It's now instead a function that applies the filters which you can try
– Johan
Mar 28 at 0:02
It works! Thank you. So 'qs' only store distinct profiles for every iteration? I don't get how it really works
– MakeItFun
Mar 28 at 10:40
|
show 1 more comment
If I search "John" it would not return anything...
– MakeItFun
Mar 27 at 23:09
Yes, this logic will filter on both name and surname. Do you want the second parameter (name or lastname) to be excluded from the search if it's omitted, but use both parameters if both are given? I think you need to expand your examples of what you provide and what you expect in your original question to make it a bit more clearer.
– Johan
Mar 27 at 23:13
Yes, sorry, I will edit to add more examples. Basically what I want is something like "join" both name and surname attributes, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
– MakeItFun
Mar 27 at 23:22
1
@MakeItFun I understand, I have updated my answer now. It's now instead a function that applies the filters which you can try
– Johan
Mar 28 at 0:02
It works! Thank you. So 'qs' only store distinct profiles for every iteration? I don't get how it really works
– MakeItFun
Mar 28 at 10:40
If I search "John" it would not return anything...
– MakeItFun
Mar 27 at 23:09
If I search "John" it would not return anything...
– MakeItFun
Mar 27 at 23:09
Yes, this logic will filter on both name and surname. Do you want the second parameter (name or lastname) to be excluded from the search if it's omitted, but use both parameters if both are given? I think you need to expand your examples of what you provide and what you expect in your original question to make it a bit more clearer.
– Johan
Mar 27 at 23:13
Yes, this logic will filter on both name and surname. Do you want the second parameter (name or lastname) to be excluded from the search if it's omitted, but use both parameters if both are given? I think you need to expand your examples of what you provide and what you expect in your original question to make it a bit more clearer.
– Johan
Mar 27 at 23:13
Yes, sorry, I will edit to add more examples. Basically what I want is something like "join" both name and surname attributes, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
– MakeItFun
Mar 27 at 23:22
Yes, sorry, I will edit to add more examples. Basically what I want is something like "join" both name and surname attributes, so when I search "John" it shows me "John Doe" and "John Smith", and when i search "John Doe" it shows me only the "John Doe" profile.
– MakeItFun
Mar 27 at 23:22
1
1
@MakeItFun I understand, I have updated my answer now. It's now instead a function that applies the filters which you can try
– Johan
Mar 28 at 0:02
@MakeItFun I understand, I have updated my answer now. It's now instead a function that applies the filters which you can try
– Johan
Mar 28 at 0:02
It works! Thank you. So 'qs' only store distinct profiles for every iteration? I don't get how it really works
– MakeItFun
Mar 28 at 10:40
It works! Thank you. So 'qs' only store distinct profiles for every iteration? I don't get how it really works
– MakeItFun
Mar 28 at 10:40
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387699%2fsearching-name-and-surname-in-django%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think what you want is an
&
instead of|
– dcg
Mar 27 at 22:56