1

how can I get objects that are associated with a user

I am creating this app for managing/tracking customers

I have form which is used to save customer personal info and another field which is how much they are willing to spend + We can assign the customer to a user to be dealt with.

e.g. user1 assigned to customer1, customer2, customer3

I want to get one amount they willing to spend from all customers assigned to user1

So for example something like this

[<user1: <customer1:$10> <customer2:$100> <customer3:$1000>]

And then sum the prices together so something like this [<user1: total:$1110>]

this is what I done but doesn't seem to work

annual_spend = Lead.objects.filter(assign_to=User).exclude(lead_status='converted').aggregate(Sum('annual_spend'))

How could I do this any ideas?

M.javid
  • 6,387
  • 3
  • 41
  • 56

1 Answers1

0

For specific user:

this_user = User.objects.get(id=ENTER USER ID HERE)
annual_spend['annual_spend__sum'] = Lead.objects.filter(assign_to=this_user).exclude(lead_status='converted').aggregate(Sum('annual_spend'))

or if you want it for the user that is authenticated:

annual_spend['annual_spend__sum'] = Lead.objects.filter(assign_to=request.user).exclude(lead_status='converted').aggregate(Sum('annual_spend'))

grand total for all Lead instances:

annual_spend['annual_spend__sum'] = Lead.objects.all().exclude(lead_status='converted').aggregate(Sum('annual_spend'))

For a table with value for each user:

all_users = User.objects.all()
values = {}
for this_user in all_users:
    values[this_user.id] = Lead.objects.filter(assign_to=this_user).exclude(lead_status='converted').aggregate(Sum('annual_spend'))['annual_spend__sum']
Fabio
  • 3,015
  • 2
  • 29
  • 49
  • Thanks @dietbacon I appreciate your help! – ohhellothere Aug 19 '15 at 10:11
  • @ohhellothere No problem. If it works please accept my answer. If it doesn't let me know so I can think of what might be the problem – Fabio Aug 19 '15 at 10:15
  • it sums all amounts for user if using this this_user = User.objects.get(id=3) but I want it for every user so it would show something like this in the template http://codepad.org/jkenMTTG – ohhellothere Aug 19 '15 at 10:34
  • @ohhellothere I've added more cases to my answer, try the third one if you want the sum of all of them. If it doesn't work, please post the model for Lead there so we can have more information – Fabio Aug 19 '15 at 10:39
  • @ohhellothere oh ok, so you want the sum for each one? – Fabio Aug 19 '15 at 10:42
  • yes, so for each user only, so for user1 assigned to customers sum them all up, for user2 sum all the amounts up and so on. – ohhellothere Aug 19 '15 at 10:46
  • @ohhellothere ok, i've added another one that should do the trick, it should return a dictionary called values with all the values you want, where the key is the user's id – Fabio Aug 19 '15 at 10:46
  • It works thanks a lot! But how can I show the user instead of annual_spend as shown in here http://codepad.org/NRjgPd44. – ohhellothere Aug 19 '15 at 10:55
  • @ohhellothere I've edited the answer, it was my bad. If you use the code now, it should return only user's id and the sum. If you want to use something other than the user's id as the key for the dictionary, you could substitute `values[this_user.id]` for something like `values[this_user.first_name]`, but beware of repeated keys. – Fabio Aug 19 '15 at 10:59
  • Thanks a bunch!!! Is there way I can contact you I would like to ask couple of questions? 'not relating this' – ohhellothere Aug 19 '15 at 11:06
  • I am a bit stuck so if I want to show the data in the template I would do this {{ values }}? or {% for value in values %} – ohhellothere Aug 19 '15 at 12:01
  • @ohhellothere More in lines with the second one, but since it is a dictionary, you can't iterate through it just like that. http://stackoverflow.com/questions/8018973/how-to-iterate-through-dictionary-in-a-dictionary-in-django-template – Fabio Aug 19 '15 at 14:19