我是否在Django中以错误的方式覆盖了模型上的save方法? [英] Am I overriding the save method on the model in a wrong way in django?

查看:45
本文介绍了我是否在Django中以错误的方式覆盖了模型上的save方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个个人资料模型,可以像这样扩展用户模型,

I have a Profile model that extends the user model like so,

class Profile(User):
    user = models.OneToOneField(User, parent_link=True, on_delete=models.CASCADE)
    slug = models.SlugField(unique=True, blank=True)
    def save(self, *args, **kwargs):
        print('self.username')
        print(self.username)
        self.slug = self.username
        super(Profile, self).save(*args, **kwargs)

我是尝试为我的模型创建一个Slug字段,因此我覆盖了save方法以将Slug用作用户名。关键是,当我使用命令 createsuperuser 创建一个新用户并打印出用户名时(如您在代码中看到的那样),它不会显示任何内容-它不会t显示提供的用户名。这可能是我出现此问题的原因吗?如果是这样,我该如何解决?

I am trying to create a slug field for my model , so I override the save method to include the slug as the username. The thing is, when I create a new user with the command createsuperuser and print out the username as you can see in the code, it doesn't show anything - it doesn't show the provided username. Could this be the reason why I am having this problem ? And if so, how can I fix it?

推荐答案

如果您确定不想拥有用户的个人资料数据在具有 OneToOneField 的单独模型中返回到Django的默认 User ,那么您可能应该将 AbstractUser 而非文档中指定的 User

If you're sure you don't want to have your user's profile data in a separate model with a OneToOneField back to Django's default User, then you should probably subclass AbstractUser and not User, as specified in the docs.

https://docs.djangoproject.com/ zh_cn / 2.2 / topics / auth / customizing /#substituing-a-custom-user-model

请考虑以下部分:


如果您对Django的User模型完全满意,并且只想添加一些其他配置文件信息,则可以简单地将django.contrib.auth.models.AbstractUser子类化。添加您的自定义配置文件字段[...]

If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you could simply subclass django.contrib.auth.models.AbstractUser and add your custom profile field [...]

(来自 https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#extending-django-s-default-user

然后您将像这样:

from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import UserManager
from django.utils.text import slugify
from django.db import models


class User(AbstractUser):
    slug = models.SlugField(unique=True, blank=True)

    objects = UserManager()

    def save(self, *args, **kwargs):
        print('self.username')
        print(self.username)
        self.slug = slugify(self.username)
        super().save(*args, **kwargs)

然后,定义 settings.AUTH_USER_MODEL

AUTH_USER_MODEL = "myapp.User"  # also, add 'myapp' to INSTALLED_APPS

记住:哟您将需要像这样引用您的User模型(无论您是否自定义该模型,这都是您应该采取的方式):

Remember: you'll need to reference your User model like this (and this is how you should do it anyway, whether you customized that model or not):

from django.contrib.auth import get_user_model

User = get_user_model()

这篇关于我是否在Django中以错误的方式覆盖了模型上的save方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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