V
V
val1n0r2020-02-21 14:55:44
Django
val1n0r, 2020-02-21 14:55:44

How to correctly update the data displayed by context'om in a timely manner?

All ku.

There is such a cbv

class StartFight(FightMixin,HeroEquipInfo, UserInfo, View):
    def post(self, request, hero_unique_id, *args,**kwargs):
        uid = request.user.id

        logic_data = super(StartFight, self)
        user_stat_list = logic_data.UserStat(uid)

        context = {}


        context = user_stat_list

        

        it_stat = logic_data.GetItemStat(uid)
        user_hero_power = it_stat['total_power']

        en_list = UserActiveHero.objects.filter(hero_power__lte=user_hero_power)
        context['enemy'] = en_list
        fight_resultat = logic_data.fight_result(uid,hero_unique_id,user_stat_list)
        context['fight_res'] = fight_resultat
        return render(request,'fight.html',context)


As you can see above - I pass data to the "FightMixin,HeroEquipInfo, UserInfo" mixins for processing.
Here is the main mixin that interests me
class FightMixin(object):
  def fight_result(self, uid, hero_unique_id, user_stat_list, *args, **kwargs):

    #Статы юзера
    user_hero = UserActiveHero.objects.filter(user_hero_id=uid)
    #Достаем статы противника
    enemy_hero = UserActiveHero.objects.filter(hero_unique_id=hero_unique_id)
    
    if enemy_hero and user_hero:
      #ОПЫТ СЕРЕБРО И ТП
      silver_win_reward = 120
      silver_loose_reward = 50
      xp_win_reward = 25
      xp_loose_reward = 10

      if user_stat_list['xp_buff']:#:
        xp_win_reward = xp_win_reward * 2
        xp_loose_reward = xp_loose_reward * 2



      fight_result_list ={'w_l':None,'xp':None,'silver':None,'drop_portal':None,'fight_color':None,'energy':None}
      for i in enemy_hero:
        enemy_power = i.hero_power

      for u in user_hero:
        user_power = u.hero_power
          # ПОБЕДА
      if user_power > enemy_power and user_stat_list['energy'] >=10:
        #user win
        fight_result_list['energy'] = True
        fight_result_list['w_l'] = 'Победа!'
        fight_result_list['fight_color'] = '#207C1C'
        fight_result_list['xp'] = xp_win_reward
        fight_result_list['silver'] = silver_win_reward
        fight_result_list['drop_portal'] = False

        #Добавляем победные статы
        UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+250,xp=F('xp')+xp_win_reward,energy=F('energy')-10,
                      totalWin=F('totalWin')+1)

        UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+20)

        return fight_result_list

          #НИЧЬЯ
      elif user_power == enemy_power and user_stat_list['energy'] >=10:
        res = random.randint(1,4)
        if res == 1 or res == 2:
          #user win
          fight_result_list['energy'] = True
          fight_result_list['w_l'] = 'Победа!'
          fight_result_list['fight_color'] = '#207C1C'
          fight_result_list['xp'] = xp_win_reward
          fight_result_list['silver'] = silver_win_reward
          fight_result_list['drop_portal'] = False
          
          #Добавляем победные статы
          UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_win_reward,xp=F('xp')+xp_win_reward,energy=F('energy')-10,
                      totalWin=F('totalWin')+1)
          UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+xp_win_reward)

          return fight_result_list


        else: #Поражение
          fight_result_list['energy'] = True
          fight_result_list['w_l'] = 'Поражение'
          fight_result_list['fight_color'] = '#CB4A3F'
          fight_result_list['xp'] = xp_loose_reward
          fight_result_list['silver'] = silver_loose_reward
          fight_result_list['drop_portal'] = False

          UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_win_reward,energy=F('energy')-10,
                      )
          UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+xp_win_reward)

          return fight_result_list


            #ПОРАЖЕНИЕ
      elif user_power < enemy_power and user_stat_list['energy'] >=10:
        fight_result_list['energy'] = True
        fight_result_list['w_l'] = 'Поражение'
        fight_result_list['fight_color'] = '#CB4A3F'
        fight_result_list['xp'] = xp_loose_reward
        fight_result_list['silver'] = silver_loose_reward
        fight_result_list['drop_portal'] = False

        UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_loose_reward,energy=F('energy')-10,)
        UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+silver_loose_reward)

      # вернуть лист со статусом и наградами
        return fight_result_list

      elif user_stat_list['energy'] <10:
        fight_result_list['energy'] = False
        return fight_result_list


    else:
      return False

In this piece of code in the mixin above, provided that the data is updated, the data sheet is returned to the context
elif user_power < enemy_power and user_stat_list['energy'] >=10:
        fight_result_list['energy'] = True
        fight_result_list['w_l'] = 'Поражение'
        fight_result_list['fight_color'] = '#CB4A3F'
        fight_result_list['xp'] = xp_loose_reward
        fight_result_list['silver'] = silver_loose_reward
        fight_result_list['drop_portal'] = False

        UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_loose_reward,energy=F('energy')-10,)
        UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+silver_loose_reward)

      # вернуть лист со статусом и наградами
        return fight_result_list

The displayed data that these lines update is not updated in the context immediately, but only after the next page update (everything in the database is updated immediately, but the displayed data in the template is not)
UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_loose_reward,energy=F('energy')-10,)

UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+silver_loose_reward)

Pressed the button -> called the "StartFight" view -> passed the data to the mixin, processed -> returned the data, but the data that is updated in other models is displayed only one more update, and not immediately.

I understand that the question was formed crookedly, but I can not better =)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-02-21
@val1n0r

Use instead of QuerySet.update() to change object attributes and MyModel().save() (if I didn't misunderstand the question)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question