Access to data within a loop in the context - Django

1

I hope everything is fine. I have a problem and I come to the community waiting for your help! Grateful in advance.

I try to do some consultations in Django, my query what you need to do is to know some figures of some users.

It happens that I manage a structure of pyramid-type users, first I share the explanation of what I have and how I intend to do it.

When entering the system I can see the users that I have created, in the same way I can consult all of them, and also of its user structure, it happens that I also manage several types of users, one that generates these "figures" that they are players and others who are its moderators. In this case I want to consult the moderators in a date range and give me these figures, (as I said they do not have these figures and I must consult the players that belong to the.) And in this way then know what figures the moderator has in general without listing all the players.

def post(self, request, *args, **kwargs):
    moderador = request.POST.get('id_moderador')

    directa = UserHeritage.objects.filter(direct_creator=moderador).values_list('user__user__id')

    moderadores = UserProfile.objects.filter(user_id=directa).filter(user__groups__name='Moderadores')

    for moderador in moderadores:
        id_mo = moderador.user_id
        cifras = Ticket.objects.filter(user_id=id_mo).filter(register_date__range=(desde, hasta)).aggregate(cifras=Sum('cifras'))
        bet = cifras['cifras']
        print bet

    context = {'cifras': bet, 'moderadores':moderadores}
    return render(request, self.template_name,context)

In the code that I share I take the ID sent from the form and declare it as moderador and I search in a model UserHeritage all the users that the moderador is the direct_creator then in UserProfile I look for those users and only the moderators group, since I can also create another type of user.

Then I do a for to take the ID and I try to bring all the figures of all the players that exist in that table. Until then everything is fine, even the print gives me the correct data in the console, but when I try to get it out in context as you will know it is out of the loop and gives me the last result.

I try to put in context within the for and it does not work, I tried to make a list to take the IDs without doing the for.

ModeradosDict = []
for moderador in moderadores:
   MLista = moderador.user_id
   ModeradosDict.append(MLista)

But that way I go up EVERYTHING, I do not add the separate players, if there are 3 players I want the sum total of the 3 separately, but if I do it by list I add those 3 and it gives me the result, Ya knowledge came up there, I try to study more, but I need to ask this question. I hope I have explained correctly.

Thank you.

    
asked by Carlos Velazquez 17.10.2017 в 14:20
source

1 answer

0

I do not know if I understood you correctly, the second% of% share is "summarized"? If you have not done it yet, I think something like:

ModeradosDict.append(bet)

And then in the for :

context = { ‘cifras’: ModeradosDict, ... }

Maybe as context you give the id of the moderator and in the template you compare it with the moderator queryset that you are sending in the context.

    
answered by 17.10.2017 в 15:05