if this possible to rewrite this PostgreSQL query to Django ORM with no raw SQL, i tried it by myself but i haven't succeeded.
Here is Fund's model
class Fund(models.Model):
name = models.CharField(max_length=320, unique=True)
fund_manager = models.CharField()
..............
And here is NAV's model
class NAV(models.Model):
price = models.DecimalField(max_digits=9, verbose_name=_('NAV'))
date = models.DateField(verbose_name=_('date'))
fund = models.ForeignKey('Fund', related_name='navs')
created_date = models.DateTimeField(default=now)
And here is sql query that works and needs to be rewritten to Django ORM
select * from finance_fund ORDER BY(
(select price from finance_nav where date='2016-11-08' and fund_id=finance_fund.id) /
(select price from finance_nav where date='2016-11-07' and fund_id=finance_fund.id)
)
So basically i need to order by division result of two simple queries of backward relations of Foreign key
Thanks in advance !
Only with raw SQL. Try something like:
qs = Fund.objects.annotate(x=RawSQL("""
(select price from finance_nav where finance_fund.id=fund_id and date=%s limit 1)
/ (select price from finance_nav where finance_fund.id=fund_id and date=%s limit 1)
""",('2016-11-08', '2016-11-07'))).order_by('x')
Related
I have the following model
class Lances(models.Model):
licitante = models.CharField(...)
valor_prop = models.DecimalField(...)
momento = models.DateTimeField(...)
flag = models.CharField(...)
situacao = models.CharField(...)
item = models.ForeignKey(Items, on_delete=models.CASCADE)
supose that my database have this state
What I want is a query with django orm that returns the minimmum value of each licitante ie. ("Katálysis Comércio, Locação e Serviços em Equipamentos de Laboratório LTDA", 116000.0000,), ("AGILENT TECHNOLOGIES BRASIL LTDA", 117000.0000), ("PERKINELMER DO BRASIL LTDA.", 128500.0000)
I have tried ordering by valor_prop and then select distinct licitante.
queryset = Lances.objects.filter(item=355, situacao='').order_by('valor_prop').distinct('licitante').values_list('licitante', 'valor_prop')
But no luck.
Is there a way to return the minimum value of each licitante in a single query?
You can use GROUP BY for this:
from django.db.models import Min
Lances.objects.values('licitante').annotate(min_valor_prop=Min('valor_prop')).values_list('licitante', 'min_valor_prop')
More information can be found in documentation
I have 3 models (A,B,C):
class A(models.Model):
url = models.URLField()
uuid = models.UUIDField()
name = models.CharField(max_length=400)
id = models.IntegerField()
class B(models.Model):
user = models.ForeignKey(C, to_field='user_id',
on_delete=models.PROTECT,)
uuid = models.ForeignKey(A, to_field='uuid',
on_delete=models.PROTECT,)
and I want to perform the following SQL query using the Django ORM:
SELECT A.id, COUNT(A.id), COUNT(foo.user)
FROM A
LEFT JOIN (SELECT uuid, user FROM B where user = '<a_specific_user_id>') as foo
ON A.uuid = foo.uuid_id
WHERE name = '{}'
GROUP by 1
HAVING COUNT(A.id)> 1 AND COUNT(A.id)>COUNT(foo.user)
My problem is mainly with LEFT JOIN. I know I can form a LEFT JOIN by checking for the existence of null fields on table B:
A.objects.filter(name='{}', b__isnull=True).values('id', 'name')
but how can I LEFT JOIN on the specific sub-query I want?
I tried using Subquery() but it seems to populate the final WHERE statement and not pass my custom sub-query in the LEFT JOIN.
For anyone stumbling upon this in the future. I directly contacted the Django irc channel and it's confirmed that, as of now, it's not possible to include a custom subquery in a LEFT JOIN clause, using the Django ORM.
I am trying to write this raw SQL query,
info_model = list(InfoModel.objects.raw('SELECT *,
max(date),
count(postid) AS freq,
count(DISTINCT author) AS contributors FROM
crudapp_infomodel GROUP BY topicid ORDER BY date DESC'))
as a django query. The following attempt does not work as I can't get related fields for 'author' and 'post'.
info_model = InfoModel.objects.values('topic')
.annotate( max=Max('date'),
freq=Count('postid'),
contributors=Count('author',
distinct=True))
.order_by('-max')
With raw SQL I can use SELECT * but how can I do the equivalent with the Django query?
The model is,
class InfoModel(models.Model):
topicid = models.IntegerField(default=0)
postid = models.IntegerField(default=0)
author = models.CharField(max_length=30)
post = models.CharField(max_length=30)
date = models.DateTimeField('date published')
I did previously post this problem here Django Using order_by with .annotate() and getting related field
I guess you want to order by the maximum date so:
InfoModel.objects.values('topic')
.annotate(
max=Max('date'), freq=Count('postid'),
contributors=Count('author', distinct=True))
.order_by('max')
The following view amalgamates two queries to solve the problem,
def info(request):
info_model = InfoModel.objects.values('topic')
.annotate( max=Max('date'),
freq=Count('postid'),
contributors=Count('author', distinct=True))
.order_by('-max')
info2 = InfoModel.objects.all()
columnlist = []
for item in info2:
columnlist.append([item])
for item in info_model:
for i in range(len(columnlist)):
if item['max'] == columnlist[i][0].date:
item['author'] = columnlist[i][0].author
item['post'] = columnlist[i][0].post
return render(request, 'info.html', {'info_model': info_model})
Trying to run a complicated query in Django over Postgresql.
These are my models:
class Link(models.Model):
short_key = models.CharField(primary_key=True, max_length=8, unique=True, blank=True)
long_url = models.CharField(max_length=150)
class Stats_links_ads(models.Model):
link_id = models.ForeignKey(Link, related_name='link_viewed', primary_key=True)
ad_id = models.ForeignKey(Ad, related_name='ad_viewed')
views = models.PositiveIntegerField()
clicks = models.PositiveIntegerField()
I want to run using the Django ORM a query which will translate into something like so:
select a.link_id, sum(a.clicks), sum (a.views), (select long_url from links_link b where b.short_key = a.link_id_id)
from links_stats_links_ads a
group by a.link_id_id;
If i exclude the long_url field that I need I can run this code and it will work:
Stats_links_Ads.objects.all().values('link_id').annotate(Sum('views'), Sum('clicks'))
I don't know how to add the subquery in the select statement.
Thanks
You can see the raw sql behind your queries using the query attribute of Queryset.
For example, look at the sql behind my first answer using select_related, it's clear the generated sql doesn't behave as expected and accessing the long_url will result in additional queries.
Take 2
You can follow relationships using double underscore notation like this
qs = Stats_links_ads.objects
.values('link_id', 'link_id__long_url')
.annotate(Sum('views'), Sum('clicks'))
str(qs.query)
'SELECT
"stackoverflow_stats_links_ads"."link_id_id",
"stackoverflow_link"."long_url",
SUM("stackoverflow_stats_links_ads"."clicks") AS "clicks__sum",
SUM("stackoverflow_stats_links_ads"."views") AS "views__sum"
FROM "stackoverflow_stats_links_ads"
INNER JOIN "stackoverflow_link"
ON ("stackoverflow_stats_links_ads"."link_id_id" = "stackoverflow_link"."short_key")
GROUP BY
"stackoverflow_stats_links_ads"."link_id_id",
"stackoverflow_link"."long_url"'
I'm not working with any data, so I haven't verified it, but the sql looks right.
Take 1
Does not work
Can't you use .select_related? [docs]
qs = Stats_links_Ads.objects.select_related('link')
.values('link_id').annotate(Sum('views'), Sum('clicks'))
str(qs.query)
'SELECT
"stackoverflow_stats_links_ads"."link_id_id",
SUM("stackoverflow_stats_links_ads"."clicks") AS "clicks__sum",
SUM("stackoverflow_stats_links_ads"."views") AS "views__sum"
FROM "stackoverflow_stats_links_ads"
GROUP BY "stackoverflow_stats_links_ads"."link_id_id"'
I have the following models:
class ApiUser(models.Model):
apikey = models.CharField(max_length=32, unique=True)
class ExtMethodCall(models.Model):
apiuser = models.ForeignKey(ApiUser)
method = models.CharField(max_length=100) #method name
units = models.PositiveIntegerField() #how many units method call cost
created_dt = models.DateField(auto_now_add=True)
For report, i need to get all users who made any call today and total cost of all calls for each user.
In SQL, that would be something like:
SELECT apiuser.*, q1.total_cost
FROM apiuser INNER JOIN (
SELECT apiuser_id, sum(units) as total_cost
FROM extmethodcall
WHERE create_dt = curdate()
GROUP by apiuser_id
) USING apiuser_id
So far, i have found the following solution:
models.ExtMethodCall.objects.filter(created_dt=datetime.date.today()).values('apiuser').annotate(Sum('units'))
which returns me apiuser_id and units__sum.
Is there any more intelligent solution?
Is there any more intelligent solution?
No this is the most natural solution, Django ORM will "tanslate"
.annotate(Sum('units'))
into SQL
SELECT ... sum(units) as units__sum