django

How to add a function result from a model to a list?


How to add a function result from a model to a list 'subdom()'

models.py

class Hromady(models.Model): ...

    def subdom(self):
        if self.alias:
            sub = self.alias
        else:
            sub = self.code.lower()
        return sub

views.py

def index(request):
    hromads = Hromady.objects.all()
    for hr in hromads:
        print(hr.subdom()) # ok

    hrom = hromads.annotate(obj_count=Count('object', filter=Q(object__state=1)))
    map_hrom = list(hrom.values('region', 'district', 'locality', 'type', *****subdom()*****???, 'obj_count'))

Solution

  • You cannot reference methods of your models in .values. But you can write a similar expression using another .annotate and then reference it in .values. It would look something like this:

    from django.db.models import (
        Case,
        Count,
        F,
        Q,
        When,
        functions,
    )
    
    queryset = (
        Hromady.objects
        .annotate(obj_count=Count('object', filter=Q(object__state=1)))
        .annotate(
            subdom=Case(
                When(~Q(alias=''), then=F('alias')),
                default=functions.Lower(F('code'))
            )
        )
        .values('region', 'district', 'locality', 'type', 'subdom', 'obj_count')
    )