Django1.5不覆盖auth\u用户

2024-09-27 22:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个更大的auth_user表,包括2-3个额外的字段。你知道吗

问题是下面的代码正在创建一个新的,与auth\u user one完全相同,但没有替换它。你知道吗

设置.py

AUTH_PROFILE_MODULE = "myaccount.MyUser"

型号.py

from django.contrib.auth.models import AbstractUser

    class MyUser(AbstractUser):
    gender = models.DateField()
    location = models.CharField(max_length=30)
    birthday = models.CharField(max_length=30)

而不是创建一个名为myaccount_MyUser的新表。如何替换当前的auth_user表而不是创建新表?你知道吗


Tags: 代码pyauthmodelsprofileonelengthmax
3条回答

我想你必须

import first django.contrib.auth.models 
import AbstractUser 

在你的型号.py在获得用AbstractUser扩展的方法之前。应该可以的

我没有尝试也不推荐的一种黑客方式是在模型中指定表名:

class MyUser(AbstractUser):
    class Meta:
        db_table = auth_user

我建议使用新表并使用south datamigration从旧表迁移数据。下面是如何做到这一点的详细答案:

Migrating existing auth.User data to new Django 1.5 custom user model?

相关问题 更多 >

    热门问题