4

I have a model that is something like this:

class Input(models.Model):
        details = models.CharField(max_length=1000)
        user = models.ForeignKey(User)

class Case(Input):
    title  = models.CharField(max_length=200)
    views = models.IntegerField()

    class Argument(Input):
        case = models.ForeignKey(Case)
        side = models.BooleanField()

A user can submit many arguments, per case. I want to be able to say how many users have submitted side=true arguments.

I mean if 1 user had 10 arguments and another user had 2 arguments (both side=true) I'd want the count to be 2, not 12.

Update:

I am using these methods on the Case object:

 def users_agree(self):
        return self.argument_set.filter(side=True).values('user').distinct()
    def users_disagree(self):
        return self.argument_set.filter(side=False).values('user').distinct()

My template code calls count() on them.

2
  • I hope you are not actually using "case" Python keyword in your code. Commented May 26, 2009 at 18:58
  • 1
    I didn't know python has a keyword case. Can you refer me to docs on this? Commented May 26, 2009 at 19:40

1 Answer 1

9

Can you try:

Argument.objects.filter(side=True).values('case__user').distinct().count()

I think it does what you want. It issues one SQL query:

SELECT COUNT(DISTINCT "example_input"."user_id") FROM "example_argument" INNER JOIN "example_case" ON ("example_argument"."case_id" = "example_case"."input_ptr_id") INNER JOIN "example_input" ON ("example_case"."input_ptr_id" = "example_input"."id") WHERE "example_argument"."side" = True

Edit:

For this_case, get all users whose argument.side is True:

Argument.objects.filter(case__id=this_case.id, side=True).values('user').distinct()
Sign up to request clarification or add additional context in comments.

7 Comments

Come to think of it, this should probably be done of the Case object. Like 'for this case show me how many users are on what side'
I've added another query to address your new requirement. Can you try?
NameError: name 'this_case' is not defined
Yeah, you said "for this case show ...". Replace this_case with the Case object for which you want users whose argument.side is True.
I'm gong to call this on a instance of a Case object, so I don't think I'll need to specify a case
|

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.