如何删除django用户模型中的名字和姓氏列? [英] How to delete the first name and last name columns in django user model?

查看:65
本文介绍了如何删除django用户模型中的名字和姓氏列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义用户模型,就像文档说,当我运行 makemigrations 时,我转到了迁移文件夹,并删除了 0001_initial.py ,然后我运行了 migrate 命令,但是当我运行 createsuperuser 命令时,它在终端中抛出了一个错误,该错误提示django.db.utils.OperationalError:无此类列:myapp_usermodel.first_name

I created a custom user model just like the docs said, when I ran the makemigrations I went to the folder of migrations and deleted the first name and last name columns inside the 0001_initial.py and then I ran the migrate command, but when I ran the createsuperuser command it throwed an error in the terminal that django.db.utils.OperationalError: no such column: myapp_usermodel.first_name

我该如何解决?我想删除名字和姓氏列,因为我没有使用它们

How can I fix this? I want to delete the first name and last name columns because I'm not using them

编辑:这是用户模型

from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractUser

# Create your models here.
class usermodel(AbstractUser):
    email = models.EmailField(max_length=60)
    username = models.CharField(max_length=20, unique=True)
    password = models.CharField(max_length=20)
    
    def __str__(self):
        return self.username

推荐答案

我转到了迁移文件夹,并删除了 0001_initial.py

您不应该那样做.这确实将阻止Django 创建列.但是,现在每次都会以列在那的印象为背景进行查询.此外,如果再次进行迁移,它将尝试添加额外的列.

You shouldn't do that. This will indeed prevent Django from creating the columns. But now it will each time query under the impression that the columns are there. Furthermore if you make migrations again, it will try to add extra columns.

您应该只使用方法解析顺序(MRO),并将字段设置为 None :

You should simply use the method resolution order (MRO) and set the field to None:

class usermodel(AbstractUser):
    first_name = None
    last_name = None
    
    def __str__(self):
        return self.username

您最好也删除数据库中的表和迁移文件,然后再次进行迁移,然后迁移数据库.

You probably also better remove the table in the database and the migration file and make migrations again, and migrate the database.

这篇关于如何删除django用户模型中的名字和姓氏列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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