Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4122696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:30:54+00:00 2026-05-20T23:30:54+00:00

I am trying to count the number of objects with have a foreign key

  • 0

I am trying to count the number of objects with have a foreign key relationship to another object, and which have the flag deleted=0.

User‘s have a foreignkey relationship to Accounts in the models:

class Account(models.Model):
    ...
    def __unicode__(self):
        return self.organisation

class User(models.Model):
    ...
    account = models.ForeignKey("Account", null=True, blank=True)
    deleted = models.BooleanField(blank=False)

In the views, for each account I would like to show the number of users who are attached to the account, which also have deleted=0.

Here’s the best I could do:

@login_required
def accounts(request):
    try:
        accounts = Account.objects.all().filter(deleted=0)
        users = User.objects.all().filter(deleted=0)
    except:
        raise Http404

    account_users = {'a':'a'}
    for account in accounts:
        account_users[account.id] = User.objects.all().filter(deleted=0).filter(account = account.id)

    variables = RequestContext(request, {
    'accounts':accounts,
    'users':users,
    'account_users':account_users,
    })
    return render_to_response('accounts/accounts.html', variables)

In the above, I’m using a dictionary which contains account.id => non-deleted user count. The problem with this is I can’t iterate over the dictionary key in the template…

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:30:55+00:00Added an answer on May 20, 2026 at 11:30 pm

    To iterate over dictionary use items() method. It returns a iterator over (key, value) pairs.

    for k, v in dictionary.items():
       do_something_with(k,v)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to count the number of records that have a null DateTime value.
I have two tables: subject and student. I'm trying to count the number of
I have TableView. My datasource is SQLite database. I'm trying to count a number
I'm trying to log page visits and count number of visitors. I have two
I'm trying to count the number of sent SMS messages, and when messages are
I am trying to count the number of rows whose date has not yet
I am trying to count the number of occurrences in an array of cart
I'm trying to count the number of purchases and 'up'/'down' votes for all items
I am just trying to count the number of white spaces to the LEFT
I am playing around with some recursion, and trying to count the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.