Django update_or_create defaults with a query object - django

I'm using Django 3.2 and I'm trying to call update_or_create like in the snippet below:
from django.db import models
result = model.objects.update_or_create(
field1="value1",
field2="value2",
defaults={
"field3": "value3",
"field4": ~models.Q(field3="value3"),
},
)
field4 is a boolean. The problem is the returning value in field4 after the method is called isn't a boolean, but a query object:
result[0].field4
<Q: (NOT (AND: ('field3', 'value3')))>
However if I refresh the object, the content is correct:
result[0].refresh_from_db()
result[0].field4
False
I'm sure the field was updated because before I ran the snippet, there was an instance with the same values for field1 and field2 but with the opposite value in field4 (True). My question is: is there a way for update_or_create return a boolean value for field4 without querying the instance again from the DB?
I checked StackOverflow before for similar questions, this was the closest one I found but it still doesn't answer my question.
Edit: I noticed the code above doesn't work when creating an instance (only when updating), but I'm still curious if it's possible to do what I asked in the question above.

Related

Overwrite field in queryset annotate

I have a django model with the fields name (string) and value (integer). Say I need to return a queryset of {name:..., value:...} objects, with each value doubled. What I am trying to do now is:
queryset.annotate(value=F('value') * 2)
However, django tells me that the field value already exists.
I also tried using extra:
queryset.annotate(value_double=F('value') * 2).extra(select={'value': 'value_double'})
but that also does not work since value_double is not a valid field.
Of course, in this case I could use something like queryset.extra(select={'value': 'value * 2'}), but I have some other, more complicated cases involving a lot of functions where I really don't want to write sql, but I'd rather find a solution in django. Again, in all of my cases, annotate works perfectly fine as long as I give the result a new name.
Say your model is XYZ with fields name and val. If you want val to contain val*2 use below queryset
x = XYZ.objects.values('name').annotate(val=F('val')*2)
print(x)
Result
<QuerySet [{'name': abc, 'val': 104},...............]
If you want queryset that return name,val and doubleval. You can use below query for same.
x = XYZ.objects.values('name','val',doubleval=F('val')*2)
print(x)
Result
<QuerySet [{'name': abc, 'val':52,'doubleval': 104},...............]
Hope this help.

How to avoid using `extra` when retrieve data from postgres?

I'm retrieving data from postgres DB with following code:
values = ('foo', 'bar', 'group')
FooBar.objects.order_by('-id').extra(select={'group': "'stackoverflow'"}).values(*values)
The code works fine but I've heard that using extra is not preferable and even django documentations says to “Use this method as a last resort.” So the question is how it's possible to avoid using extra to retrieve data?
You can try with Value() expressions. Basically when you need to represent the value of an integer, boolean, or string within an expression, you can wrap that value within a Value().
from django.db.models import Value, CharField
FooBar.objects.annotate(group=Value('stackoverflow', output_field=CharField())).values('foo', 'bar', 'group').order_by('-id')

Get information from a model using unrelated field

I have these two models:
class A(models.Model):
name=models.CharField(max_length=10)
class D(models.Model):
code=models.IntegerField()
the code field can have a number that exists in model A but it cant be related due to other factors. But what I want know is to list items from A whose value is the same with code
items=D.objects.values('code__name')
would work but since they are not related nor can be related, how can I handle that?
You can use Subquery() expressions in Django 1.11 or newer.
from django.db.models import OuterRef, Subquery
code_subquery = A.objects.filter(id=OuterRef('code'))
qs = D.objects.annotate(code_name=Subquery(code_subquery.values('name')))
The output of qs is a queryset of objects D with an added field code_name.
Footnotes:
It is compiled to a very similar SQL (like the Bear Brown's solution with "extra" method, but without disadvantages of his solution, see there):
SELECT app_d.id, app_d.code,
(SELECT U0.name FROM app_a U0 WHERE U0.id = (app_d.code)) AS code_name
FROM app_d
If a dictionary output is required it can be converted by .values() finally. It can work like a left join i.e. if the pseudo related field allows null (code = models.IntegerField(none=True)) then the objects D are not restricted and the output code_name value could be None. A feature of Subquery is that it returns only one field expression must be eventually repeated for another fields. (That is similar to extra(select={...: "SELECT ..."}), but thanks to object syntax it can be more readable customized than an explicit SQL.)
you can use django extra, replace YOUAPP on your real app name
D.objects.extra(select={'a_name': 'select name from YOUAPP_a where id=code'}).values('a_name')
# Replace YOUAPP^^^^^

Django: order by position ignoring NULL

I have a problem with Django queryset ordering.
My model contains a field named position, a PositiveSmallIntegerField which I'd like to used to order query results.
I use order_by('position'), which works great.
Problem : my position field is nullable (null=True, blank=True), because I don't wan't to specify a position for every 50000 instances of my model. When some instances have a NULL position, order_by returns them in the top of the list: I'd like them to be at the end.
In raw SQL, I used to write things like:
IF(position IS NULL or position='', 1, 0)
(see http://www.shawnolson.net/a/730/mysql-sort-order-with-null.html). Is it possible to get the same result using Django, without writing raw SQL?
You can use the annotate() from django agrregation to do the trick:
items = Item.objects.all().annotate(null_position=Count('position')).order_by('-null_position', 'position')
As of Django 1.8 you can use Coalesce() to convert NULL to 0.
Sample:
import datetime
from django.db.models.functions import Coalesce, Value
from app import models
# Coalesce works by taking the first non-null value. So we give it
# a date far before any non-null values of last_active. Then it will
# naturally sort behind instances of Box with a non-null last_active value.
the_past = datetime.datetime.now() - datetime.timedelta(days=10*365)
boxes = models.Box.objects.all().annotate(
new_last_active=Coalesce(
'last_active', Value(the_past)
)
).order_by('-new_last_active')
It's a shame there are a lot of questions like this on SO that are not marked as duplicate. See (for example) this answer for the native solution for Django 1.11 and newer. Here is a short excerpt:
Added the nulls_first and nulls_last parameters to Expression.asc() and desc() to control the ordering of null values.
Example usage (from comment to that answer):
from django.db.models import F
MyModel.objects.all().order_by(F('price').desc(nulls_last=True))
Credit goes to the original answer author and commenter.
Using extra() as Ignacio said optimizes a lot the end query. In my aplication I've saved more than 500ms (that's a lot for a query) in database processing using extra() instead of annotate()
Here is how it would look like in your case:
items = Item.objects.all().extra(
'select': {
'null_position': 'CASE WHEN {tablename}.position IS NULL THEN 0 ELSE 1 END'
}
).order_by('-null_position', 'position')
{tablename} should be something like {Item's app}_item following django's default tables name.
I found that the syntax in Pablo's answer needed to be updated to the following on my 1.7.1 install:
items = Item.objects.all().extra(select={'null_position': 'CASE WHEN {name of Item's table}.position IS NULL THEN 0 ELSE 1 END'}).order_by('-null_position', 'position')
QuerySet.extra() can be used to inject expressions into the query and order by them.

django - queryset with extra method returns empty, count() says otherwise

Here is a related manager I wrote:
class PortfolioItemManager(models.Manager):
use_for_related_fields = True
def extended(self):
return self.extra(select = {'current_price':'current_price', 'current_value':'current_price*quantity', 'gain':'current_price*quantity - cost'},
tables = ['pm_core_contract', ],
where = ['pm_core_contract.id = pm_core_portfolioitem.contract_id', ]
)
Here are the results that stumps me:
In [10]: PortfolioItem.objects.extended()
Out[10]: []
In [11]: PortfolioItem.objects.extended().count()
Out[11]: 402
Result from count() is correct. What am I missing here?
EDIT: The generated SQL is correct and can be executed against the db directly.
EDIT2: The issue stems from the last 2 select arguments, which feature arithmetic operations.
I think I have just figured out the problem. Thanks go to Alex, whose comment has sparked the idea:
The PortfolioItem model has the properties current_value and current_gain, which I have been trying to replace with calculated SQL fields. It was my mistake to name one of the extra() method's select fields 'current_value' without removing the property, as that led to the model having two fields with the same name. When I did away with that overlapping, everything became OK. Lesson is learned.