Home > Blockchain >  How to get the count of fields that are either None ot empty in a row using ORM Django?
How to get the count of fields that are either None ot empty in a row using ORM Django?

Time:04-14

I have a model,

class personal_profile(models.Model):
    custom_user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    picture = models.ImageField(default='pro.png', upload_to='profile_image', blank=True)
    designation = models.CharField(max_length=255, blank=True, null=True)
    gender = models.CharField(max_length=255, blank=True, null=True)
    date_of_birth = models.DateField(blank=True, null=True)
    language = models.CharField(max_length=255, blank=True, null=True)
    linkedin_url = models.CharField(max_length=255, blank=True, null=True)
    def __str__(self):
        return str(self.pk)

I want to get the number of field's count which are either None or "".

personal_profile.objects.get(custom_user=some_user_id)

By the above query, I can get the queryset of that specific user. Now I want to check all the fields belongs to that custom_user. For example, picture, desgination. gender, date_of_birth, language, linkedin_url

Thanks

CodePudding user response:

You have two choices. You can either do the counting at the python level or the database level.

Solution #1 Python level

Use the @property decorator to count the number of empty fields.

class PersonalProfile(models.Model):
    ...

    @property
    def empty_fields_count(self):
        fields = ["picture", "desgination", "gender", "date_of_birth", "language", "linkedin_url"]
        empty_values = {"", None}
        empty_values_count = 0

        for field in fields:
            field_value = getattr(self, field)
            if field_value in empty_values:
                empty_values_count  = 1
        return empty_values_count

And use it like so

personal_profile = PersonalProfile.objects.get(custom_user=some_user_id)
# this will return the number of empty fields
personal_profile.empty_fields_count

Solution #2 Database level

Use annotate to sum the number of empty fields

personal_profile_queryset = PersonalProfile.objects.annotate(
    picture_empty=models.Case(
        models.When(models.Q(picture__isnull=True) | models.Q(picture=""), then=1),
        default=0,
    ),
    desgination_empty=models.Case(
        models.When(models.Q(desgination__isnull=True) | models.Q(desgination=""), then=1),
        default=0,
    ),
    gender_empty=models.Case(
        models.When(models.Q(gender__isnull=True) | models.Q(gender=""), then=1),
        default=0,
    ),
    date_of_birth_empty=models.Case(
        models.When(date_of_birth__isnull=True, then=1),
        default=0,
    ),
    language_empty=models.Case(
        models.When(models.Q(language__isnull=True) | models.Q(language =""), then=1),
        default=0,
    ),
    linkedin_url_empty=models.Case(
        models.When(models.Q(linkedin_url__isnull=True) | models.Q(linkedin_url=""), then=1),
        default=0,
    ),
    empty_fields_count=(
        models.F("picture_empty")   
        models.F("desgination_empty")   
        models.F("gender_empty")   
        models.F("date_of_birth_empty")   
        models.F("language_empty")   
        models.F("linkedin_url_empty")
    )
)

Then use the queryset as follows

personal_profile = personal_profile_queryset.get(custom_user=some_user_id)
# this will return the number of empty fields
personal_profile.empty_fields_count

Notes

You might want to move the annotate codes to be in a custom QuerySet or Manager if you want to reuse the queryset with annotated empty_fields_count attribute.

  • Related