django模型迁移问题表已存在

问题描述 投票:0回答:1

你好,我有一个旧数据库,其中包含一个用户表,我只想向该表添加一些字段并覆盖 Django 模型身份验证的抽象基本用户 问题是当我尝试迁移时说表用户已经存在于数据库中

from django.contrib.auth.base_user import AbstractBaseUser,BaseUserManager
from django.db import models
import sha512_crypt
import time

# function that return actual timestamp
def actual_epoch_time():
    return int(time.time())
class UserManager(BaseUserManager):
    def create_user(self,email,username,password=None):
        user = self.model(email = normalize_email(email),username=username)
        user.set_password(password)
        user.save()
        return user
    def create_superuser(self,email,username,password=None):
        user = self.create_user(username=username,password=password,email=email)
        user.is_admin = True
        user.save()
        return user

#here is the abstracted user model from xui database with additional fields
class Users(AbstractBaseUser):
    username = models.CharField(max_length=50, blank=True, null=True,unique=True)
    password = models.CharField(max_length=255, blank=True, null=True)
    email = models.CharField(max_length=255, blank=True, null=True)
    ip = models.CharField(max_length=255, blank=True, null=True)
    date_registered = models.IntegerField(default=actual_epoch_time())
    last_login = models.IntegerField(blank=True, null=True)
    member_group_id = models.IntegerField(blank=True, null=True)
    credits = models.FloatField(blank=True, null=True)
    notes = models.TextField(blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)
    reseller_dns = models.TextField(blank=True, null=True)
    owner_id = models.IntegerField(blank=True, null=True)
    override_packages = models.TextField(blank=True, null=True)
    hue = models.CharField(max_length=50, blank=True, null=True)
    theme = models.IntegerField(blank=True, null=True)
    timezone = models.CharField(max_length=255, blank=True, null=True)
    api_key = models.CharField(max_length=64, blank=True, null=True)
 

    objects = UserManager()

    USERNAME_FIELD = 'username'
    class Meta:
        managed = True
        db_table = 'users'

    def set_password(self, raw_password):
        self.password = sha512_crypt.encrypt(raw_password,rounds=200000)
    def check_password(self, raw_password):
        return sha512_crypt.verify(raw_password,self.password)



django database legacy
1个回答
0
投票

您正在尝试扩展用户模型,在这种情况下,您必须删除数据库并使用用户表的新更改再次进行,但是如果数据库中有重要数据,您应该首先从中进行备份,然后删除数据库db 将其插入到新数据库中,但它总是建议在项目开始时从用户模型中创建一个抽象模型以供将来更改。

© www.soinside.com 2019 - 2024. All rights reserved.