django-queryset

Django Count and Sum annotations interfere with each other

梦想的初衷 提交于 2019-12-07 01:50:43
问题 While constructing a complexe QuerySet with several annotations, I ran into an issue that I could reproduce with the following simple setup. Here are the models: class Player(models.Model): name = models.CharField(max_length=200) class Unit(models.Model): player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='unit_set') rarity = models.IntegerField() class Weapon(models.Model): unit = models.ForeignKey(Unit, on_delete=models.CASCADE, related_name='weapon_set') With my test

Django REST Framework Serialize extremely slow

眉间皱痕 提交于 2019-12-07 00:15:21
问题 I am in Python 2.7 and Django 1.7.1 with django-restframework I have an API that returns me some specific values taken fron the Database, it uses a Custom Serializer like this: class InventarioSerializer(serializers.ModelSerializer): item = serializers.RelatedField(source='producto.item') ubicacion = serializers.RelatedField(source='ubicacion.nombre') class Meta: model = Inventario fields = ('epc','item','cantidad','ubicacion') My API's view is called this way: class ItemEnInventarioViewSet

Django reverse lookup chaining queryset

梦想与她 提交于 2019-12-06 21:30:29
I have several models that I need to do reverse lookups in, then somehow chain the querysets together. I got 3 models, (1) designers create (2) projects, and projects contain (3) uploaded images. Models class Designer(models.Model): ... class Project(models.Model): designer = models.ForeignKey('Designer') class UploadedImage(models.Model): project = models.ForeignKey('Project') So a designer opens up his page and wants to see all his projects and some images associated with his projects, I could do something like, d = Designer.objects.get(id=2) projects = d.project_set.all() But then with

How to pass additional arguments to custom python sorting function

天大地大妈咪最大 提交于 2019-12-06 21:10:47
问题 Background: I would like to know how I can implement advanced sorting functions that I can pass in as tuple element to the key argument of the python 'sorted' function. Here is an example depicting what I would like to do: class Book: def __init__(self, name, author, language, cost): self.name = name self.author = author self.language=language self.cost = cost bookList = [list of books] firstLanguage = "Armenian" possibleLanguages = ["English", "Spanish", "Armenian", "French", "Chinese",

Django Inner Join Queryset

扶醉桌前 提交于 2019-12-06 17:56:16
问题 I'm working with Django and I need to do a queryset using two inner joins. I have three models A, B, and C and I want to do a query like the following in psql: select DISTINCT a from A inner join B on B.a_id = A.id inner join C on C.b_id = B.id; Models: (only included relevant fields) class A(models.Model): id = models.IntegerField(primary_key=True) class B(models.Model): id = models.IntegerField(primary_key=True) a = models.ForeignKey(A, null=True, blank=True,on_delete=models.SET_NULL) class

Recursive QuerySet with django

若如初见. 提交于 2019-12-06 16:46:58
I have this model referencing itself to allow building a tree: class PartCategory(models.Model): parent = models.ForeignKey('PartCategory', on_delete=models.DO_NOTHING, null=True, default=None, blank=True) name = models.TextField() I now have an SQL query to get one element and all of its child in one select (in this example the element with id=64): WITH RECURSIVE under_partcategory(id,name, parent_id,level) AS ( select api_partcategory.id,api_partcategory.name,api_partcategory.parent_id,0 from api_partcategory where api_partcategory.id=64 UNION ALL SELECT api_partcategory.id,api_partcategory

django combine queries on two different base models

笑着哭i 提交于 2019-12-06 14:07:43
I have two different different queryset, I want to make union of both queryset q1 = tbl_nt_123.objects.values_list('id', 'value', 'geometry').filter(restriction='height')\ .exclude(condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True)) q2 = tbl_network.objects.all().filter(Q(name="height"), Q(state_id=1) | Q(state_id=2)).values_list('id', 'value', 'geometry'); I have tried this function queryset = (q1) | (q2) facing this error ' django combine queries on two different base models ' So i have tried two other functions but these are also throwing same

Django reverse query by the last created object

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 13:13:45
I have two models: class SomeActivity(models.Model): name = models.ChartField(max_length=100) class SomeStatus(models.Model): name = models.CharField(max_length=100) status = models.IntegerField(choises=STATUS_CHOISES) some_activity = models.ForeignKey(SomeActivity, related_name='statuses') The last created status for the activity is the current one. To get it I use this code: try: last_status = some_activity.statuses.latest('id') except: last_status = None But the problem is when I want to make a query that returns all Activities that have a last_status matching status__in=[1, 2] . iMom0 This

Custom properties in a query

我只是一个虾纸丫 提交于 2019-12-06 10:14:44
Given the simplified example below, how would I access my custom "current_status" property within a queryset? Is it even possible? At the moment, I want to list the all the current Events and show the current status. I can get the property to display in a template ok, but I can't order the queryset by it. Alternatively, would I need to create a custom manager with some kind of nested "if" statement in the 'Select'? class Event(models.Model): .... date_registered = models.DateField(null=True, blank=True) date_accepted = models.DateField(null=True, blank=True) date_reported = models.DateField

Two or more __in filters in django queryset

对着背影说爱祢 提交于 2019-12-06 10:13:16
问题 I have this query query = 'select * from products where (productnr, supplier_id) in (%s)' % product_list where product_list looks like this ((OB520, 3),(RH402, 20)...) How do I go about doing this in Django using queryset and the __in filter 回答1: What part of this is confusing? http://docs.djangoproject.com/en/1.2/ref/models/querysets/#in It seems very clear. It's not perfectly clear from the question what the problem is. Are you asking how to use a multi-part key? If so, you're going to be