gpt4 book ai didi

python - 禁止用户并将其添加到表中时出现问题

转载 作者:行者123 更新时间:2023-12-01 09:26:21 25 4
gpt4 key购买 nike

我有一个旨在禁止用户的函数,然后将其添加到 Banned_User 表中。这样我就可以向用户发送一封包含其禁令详细信息的电子邮件。但我可以好像没有禁止和保存的功能

当前回溯错误:异常类型:/admin/api/profile/处的 TypeError异常值:禁止直接分配到多对多集的前向端。请改用report_reason.set()。

def banning_users(self, request, queryset):

for obj in queryset:
if hasattr(obj, 'user'):
# This object is a Profile, so lookup the user
profile = obj
user = obj.user
user.is_active = False
user.save()


# Get the report(s) for this user
user_reports = Report.objects.filter(user_reported=profile)

# Go through each report, in case there are multiples,
# add a record in the ban table

banned_reasons = []

for report in user_reports:
ban_record = Banned_User.objects.create(profile=profile, report_reason=report)
ban_record.save()
banned_reasons.append(report.get_report_reason_display())



# Send the email
subject = 'Ban'
message = 'You have been banned for the following reasons: []' + \
report.report_reason + banned_reasons[0] + banned_reasons[1]

email_from = settings.EMAIL_HOST_USER
recipient_list = [user.email]
send_mail( subject, message,email_from, recipient_list)

self.message_user(request, "User is banned and Email has been sent")

BANNED_USER 模型

class Banned_User(models.Model):
def __str__(self):
return str.join(str(self.profile), str(self.report_reason.report_reason))
#return self.profile
#return str.join(str(self.profile), str(self.report_reason))

profile = models.ForeignKey(
'Profile',
on_delete=models.PROTECT,
blank=False,
null=False,
related_name='banned_profile'
)

report_reason = models.ManyToManyField(
'Report',
#on_delete=models.PROTECT,
blank=True,
#null=True,
)

date_banned = models.DateField(null=True, blank=False,)

报告模型

class Report(models.Model):
def __str__(self):
return str.join(str(self.user_reported), str(self.datetime_sent))

TOXICITY = 'Toxicity'
SPORTSMANSHIP = 'Poor sportsmanship'

REPORT_REASON_CHOICES = (
(TOXICITY, 'Toxicity'),
(SPORTSMANSHIP, 'Unsportsmanlike Behaviour'),
)

session = models.ForeignKey(
'Session',
on_delete=models.PROTECT,
blank=False,
null=False,
)

user_reported = models.ForeignKey(
'Profile',
on_delete=models.PROTECT,
blank=False,
null=False,
related_name='user_reported_report',
)

sent_by = models.ForeignKey(
'Profile',
on_delete=models.PROTECT,
blank=False,
null=False,
related_name='sent_by_report',
)

report_reason = models.CharField(
max_length=255,
choices=REPORT_REASON_CHOICES,
default=TOXICITY,
)

datetime_sent = models.DateTimeField(auto_now_add=True,)

最佳答案

报告失败的原因是您正在尝试将外键分配给多对多字段,该字段设置为期望您使用添加命令。

ban_record = Banned_User.objects.create(profile=profile, report_reason=report)

这是行不通的。您需要做的是为配置文件创建banned_user模型

banned_user = profile.banned_profile.create(profile=profile)

然后您将为被禁止的用户添加禁止原因已编辑

reports = banned_user.profile.user_reported_report.all()
for report in reports:
banned_user.report_reason.add(report)

您可以在此处了解如何处理多对多字段。 https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/

关于python - 禁止用户并将其添加到表中时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50361015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com