2

I'm struggling a little to work out how to follow the relation and count fields of objects.

In my Django site, I have a profile model:

user = models.ForeignKey(User, unique=True)
name = models.CharField(_('name'), null=True, blank=True)
about = models.TextField(_('about'), null=True, blank=True)
location = models.CharField(_('location'), null=True, blank=True)
website = models.URLField(_('website'), null=True, blank=True)

My understanding is that this is using the username as the foreign key.

I would like to be able to count and display the number of completed profiles my users have filled out, and ones that have a specific "element / field"? (name)* filled out. I tried:

Profile.objects.all().count()

That gave me the same number of profiles as users, which I am guessing is because the profile model exists for each user, even if it is blank.

I'm unsure how to count profiles that have one of these fields completed in them, and I am also unsure how to count the number of completed "name" fields that have been completed.

I tried:

Profile.objects.all().name.count()

Django has some good docs on queryset api, but its currently going a little over my head

  • please excuse my use of incorrect terminology.

1 Answer 1

1

You should be able to get them using:

Profile.objects.filter(name__isnull=False)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.