“经理"对象没有属性"normalize_email" [英] 'Manager' object has no attribute 'normalize_email'

查看:83
本文介绍了“经理"对象没有属性"normalize_email"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的模型,它具有多对多关系:

I have a fairly simple model with a many-to-many relationship:

class Person(AbstractUser):
...
    topics = models.ManyToManyField("web.Topic", through="Interests", blank=True)
...

    objects = models.Manager()

    def __str__(self):
        return "{last}, {first} ({id})".format(
            last=self.last_name, first=self.first_name, id=self.id
        )

    class Meta:
        ordering = ("last_name", "first_name")

基于该模型,我的表单定义为:

Based on that model, I have a form defined by:

class TopicForm(ModelForm):
    class Meta:
        model = Person
        fields = ("topics",)

    def __init__(self, *args, **kwargs):
        super(TopicForm, self).__init__(*args, **kwargs)

        self.fields["topics"].widget = SelectMultiple(attrs={"size": 20})
        self.fields["topics"].queryset = Topic.objects.order_by(Lower("name")).all()
        self.fields[
            "topics"
        ].help_text = "Don't forget to hold the CTRL key (or cmd on a Mac) to select multiple topics"

最后,视图如下:

def update_my_profile(request):
    topics_form = TopicForm(request.POST or None, instance=request.user)

    if request.method == "POST":
      if topics_form.is_valid():
                topics_form.save()
    
    context = {
        "topics_form": topics_form,
    }
    return render(request, "web/profile.html", context)

出于某种原因,我真的不明白,django框架在 if topic_form.is_valid()上失败了:我收到以下错误消息:

for a reason I really don't get, the django framework is failing on if topics_form.is_valid(): I get the following error message:

"Manager"对象没有属性"normalize_email"

'Manager' object has no attribute 'normalize_email'

令人惊讶的是,失败的代码在django框架内(第365行的django/contrib/auth/models.py干净):

Surprinsingly, the failing code is within the django framework (django/contrib/auth/models.py in clean at line 365):

    def clean(self):
        super().clean()
        self.email = self.__class__.objects.normalize_email(self.email)

更令人惊讶的是,如果我在 if topic_form.is_valid():行上放置一个断点(在VSCode中)并在调试控制台中手动运行该命令,它不会失败...

Even more surprisingly, if I put a breakpoint (in VSCode) at the line if topics_form.is_valid(): and run the command manually in the debug console, it does not fail...

我想念什么?

推荐答案

删除此行:

objects = models.Manager()

来自 Person 模型,因此它可以使用 AbstractUser 类中指定的适当管理器.

from Person model, so it can use the proper Manager that is specified in AbstractUser class.

这篇关于“经理"对象没有属性"normalize_email"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆