2016-11-20 27 views
0

所以我在我的admin.py中创建了一个内联类和一些更新选定用户的操作。我面临的问题是,当我选择一个用户并执行所述操作时,它只会更新管理员用户....我猜测是因为我正在使用request。我正在尝试更新所选用户的操作,而不是启动操作的管理员用户,但我现在一直在努力解决这个问题,并试图解决这个问题。当使用queryset时,我收到错误消息'Queryset' object has no attribute 'profile',但我不确定此处还有其他用途。所以亩问题是,我正在努力做到这一点,如果有的话,任何有关如何得到这个工作的指导将不胜感激。请参阅下面的代码。Django admin对选定的用户进行更改?

admin.py

admin.site.unregister(User) 


class ProfileAdminInLine(admin.StackedInline): 
    model = Profile 


class ProfileAdmin(UserAdmin): 
    list_display = ['username', 'email', 'first_name', 'last_name', 
        'rewards_punch_card', 'rewards_tier'] 
    list_select_related = True 
    inlines = [ProfileAdminInLine] 

    actions = ['pc_add_1', 'pc_add_2', 'pc_add_3', 'pc_add_4', 'pc_add_5', 
       'pc_add_6', 'pc_add_7', 'pc_add_8', 'pc_add_9'] 

    def rewards_tier(self, user): 
     return user.profile.rewards_tier 

    def rewards_punch_card(self, user): 
     return user.profile.rewards_current 

    def pc_add_1(self, request, queryset): 

     punch_card = request.user.profile.rewards_current 
     tier = request.user.profile.rewards_tier 
     credits = request.user.profile.rewards_credits 

     user_profile = request.user.profile 

     punch_cards_updated = punch_card + 1 

     if punch_cards_updated == 10: 
      request.user.profile.rewards_current = 0 
      user_profile.save() 
      if tier == 1: 
       request.user.profile.rewards_tier = tier + 1 
       request.user.profile.rewards_credits = credits + 25 
       user_profile.save() 
      elif tier == 2: 
       request.user.profile.rewards_tier = tier + 1 
       request.user.profile.rewards_credits = credits + 30 
       user_profile.save() 
      elif tier == 3: 
       request.user.profile.rewards_tier = tier + 1 
       request.user.profile.rewards_credits = credits + 35 
       user_profile.save() 
      elif tier == 4: 
       request.user.profile.rewards_tier = tier + 1 
       request.user.profile.rewards_credits = credits + 40 
       user_profile.save() 
      elif tier == 5: 
       request.user.profile.rewards_credits = credits + 50 
       user_profile.save() 
      elif tier == 6: 
       request.user.profile.rewards_credits = credits + 50 
       user_profile.save() 

     else: 
      request.user.profile.rewards_current = punch_card + 1 
      user_profile.save() 

     self.message_user(request, "Users were successfully updated by 1 item.") 
+0

'queryset'包含在所选配置文件的列表管理员:你应该直接使用它。然而,目前还不清楚你实际上想要如何处理这些配置文件。你的代码不会改变任何东西。 –

+0

如此行'punch_cards_updated = punch_card + 1'所示,我正在尝试向'user.profile.rewards_current'对象添加一个。当使用'queryset'时,我收到一个属性错误,指出Queryset不包含任何属性配置文件。我更新我的代码以显示我正在尝试完成的全部内容。 – Kerosive

+0

但是你想保存结果吗?即使这个代码有效,它也不会在没有保存的情况下做任何事情。 –

回答

2

正如我在评论说,queryset是一组中选择的用户配置文件。所以你只需要重复一遍。很明显,你还需要一直使用循环中的配置文件,而不是request.user。

def pc_add_1(self, request, queryset): 

    for user_profile in queryset: 

     punch_card = user_profile.rewards_current 
     tier = user_profile.rewards_tier 
     credits = user_profile.rewards_credits 

     punch_cards_updated = punch_card + 1 

     if punch_cards_updated == 10: 
      user_profile.rewards_current = 0 
      if tier == 1: 
       user_profile.rewards_tier = tier + 1 
       user_profile.rewards_credits = credits + 25 
     ... 
     user_profile.save() 

(一个次要的一点:你似乎没有做任何与punch_cards_updated,你的意思是设置回user_profile.rewards_current?)

+0

工作,谢谢!至于'punch_cards_updated',我正在使用它来检查打孔卡在价值方面的内容。因为当函数的值是“18”时,我有函数进入'pc_add_9',例如我将重设卡重置为8.原因是如果打卡一直在10或更高,我希望它翻转过来。 – Kerosive