所以我在我的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.")
'queryset'包含在所选配置文件的列表管理员:你应该直接使用它。然而,目前还不清楚你实际上想要如何处理这些配置文件。你的代码不会改变任何东西。 –
如此行'punch_cards_updated = punch_card + 1'所示,我正在尝试向'user.profile.rewards_current'对象添加一个。当使用'queryset'时,我收到一个属性错误,指出Queryset不包含任何属性配置文件。我更新我的代码以显示我正在尝试完成的全部内容。 – Kerosive
但是你想保存结果吗?即使这个代码有效,它也不会在没有保存的情况下做任何事情。 –