Django管理员对选定的用户进行了更改?

时间:2016-11-20 15:35:37

标签: python django

所以我在admin.py中创建了一个内联类,并在一些操作中更新了所选用户。我面临的问题是,当我选择一个用户并执行所述操作时,它只会更新管理员用户....我猜测是因为我使用的是request。我正在尝试更新所选用户的操作,而不是启动操作的管理员用户,但我已经苦苦挣扎了几天,现在关闭并尝试解决这个问题。使用queryset时,我会收到错误消息'Queryset' object has no attribute 'profile',但我不确定此处还有什么用处。所以mu问题是,我正在尝试做什么,如果是这样,任何关于如何使这项工作的指导将不胜感激。请参阅下面的代码。

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.")

1 个答案:

答案 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?)