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

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

问题描述

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

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 创建一个新用户并打印出您在代码中看到的用户名时,它没有显示任何内容 - 它没有显示提供的用户名.这可能是我遇到这个问题的原因吗?如果是这样,我该如何解决?

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/en/2.2/topics/auth/customizing/#substituting-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 中以错误的方式覆盖了模型上的保存方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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