Python Django管理员是否对所选用户进行更改?
因此,我在admin.py中创建了一个内联类,并执行了一些操作来更新所选用户。我面临的问题是,当我选择一个用户并执行上述操作时,它只会更新管理员用户……我猜这是因为我使用的是Python Django管理员是否对所选用户进行更改?,python,django,Python,Django,因此,我在admin.py中创建了一个内联类,并执行了一些操作来更新所选用户。我面临的问题是,当我选择一个用户并执行上述操作时,它只会更新管理员用户……我猜这是因为我使用的是请求。我正在尝试更新所选用户的操作,而不是启动该操作的管理员用户,但我已经为此挣扎了几天,现在断断续续地尝试解决这个问题。使用queryset时,我会收到错误消息说'queryset'对象没有属性'profile',但我不确定这里还可以使用什么。所以mu的问题是,我正在尝试做的是可能的,如果是的话,任何关于如何让这项工作的指
请求
。我正在尝试更新所选用户的操作,而不是启动该操作的管理员用户,但我已经为此挣扎了几天,现在断断续续地尝试解决这个问题。使用queryset
时,我会收到错误消息说'queryset'对象没有属性'profile'
,但我不确定这里还可以使用什么。所以mu的问题是,我正在尝试做的是可能的,如果是的话,任何关于如何让这项工作的指导都将非常感谢。请看下面我的代码
管理员
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.")
正如我在评论中所说,
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()
(一个小问题:你似乎没有对更新后的打孔卡做任何事情;你的意思是要将其设置回用户档案。当前的奖励?)正如我在评论中所说,
查询集是所选的用户档案集。所以你只需要迭代一下。显然,您还需要一致地使用循环中的概要文件,而不是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()
(一个小问题:你似乎没有对更新后的打孔卡做任何事情;你的意思是将其设置回用户档案。当前的奖励?)queryset
包含在管理中选择的档案列表:你应该直接使用它。然而,不清楚你到底想用这些配置文件做什么;您的代码没有改变任何内容。如此行所示,punch\u cards\u updated=punch\u card+1
我正在尝试向user.profile.rewards\u current
对象添加一个。使用queryset
时,我收到一个属性错误,说明queryset不包含属性配置文件。我更新代码以显示我试图完成的全部任务。但是否要保存结果?即使此代码有效,如果不保存,它仍然无法执行任何操作。我更新了代码以显示我正在执行的操作的全貌queryset
包含在管理中选择的配置文件列表:您应该直接使用它。然而,不清楚你到底想用这些配置文件做什么;您的代码没有改变任何内容。如此行所示,punch\u cards\u updated=punch\u card+1
我正在尝试向user.profile.rewards\u current
对象添加一个。使用queryset
时,我收到一个属性错误,说明queryset不包含属性配置文件。我更新代码以显示我试图完成的全部任务。但是否要保存结果?即使这段代码有效,如果不保存,它仍然不能做任何事情。我更新了代码,以显示我正在工作的全部情况,谢谢!至于punch\u cards\u updated
我用它来检查punch\u cards的价值。例如,当值为18
时,我将有一些函数转到pc\u add_9
,因此我将punchcard重置为8。这样做的原因是,如果punchcard是在10或以上,我希望它只是滚动。这起作用了,谢谢!至于punch\u cards\u updated
我用它来检查punch\u cards的价值。例如,当值为18
时,我将有一些函数转到pc\u add_9
,因此我将punchcard重置为8。这样做的原因是,如果punchcard在10或以上,我希望它只是翻滚。