Filtering by calculated (additional ()) fields

Does anyone know a way to filter QuerySet by a field added via the extra () method?

For example, this is what I would like to do

_list_items = ListItem.objects.filter(list=1).extra(select= 'SELECT value AS "type" FROM list_item_optional WHERE list_item_optional.list_optional_id=1 AND list_item_optional.list_item_id = list_item.id') _list_items = _list_items.filter(type='A') 

I know that the above situation is not resolved, but what will be the workaround in Django?

In the same way, the raw SQL way to do this is also invalid, for example: SELECT fieldA, fieldB, (SELECT blahblah from tableY WHERE id=1) AS "bla" FROM tableX WHERE "bla" = 'x'

Filtering by computed field is simply not allowed in Postgres ... workarounds? The problem is getting bigger because the SQL statement (QuerySet retrives) is dynamic and is only determined at run time.

Greetings

Attaching Models:

 class List(models.Model): account = models.ForeignKey(Account) created_date = models.DateTimeField(auto_now_add=True) created_by = models.ForeignKey(AccountUser, related_name='x10') updated_date = models.DateTimeField(null=True, blank=True) updated_by = models.ForeignKey(AccountUser, null=True, blank=True, related_name='x11') is_deleted = models.BooleanField(default=False) name = models.CharField(max_length=50) objects_active = DeletedManager() class Meta: db_table = u'list' class ListItem(models.Model): list = models.ForeignKey(List, db_index=True) key = models.CharField(max_length=50) name = models.CharField(max_length=100) class Meta: db_table = u'list_item' unique_together = ("list", "key") class ListOptional(models.Model): list = models.ForeignKey(List, db_index=True) name = models.CharField(max_length=50) is_searchable = models.BooleanField() class Meta: db_table = u'list_optional' class ListItemOptional(models.Model): list_item = models.ForeignKey(ListItem, null=True, db_index=True) list_optional = models.ForeignKey(ListOptional, null=True) value = models.CharField(max_length=100) class Meta: db_table = u'list_item_optional' 
+4
source share
2 answers
 SELECT value AS "type" FROM list_item_optional WHERE list_item_optional.list_optional_id=1 AND list_item_optional.list_item_id = list_item.id 

The WHERE is a combination of two conditions: list_item_optional.list_optional_id=1 and list_item_optional.list_item_id = list_item.id . Can you apply the filter conditions directly and then select them as optional and then filter? I write this answer without seeing the models in question. Providing source code for models will help.

0
source

Try the following:

 _list_items = ListItem.objects.filter(list=1).extra(select={'type': 'SELECT value AS "type" FROM list_item_optional ' 'WHERE list_item_optional.list_optional_id=1 ' 'AND list_item_optional.list_item_id = list_item.id') _list_items = _list_items.extra(where="`type` = 'A'") 

The trick is to use the extra() where argument, but in the subsequent application extra() , so the constructed query already has a new field.

0
source

All Articles