django-queryset

Django queryset get distinct column values with respect to other column

拜拜、爱过 提交于 2019-12-11 06:55:22
问题 I am using django orm and I am trying to get all the values of a column, but only if a different column is unique with respect to it. Its hard to explain, so here is an example: q | a | 1 w | s | 2 e | a | 3 q | a | 4 w | s | 5 e | a | 6 I would like to get all the values that are in column 2 but if they also have the same value in column 1 don't take duplicates. So in this case I want to get [a,s,a]. (column 3 only serves to show why these duplicates don't get merged in the first place).

Django intersect count annotate for sorting

情到浓时终转凉″ 提交于 2019-12-11 06:29:09
问题 In this question, we were given a solution to sort a query based on the intersection of two many to many fields. While it's a great answer, the restriction is that you have to filter first. Say I have the same two models. Is there a way to annotate all results by count of intersection, so that I can display all Questions regardless of location, but still sorted by location? class Location(models.Model): name = models.CharField(max_length=100) class Profile(models.Model): locations_of_interest

incremental count and calculation in django model

限于喜欢 提交于 2019-12-11 06:28:36
问题 Let's say my models are like this: class Publisher(models.Model): name = models.CharField(max_length=30) code = models.SmallIntegerField(unique=True) class Book(models.Model): date = models.DateField(auto_now_add=True) publisher = models.ForeignKey(Publisher) hardback = models.BooleanField() large_print = models.BooleanField() For a given date range, I want to be able to output a CSV which has total number of books per publisher, and percentage of each boolean field. Eg: Publisher-code Total

Django - Select nested collection Avg with group by in one to many relationship

喜你入骨 提交于 2019-12-11 05:31:17
问题 I have the following models, and what I would like is to select a collection of businesses, each of them with a collection of AVG(rate) based on group by review_question_id. Here are the necessary models: class ReviewQuestion(models.Model): """Represents a question to be given in a business type """ business_type = models.ForeignKey(BusinessType, on_delete=models.CASCADE) question_text = models.CharField(max_length=100) class Business(models.Model): """Values for a specific business, based on

Providing a LIMIT param in Django query without taking a slice of a QuerySet

廉价感情. 提交于 2019-12-11 04:45:23
问题 I have a utility function in my program for searching for entities. It takes a max_count parameter. It returns a QuerySet. I would like this function to limit the max number of entries. The standard way would be to take a slice out of my QuerySet: return results[:max_count] My problem is that the views which utilize this function sort in various ways by using .order_by() . This causes exceptions as re-ordering is not allowed after taking a slice. Is it possible to force a "LIMIT 1000" into my

Django combine foreign keys in a single queryset

核能气质少年 提交于 2019-12-11 04:35:04
问题 I have a model in a Django application that is being referenced by multiple other models as a ForeignKey. What I am looking for is for a way to create a single queryset for all objects of this class that are being referenced as ForeignKey by the rest of the classes based on some criteria. I am not even sure if this is possible, but I thought about asking anyway. class Person(models.Model): pass class Book(models.Model): year_published = models.PositiveIntegerField() author = models.ForeignKey

Django Filter by latest related object

让人想犯罪 __ 提交于 2019-12-11 04:27:43
问题 I've setup a system to track moderation tasks for various objects in the database. These are linked via a Generic Foreign Key relation. Given a ObjectModerationState Object I must determine its state from the most recent StateTransisition object. The solution must lend it self to be used in a SimpleListFilter for sorting on list_display using list_filter. e.g. Given States - Queued - Completed - Verify I should be able to filter ObjectModerationState Objects, based on the latest state value

Django QuerySet API: How do I join iexact and icontains?

情到浓时终转凉″ 提交于 2019-12-11 04:17:36
问题 I have this join: lawyers = Lawyer.objects.filter(last__iexact=last_name).filter(first__icontains=first_name) This is the site If you try Last Name: Abbas and First Name: Amr it tells you that amr abbas has 1 schoolmates. But if you try First name only it says that there are no lawyers in the database called amr (obviously there is). If I change (last__iexact=last_name) to (last__icontains=last_name) then leaving Last Name blank works fine and amr is found. But with last__icontains=last_name

Django 1.8 conditional annotation results in INNER JOIN instead of LEFT OUTER JOIN

那年仲夏 提交于 2019-12-11 04:05:11
问题 The models: class Bar(GenericModel): ... class Foo(GenericModel): bar = models.ForeignKey(Bar, related_name='foo_bar') The query: bars = Bar.objects .prefetch_related('foo_bar') .annotate(sum_foo=Sum( Case( When(foo_bar__is_deleted=False, then='foo_bar__amount'), default=Value(0), output_field=IntegerField() ) ) ) The former results in an inner join: SELECT ... FROM "bar" INNER JOIN "foo" ON ( "bar"."id" = "foo"."bar_id" ) ... What I intend to obtain is a LEFT OUTER JOIN (a full list of "bar"

Django query hitting the db for every iteration

空扰寡人 提交于 2019-12-11 03:33:36
问题 I have a some model.py like so: class Muestraonline(models.Model): accessionnumber = models.ForeignKey(Muestra, related_name='online_accessionnumber') muestraid = models.ForeignKey(Muestra) taxonid = models.ForeignKey(Taxon, null=True, blank=True) ... class Muestra(models.Model): muestraid = models.IntegerField(primary_key=True, db_column='MuestraID') # Field name made lowercase. latitudedecimal = models.DecimalField(decimal_places=6, null=True, max_digits=20, db_column='LatitudeDecimal',