我无法将我的项目与Django中的自定义用户同步

时间:2013-12-20 04:05:39

标签: python django model

我在Django模型中有下一个用于创建自定义用户的代码: 来自django.db导入模型

#Clases abstractas para los usuarios personalizados
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager

#El mannager que seria nuestro objects que es la transicion entre modelo y la bd
class MiMannager(BaseUserManager): #Nos exige create_user y create_superuser

    #Metodo abstracto para crear usuarios
    def _create_user(self, username, dni, password, is_staff, is_superuser, **extra_fields):
        if not username:
            raise ValueError('El username debe ingresarse')

        user = self.model(username = username, dni = dni, is_active = True, is_staff = is_staff, is_superuser = is_superuser, **extra_fields)
        user.set_password(password)
        #En que base de datos se va a guardar
        user.save(using=self._db)

        return user

    #Dos metodos diferentes para crear usuarios
    def create_user(self, username, dni, password=None, **extra_fields):
        return self._create_user(username, dni, password, False, False, **extra_fields)

    def create_superuser(self, username, dni, password, **extra_fields):
        return self._create_user(username, dni, password, True, True, **extra_fields)   
#PermissioMixin es para los permisos del superusuario

class Usuario(AbstractBaseUser, PermissionsMixin):

    username = models.CharField(max_length=140, unique=True)
    dni = models.IntegerField(unique=True)
    nombre = models.CharField(max_length=140)
    apellidos = models.CharField(max_length=140)

    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    objects = MiMannager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['dni']

所以我得到一个错误:

CommandError: One or more models did not validate:
auth.user: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
auth.user: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.
Principal.usuario: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
Principal.usuario: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.

我只是关注一个教程,但我无法理解我的代码有什么问题。

1 个答案:

答案 0 :(得分:0)

尝试使用:

class Usuario(models.Model):
    username = models.CharField(max_length=140, unique=True)
    dni = models.IntegerField(unique=True)
    nombre = models.CharField(max_length=140)
    apellidos = models.CharField(max_length=140)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['dni']
    objects = MiMannager()

    def __unicode__(self):
        return self.username

    class Meta:
        db_table = 'users'

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_staff(self):
        return True

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

请注意,我继承 models.Model Usuario

并在settings.py

AUTH_USER_MODEL = 'app_name.Usuario'

您可能还想查看我用 Drupal用户表验证 Django应用的示例项目。

Link to GitHub Repository

请检查存储库的 drupal_auth 文件夹(检查managers.py)。

同时检查同一存储库的 forums / settings.py 的最后几行。

我希望这会有所帮助。