Conditionally change field value for lookup in Django ORM
up vote
0
down vote
favorite
I'm trying to conditionally change field value during lookup - I have some specific order in mind and I do not want to overwrite field value, just to sort it my way. Let's say, I have classProduct
and every class object has product_code
field. Now I want to get less than or equal, but it's not trivial - product_code
is for most of the time like this A01
, B02
and so on and Django lookup lte
would work. But now I have fields 0001C01
which I would like to be the biggest value. So during lookup I would like to add 0000
at the begining of every string that does not have this prefix, so it would look like 0001C01
, 0000B02
, 0000A01
.
python django django-orm
add a comment |
up vote
0
down vote
favorite
I'm trying to conditionally change field value during lookup - I have some specific order in mind and I do not want to overwrite field value, just to sort it my way. Let's say, I have classProduct
and every class object has product_code
field. Now I want to get less than or equal, but it's not trivial - product_code
is for most of the time like this A01
, B02
and so on and Django lookup lte
would work. But now I have fields 0001C01
which I would like to be the biggest value. So during lookup I would like to add 0000
at the begining of every string that does not have this prefix, so it would look like 0001C01
, 0000B02
, 0000A01
.
python django django-orm
You can annotate your queryset using database functions like Concat to get a new field to use in your order_by statement
– ivissani
Nov 20 at 0:10
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to conditionally change field value during lookup - I have some specific order in mind and I do not want to overwrite field value, just to sort it my way. Let's say, I have classProduct
and every class object has product_code
field. Now I want to get less than or equal, but it's not trivial - product_code
is for most of the time like this A01
, B02
and so on and Django lookup lte
would work. But now I have fields 0001C01
which I would like to be the biggest value. So during lookup I would like to add 0000
at the begining of every string that does not have this prefix, so it would look like 0001C01
, 0000B02
, 0000A01
.
python django django-orm
I'm trying to conditionally change field value during lookup - I have some specific order in mind and I do not want to overwrite field value, just to sort it my way. Let's say, I have classProduct
and every class object has product_code
field. Now I want to get less than or equal, but it's not trivial - product_code
is for most of the time like this A01
, B02
and so on and Django lookup lte
would work. But now I have fields 0001C01
which I would like to be the biggest value. So during lookup I would like to add 0000
at the begining of every string that does not have this prefix, so it would look like 0001C01
, 0000B02
, 0000A01
.
python django django-orm
python django django-orm
asked Nov 19 at 19:16
PotatoBox
1191221
1191221
You can annotate your queryset using database functions like Concat to get a new field to use in your order_by statement
– ivissani
Nov 20 at 0:10
add a comment |
You can annotate your queryset using database functions like Concat to get a new field to use in your order_by statement
– ivissani
Nov 20 at 0:10
You can annotate your queryset using database functions like Concat to get a new field to use in your order_by statement
– ivissani
Nov 20 at 0:10
You can annotate your queryset using database functions like Concat to get a new field to use in your order_by statement
– ivissani
Nov 20 at 0:10
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
This sounds fairly straightforward. Fetch the desired Product
objects, and for each one, prepend 0000
to product_code
if it doesn't start with that string.
products = Product.objects.filter(some_query_expression)
for product in products:
if not product.product_code.startswith('0000'):
product.product_code = '0000' + product.product_code
It's not clear if you want to save this value back to the database, or just use it for temporary comparisons. If you do want to save it, call product.save()
.
I do not want to overwrite the original value nor change it, just to conditionally modify it for the time of lookup.
– PotatoBox
Nov 19 at 19:31
add a comment |
up vote
0
down vote
You can conditionally annotate your queryset in order to get a new field that has de desired value and then use this field in your filter
or order_by
clause. For example you could do the following:
from django.db.models import CharField, Value as V, F, Q, Case, When
from django.db.models.functions import Concat
Product.objects.annotate(
new_product_code=Case(
When(product_code__iregex=r'^[A-Z]+.*', # If it starts with letters
then=Concat(V('0000'), 'product_code', output_field=CharField()) # Then prepend four 0's
),
default=F('product_code') # Else, the original value
)
).filter(new_product_code__lte='whatever you like') # Now filter by using your new value
Relevant parts of the documentation are conditional expressions, database functions and QuerySet API reference
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This sounds fairly straightforward. Fetch the desired Product
objects, and for each one, prepend 0000
to product_code
if it doesn't start with that string.
products = Product.objects.filter(some_query_expression)
for product in products:
if not product.product_code.startswith('0000'):
product.product_code = '0000' + product.product_code
It's not clear if you want to save this value back to the database, or just use it for temporary comparisons. If you do want to save it, call product.save()
.
I do not want to overwrite the original value nor change it, just to conditionally modify it for the time of lookup.
– PotatoBox
Nov 19 at 19:31
add a comment |
up vote
1
down vote
This sounds fairly straightforward. Fetch the desired Product
objects, and for each one, prepend 0000
to product_code
if it doesn't start with that string.
products = Product.objects.filter(some_query_expression)
for product in products:
if not product.product_code.startswith('0000'):
product.product_code = '0000' + product.product_code
It's not clear if you want to save this value back to the database, or just use it for temporary comparisons. If you do want to save it, call product.save()
.
I do not want to overwrite the original value nor change it, just to conditionally modify it for the time of lookup.
– PotatoBox
Nov 19 at 19:31
add a comment |
up vote
1
down vote
up vote
1
down vote
This sounds fairly straightforward. Fetch the desired Product
objects, and for each one, prepend 0000
to product_code
if it doesn't start with that string.
products = Product.objects.filter(some_query_expression)
for product in products:
if not product.product_code.startswith('0000'):
product.product_code = '0000' + product.product_code
It's not clear if you want to save this value back to the database, or just use it for temporary comparisons. If you do want to save it, call product.save()
.
This sounds fairly straightforward. Fetch the desired Product
objects, and for each one, prepend 0000
to product_code
if it doesn't start with that string.
products = Product.objects.filter(some_query_expression)
for product in products:
if not product.product_code.startswith('0000'):
product.product_code = '0000' + product.product_code
It's not clear if you want to save this value back to the database, or just use it for temporary comparisons. If you do want to save it, call product.save()
.
answered Nov 19 at 19:26
John Gordon
9,27551728
9,27551728
I do not want to overwrite the original value nor change it, just to conditionally modify it for the time of lookup.
– PotatoBox
Nov 19 at 19:31
add a comment |
I do not want to overwrite the original value nor change it, just to conditionally modify it for the time of lookup.
– PotatoBox
Nov 19 at 19:31
I do not want to overwrite the original value nor change it, just to conditionally modify it for the time of lookup.
– PotatoBox
Nov 19 at 19:31
I do not want to overwrite the original value nor change it, just to conditionally modify it for the time of lookup.
– PotatoBox
Nov 19 at 19:31
add a comment |
up vote
0
down vote
You can conditionally annotate your queryset in order to get a new field that has de desired value and then use this field in your filter
or order_by
clause. For example you could do the following:
from django.db.models import CharField, Value as V, F, Q, Case, When
from django.db.models.functions import Concat
Product.objects.annotate(
new_product_code=Case(
When(product_code__iregex=r'^[A-Z]+.*', # If it starts with letters
then=Concat(V('0000'), 'product_code', output_field=CharField()) # Then prepend four 0's
),
default=F('product_code') # Else, the original value
)
).filter(new_product_code__lte='whatever you like') # Now filter by using your new value
Relevant parts of the documentation are conditional expressions, database functions and QuerySet API reference
add a comment |
up vote
0
down vote
You can conditionally annotate your queryset in order to get a new field that has de desired value and then use this field in your filter
or order_by
clause. For example you could do the following:
from django.db.models import CharField, Value as V, F, Q, Case, When
from django.db.models.functions import Concat
Product.objects.annotate(
new_product_code=Case(
When(product_code__iregex=r'^[A-Z]+.*', # If it starts with letters
then=Concat(V('0000'), 'product_code', output_field=CharField()) # Then prepend four 0's
),
default=F('product_code') # Else, the original value
)
).filter(new_product_code__lte='whatever you like') # Now filter by using your new value
Relevant parts of the documentation are conditional expressions, database functions and QuerySet API reference
add a comment |
up vote
0
down vote
up vote
0
down vote
You can conditionally annotate your queryset in order to get a new field that has de desired value and then use this field in your filter
or order_by
clause. For example you could do the following:
from django.db.models import CharField, Value as V, F, Q, Case, When
from django.db.models.functions import Concat
Product.objects.annotate(
new_product_code=Case(
When(product_code__iregex=r'^[A-Z]+.*', # If it starts with letters
then=Concat(V('0000'), 'product_code', output_field=CharField()) # Then prepend four 0's
),
default=F('product_code') # Else, the original value
)
).filter(new_product_code__lte='whatever you like') # Now filter by using your new value
Relevant parts of the documentation are conditional expressions, database functions and QuerySet API reference
You can conditionally annotate your queryset in order to get a new field that has de desired value and then use this field in your filter
or order_by
clause. For example you could do the following:
from django.db.models import CharField, Value as V, F, Q, Case, When
from django.db.models.functions import Concat
Product.objects.annotate(
new_product_code=Case(
When(product_code__iregex=r'^[A-Z]+.*', # If it starts with letters
then=Concat(V('0000'), 'product_code', output_field=CharField()) # Then prepend four 0's
),
default=F('product_code') # Else, the original value
)
).filter(new_product_code__lte='whatever you like') # Now filter by using your new value
Relevant parts of the documentation are conditional expressions, database functions and QuerySet API reference
answered Nov 20 at 0:34
ivissani
35125
35125
add a comment |
add a 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53381214%2fconditionally-change-field-value-for-lookup-in-django-orm%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
You can annotate your queryset using database functions like Concat to get a new field to use in your order_by statement
– ivissani
Nov 20 at 0:10