So what I'm trying to achieve is to count of many fields are filled on a model. I thought it might be good use case for GeneratedField, but I fail to put together the query for it.
So let's say I have a model
class ProductTagTranslation(TranslationBase):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
I now want to add field with value 1/2 if there are 2 translated fields but only one of them has been filled. I experimented with queries like
qs = ProductTagTranslation.objects.all().annotate(trans=Count(*[Case(When(~Q(Q(**{f"{fn}__isnull": True}) | Q(**{f"{fn}": ""})), then=Value(1)), default=Value(0)) for fn in fields], output_field=IntegerField(), ) / len(fields)) or
qs = ProductTagTranslation.objects.all().annotate(trans=Sum(*[Case(When(~Q(Q(**{f"{fn}__isnull": True}) | Q(**{f"{fn}": ""})), then=Value(1)), default=Value(0)) for fn in fields], output_field=IntegerField(), ) / len(fields))
where fields = ('name', 'slug') but both of those fail with different errors so I'm thinking perhaps I'm not taking the best approach here. The problem is, that I could also just do Case(...) + Case(...). One per each field, but then how do variable amount of + operations?