django-queryset

Django Aggregation - Expression contains mixed types. You must set output_field

末鹿安然 提交于 2019-12-04 09:11:00
问题 I'm trying to achive an Aggregation Query and that's my code: TicketGroup.objects.filter(event=event).aggregate( total_group=Sum(F('total_sold')*F('final_price'))) I have 'total_sold' and 'final_price' in TicketGroup object and all what I want to do is sum and multiply values to get the total sold of all TicketGroups together. All I get is this error: Expression contains mixed types. You must set output_field What I am doing wrong, since I'm calling 'total_group' as my output field? Thanks!

Django ORM: Retrieving posts and latest comments without performing N+1 queries

五迷三道 提交于 2019-12-04 08:44:51
I have a very standard, basic social application -- with status updates (i.e., posts), and multiple comments per post. Given the following simplified models, is it possible, using Django's ORM, to efficiently retrieve all posts and the latest two comments associated with each post, without performing N+1 queries? (That is, without performing a separate query to get the latest comments for each post on the page.) class Post(models.Model): title = models.CharField(max_length=255) text = models.TextField() class Comment(models.Model): text = models.TextField() post = models.ForeignKey(Post,

Django pagination is repeating results

无人久伴 提交于 2019-12-04 05:16:29
I have this weird pagination bug in Django: using object_list as a return of a view, but passing a "paginate_by" argument to it, it's repeating some of the results; Otherwise, if I remove the argument or set as "paginate_by=None", the results are correct. If using pagination, the quantity of results is maintained at a total, so, because there are repeated results, the last results are left out of the list, so they don't appear in the template. Any ideas of what might be happening? Thanks! I had this problem also, but found a solution. The problem was that i sorted the dataset by date. When i

Newbie: Django : Adding calculated results to Queryset before passing to template

痴心易碎 提交于 2019-12-04 04:16:04
Its day two of my new life with Django, please excuse the simplicity of my question. I have an existing DB table(read-only access) that I have successfully displayed the contents of on a webpage using urls, views, models and all that good stuff. The challenge I have is the table does not contain all the information I need to display. The table contains test results with the columns, sampletime, samplevalue, sampleresult. I need to display different data based on what I calculate from those columns. My end goal is to display this info as a time series graph using flotr . For now Id be happy to

Django: How to annotate M2M or OneToMany fields using a SubQuery?

狂风中的少年 提交于 2019-12-04 04:12:47
问题 I have Order objects and OrderOperation objects that represent an action on a Order (creation, modification, cancellation). Conceptually, an order has 1 to many order operations. Each time there is an operation on the order, the total is computed in this operation. Which means when I need to find an attribute of an order, I just get the last order operation attribute instead, using a Subquery. The simplified code class OrderOperation(models.Model): order = models.ForeignKey(Order) total =

Django query for many-to-many subset containment

跟風遠走 提交于 2019-12-04 03:20:30
Is there a way to query for subset or superset containment with many-to-many fields? Suppose each Person has a list of birds they want to see, and each Aviary houses a list of birds. How can I make a query to find, for a given Person instance, which Aviaries have every bird on the person's list? And similarly, for a given Person instance, how do I find which Aviaries have only birds on the person's list (but not necessarily all of them). Here are my Django 1.5 models: class Bird(models.Model): name = models.CharField(max_length=255, unique=True) class Aviary(models.Model): name = models

Django conditional annotation

故事扮演 提交于 2019-12-04 03:17:35
I'm surprised that this question apparently doesn't yet exist. If it does, please help me find it. I want to use annotate (Count) and order_by, but I don't want to count every instance of a related object, only those that meet a certain criteron. To wit, that I might list swallows by the number of green coconuts they have carried: swallow.objects.annotate(num_coconuts=Count('coconuts_carried__husk__color = "green"').order_by('num_coconuts') This should be the right way. swallow.objects.filter( coconuts_carried__husk__color="green" ).annotate( num_coconuts=Count('coconuts_carried') ).order_by(

Django: Filtering datetime field by *only* the year value?

余生长醉 提交于 2019-12-04 02:33:50
I'm trying to spit out a django page which lists all entries by the year they were created. So, for example: 2010: Note 4 Note 5 Note 6 2009: Note 1 Note 2 Note 3 It's proving more difficult than I would have expected. The model from which the data comes is below: class Note(models.Model): business = models.ForeignKey(Business) note = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) class Meta: db_table = 'client_note' @property def note_year(self): return self.created.strftime('%Y') def __unicode__(self): return '%s' % self

Django: using values() and get_FOO_display()?

陌路散爱 提交于 2019-12-04 00:40:25
问题 I'm trying to improve some existing code which originally took 3 minutes to prepare a large dataTable (then returned by Ajax). The old code iterated over a large querySet, gathering information from a variety of related objects. From what I've read, and from monitoring the SQL log, iterating over querysets is generally a bad idea, because SQL is executed for each item. Instead, I've been using values to gather information in a single SQL statement, then iterating through that. Using this

Django Postgresql ArrayField aggregation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 00:36:05
问题 In my Django application, using Postgresql, I have a model with an ArrayField of CharFields. I would like to know if there's a DB way to aggregate and get a list of all the strings in the table. For example: ['dog', 'cat'] ['dog'] ['cat'] would yield ['dog', 'cat'] I know how to do that in Python but would like to find out a way to aggregate this on the DB level. Using Django 1.8.4 回答1: In PostgreSQL you can do the following: SELECT DISTINCT UNNEST(array_column) FROM the_table; So if your