Re: Possible bug in interaction between 'block' and 'include' in Django Templates
I got the answer on IRC that this is not a bug as explained in the documentation for include https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#include Suriya On Thursday, September 10, 2015 at 2:55:42 PM UTC+5:30, Suriya Subramanian wrote: > > Hi, > > I think there is a bug in how 'block' and 'include' template tags interact > in the Django Template Language. I have created a Gist showing this issue: > https://gist.github.com/anonymous/2627bb35955db77dbfaa > > The order of the files in the Gist can be a bit confusing. I will explain > what I am trying to do here. I have a base template that defines two > blocks, one block directly, and one block within an included file. A child > template extends the base template and defines both the blocks. However, in > the rendered output the child is able to override only of the blocks. The > child template is unable to override the block defined in the include file. > > My understand is that, a file and block included within a parent template > should be considered a part of the parent. However, that does not seem to > be the case. > > Thanks, > Suriya > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5ee79ae1-9d6f-41de-aa23-369d3104e455%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Possible bug in interaction between 'block' and 'include' in Django Templates
Hi, I think there is a bug in how 'block' and 'include' template tags interact in the Django Template Language. I have created a Gist showing this issue: https://gist.github.com/anonymous/2627bb35955db77dbfaa The order of the files in the Gist can be a bit confusing. I will explain what I am trying to do here. I have a base template that defines two blocks, one block directly, and one block within an included file. A child template extends the base template and defines both the blocks. However, in the rendered output the child is able to override only of the blocks. The child template is unable to override the block defined in the include file. My understand is that, a file and block included within a parent template should be considered a part of the parent. However, that does not seem to be the case. Thanks, Suriya -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e133b1b0-12aa-4496-84ec-f76dab24bc28%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Django ORM Interaction between order_by(), distinct(), and filter()
I found a way to express the second query in the ORM. Person.objects.filter(sex='F', pk__in=tallest_people) Thanks, Suriya On Monday, June 1, 2015 at 12:13:49 PM UTC+5:30, Suriya Subramanian wrote: > > I have a question about how order_by(), distinct(), and filter() interact. > This question is applicable only to the PostgreSQL backend since the > Queryset is constructed by passing filed arguments to distinct(). > https://docs.djangoproject.com/en/1.8/ref/models/querysets/#distinct In > the other backends, distinct() accepts no arguments. > > I'll illustrate my question with an example model. Here's a link to the > code snippet: https://gist.github.com/anonymous/4111b718dbef264fb339 > > from django.db import models > > class Person(models.Model): > SEX_CHOICES = ( > ('M', 'Male'), > ('F', 'Female'), > ) > name = models.CharField(max_length=255) > sex = models.CharField(max_length=255, choices=SEX_CHOICES) > city = models.CharField(max_length=255) > height = models.DecimalField(max_digits=10, decimal_places=2) > > def __unicode__(self): > return self.name > > > I am interested in queries about the tallest person in a city. > Specifically, 1) the tallest female in each city, and 2) the tallest person > in cities where a female is the tallest. I am able to write the first query > using the ORM, but not the second query. > > import django > django.setup() > > from testapp.models import Person > > # Get a list consisting of the tallest female in each city. > tallest_females = Person.objects.filter(sex='F').order_by('city', > '-height').distinct('city').values_list('pk', flat=True) > print tallest_females.query > # This outputs: > # > # SELECT DISTINCT ON ("testapp_person"."city") "testapp_person"."id" > # FROM "testapp_person" > # WHERE "testapp_person"."sex" = F > # ORDER BY "testapp_person"."city" ASC, "testapp_person"."height" DESC > > # Get a list consiting of females who are the tallest (among all people) in > # their respective cities. > # How do I get this list using the ORM? Note that I am having resort to > # filtering results in Python. > tallest_people = Person.objects.order_by('city', > '-height').distinct('city').values_list('pk', flat=True) > females_who_are_tallest_in_their_city = [ p for p in tallest_people if (p in > tallest_females) ] > > # Ideally, I'd like to write: > # Person.objects.order_by('city', > '-height').distinct('city').filter(sex='F').values_list('pk', flat=True) > > > What's a way to compute the results of the second query fully in the > database, without resorting to Python code? > > Thanks, > Suriya > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/43f9e3a1-4211-4bfa-aa8d-292cfe436169%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Django ORM Interaction between order_by(), distinct(), and filter()
I have a question about how order_by(), distinct(), and filter() interact. This question is applicable only to the PostgreSQL backend since the Queryset is constructed by passing filed arguments to distinct(). https://docs.djangoproject.com/en/1.8/ref/models/querysets/#distinct In the other backends, distinct() accepts no arguments. I'll illustrate my question with an example model. Here's a link to the code snippet: https://gist.github.com/anonymous/4111b718dbef264fb339 from django.db import models class Person(models.Model): SEX_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) name = models.CharField(max_length=255) sex = models.CharField(max_length=255, choices=SEX_CHOICES) city = models.CharField(max_length=255) height = models.DecimalField(max_digits=10, decimal_places=2) def __unicode__(self): return self.name I am interested in queries about the tallest person in a city. Specifically, 1) the tallest female in each city, and 2) the tallest person in cities where a female is the tallest. I am able to write the first query using the ORM, but not the second query. import django django.setup() from testapp.models import Person # Get a list consisting of the tallest female in each city. tallest_females = Person.objects.filter(sex='F').order_by('city', '-height').distinct('city').values_list('pk', flat=True) print tallest_females.query # This outputs: # # SELECT DISTINCT ON ("testapp_person"."city") "testapp_person"."id" # FROM "testapp_person" # WHERE "testapp_person"."sex" = F # ORDER BY "testapp_person"."city" ASC, "testapp_person"."height" DESC # Get a list consiting of females who are the tallest (among all people) in # their respective cities. # How do I get this list using the ORM? Note that I am having resort to # filtering results in Python. tallest_people = Person.objects.order_by('city', '-height').distinct('city').values_list('pk', flat=True) females_who_are_tallest_in_their_city = [ p for p in tallest_people if (p in tallest_females) ] # Ideally, I'd like to write: # Person.objects.order_by('city', '-height').distinct('city').filter(sex='F').values_list('pk', flat=True) What's a way to compute the results of the second query fully in the database, without resorting to Python code? Thanks, Suriya -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/844b90e2-d312-48b9-8239-150ae821d3e3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Leveraging the ORM for very complex queries
Thank you Russ and Michael. For the reference of other users and to get feedback, here's what I ended up doing: https://gist.github.com/anonymous/154512b369fe7e273631 import itertools from django.db import connection """ Combine SQL and ORM. sql = 'SELECT * FROM ( {} ) T1 NATURAL FULL OUTER JOIN ( {} ) T2; ' querysets = [ MyModel1.objects.filter(...), MyModel2.objects.filter(...), ] cursor = execute_orm_sql_query(sql, querysets) """ def sqlparams_from_queryset(qs): """ Returns the SQL query and params of a queryset. """ (qstr, params) = qs.query.sql_with_params() return (qstr, params) def execute_orm_sql_query(sqltemplate, querysets): """ Combine the Raw SQL with Django ORM query output and execute the query. Returns the database cursor. """ sqlparams = [ sqlparams_from_queryset(qs) for qs in querysets ] (sqls, params) = zip(*sqlparams) # zip(*...) is the inverse of zip params = tuple(itertools.chain.from_iterable(params)) cursor = connection.cursor() cursor.execute(sqltemplate.format(*sqls), params) return cursor Thanks, Suriya On Monday, May 4, 2015 at 10:36:11 PM UTC+5:30, Michael Manfre wrote: > > The internals of the ORM are deemed a private API and have undergone > significant changes in the past without being constrained by the two > release deprecation cycle. As some one who was forced to write query > construction code based upon Django internals, my recommendation is to only > do that if you have no other choice. You will eventually get hit by a > change in a subsequent release of Django that forces you to remain on a no > longer supported version of Django while you update the hack for the new > version of Django or (better) rewrite it to use supported APIs. > > Regards, > Michael Manfre > > On Monday, May 4, 2015 at 7:17:49 AM UTC-4, Suriya Subramanian wrote: >> >> Hi Russ, >> >> Thank you for your answer. I am aware of raw. However, that's now what I >> am looking for. Let me give a few examples of queries that I would like to >> write: >> >> 1) Window functions over an ORM query: >> SELECT "date", SUM("weight__sum") OVER (ORDER BY "date") >> FROM >> ( >> MyModel.objects.values('date').annotate(Sum('weight')).query >> ) T >> >> 2) Join of two ORM queries >> ( complex ORM query ) NATURAL JOIN ( complex ORM query ) >> >> Writing query 1 and 2 fully in SQL is painful, since they leverage a lot >> of Python and ORM logic (for example: parsing URL arguments and filtering). >> Composing SQL and ORM, even if it means having to deal with the guts of >> SQLCompiler.as_sql() seems to be a decent solution for me. I am asking if >> there any best practices to follow or gotchas to watch out for. >> >> Thanks, >> Suriya >> >> On Monday, May 4, 2015 at 5:35:50 AM UTC+5:30, Russell Keith-Magee wrote: >>> >>> Hi Suriya, >>> >>> It sounds like you're looking for raw SQL queries: >>> >>> >>> https://docs.djangoproject.com/en/1.8/topics/db/sql/#performing-raw-queries >>> >>> This allows you to issue a SQL query in SQL, rather than trying to bend >>> the ORM to meet some complex query requirement. >>> >>> You can't compose a raw query like a normal Django ORM query (e.g., you >>> can't add a filter clause to an raw query), but a raw query object behaves >>> exactly like queryset when it returns results - it is iterable, it returns >>> full Django objects, and so on. >>> >>> Yours, >>> Russ Magee %-) >>> >>> On Sun, May 3, 2015 at 9:22 AM, Suriya Subramanian >>> wrote: >>> >>>> Hello, >>>> >>>> I have to write some complex SQL queries that I am unable to express >>>> using the ORM. I construct these complex queries by writing a few simple >>>> ORM queries, getting the SQL using QuerySet.query and combining them with >>>> various SQL operators manually. These hand-crafted queries are not very >>>> flexible because it is very easy to modify the final SQL. >>>> >>>> My question: Is there a way to programmatically construct the complex >>>> queries. I see that I can get the generated SQL and parameters by invoking >>>> SQLCompiler.as_sql(). Can I invoke as_sql() on the individual query sets >>>> and then construct the complex query? What are some gotchas that I need to >>>> watch out f
Re: Leveraging the ORM for very complex queries
Hi Russ, Thank you for your answer. I am aware of raw. However, that's now what I am looking for. Let me give a few examples of queries that I would like to write: 1) Window functions over an ORM query: SELECT "date", SUM("weight__sum") OVER (ORDER BY "date") FROM ( MyModel.objects.values('date').annotate(Sum('weight')).query ) T 2) Join of two ORM queries ( complex ORM query ) NATURAL JOIN ( complex ORM query ) Writing query 1 and 2 fully in SQL is painful, since they leverage a lot of Python and ORM logic (for example: parsing URL arguments and filtering). Composing SQL and ORM, even if it means having to deal with the guts of SQLCompiler.as_sql() seems to be a decent solution for me. I am asking if there any best practices to follow or gotchas to watch out for. Thanks, Suriya On Monday, May 4, 2015 at 5:35:50 AM UTC+5:30, Russell Keith-Magee wrote: > > Hi Suriya, > > It sounds like you're looking for raw SQL queries: > > https://docs.djangoproject.com/en/1.8/topics/db/sql/#performing-raw-queries > > This allows you to issue a SQL query in SQL, rather than trying to bend > the ORM to meet some complex query requirement. > > You can't compose a raw query like a normal Django ORM query (e.g., you > can't add a filter clause to an raw query), but a raw query object behaves > exactly like queryset when it returns results - it is iterable, it returns > full Django objects, and so on. > > Yours, > Russ Magee %-) > > On Sun, May 3, 2015 at 9:22 AM, Suriya Subramanian > wrote: > >> Hello, >> >> I have to write some complex SQL queries that I am unable to express >> using the ORM. I construct these complex queries by writing a few simple >> ORM queries, getting the SQL using QuerySet.query and combining them with >> various SQL operators manually. These hand-crafted queries are not very >> flexible because it is very easy to modify the final SQL. >> >> My question: Is there a way to programmatically construct the complex >> queries. I see that I can get the generated SQL and parameters by invoking >> SQLCompiler.as_sql(). Can I invoke as_sql() on the individual query sets >> and then construct the complex query? What are some gotchas that I need to >> watch out for? >> >> Thanks, >> Suriya Subramanian >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users...@googlegroups.com . >> To post to this group, send email to django...@googlegroups.com >> . >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/b4465893-6d08-4ed3-babd-1f6e0f0f52ab%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/b4465893-6d08-4ed3-babd-1f6e0f0f52ab%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fcb71fe4-cf87-40f6-ad5c-4792e9261754%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Leveraging the ORM for very complex queries
Hello, I have to write some complex SQL queries that I am unable to express using the ORM. I construct these complex queries by writing a few simple ORM queries, getting the SQL using QuerySet.query and combining them with various SQL operators manually. These hand-crafted queries are not very flexible because it is very easy to modify the final SQL. My question: Is there a way to programmatically construct the complex queries. I see that I can get the generated SQL and parameters by invoking SQLCompiler.as_sql(). Can I invoke as_sql() on the individual query sets and then construct the complex query? What are some gotchas that I need to watch out for? Thanks, Suriya Subramanian -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b4465893-6d08-4ed3-babd-1f6e0f0f52ab%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.