英文:
raise FieldError( django.core.exceptions.FieldError: Cannot resolve keyword 'is_active' into field
问题
在应用迁移时,您遇到了以下错误:
raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword 'is_active' into field. Choices are: cargo, centro, centro_id, direccion, fecha_nacimiento, id, imagen_perfil, last_login, logentry, nacionalidad, password, profesion, rut, sexo, telefono
这个错误是因为在您的自定义用户模型中,Django 试图解析一个名为 'is_active' 的字段,但未找到该字段。您的模型代码如下:
class Usuario(AbstractBaseUser):
# ...其他字段...
要解决此问题,您需要确保自定义用户模型(Usuario)继承自 AbstractBaseUser
并且包含一个名为 'is_active' 的布尔字段。通常,您需要添加以下字段到您的用户模型中:
is_active = models.BooleanField(default=True, verbose_name='Is Active')
将此字段添加到您的模型后,再次运行迁移,应该不再出现此错误。这是因为 Django 的身份验证系统依赖于 'is_active' 字段来确定用户是否处于活动状态。
在此之后,您可以在您的自定义用户模型中进行其他必要的设置和更改,以适应您的应用程序的需求。如果您有其他问题或需要进一步的帮助,请随时告诉我。
英文:
I got this doubt, I'm creating a custom user model in Django and at the moment to apply the migrations, it throws me this error
raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword 'is_active' into field. Choices are: cargo, centro, centro_id, direccion, fecha_nacimiento, id, imagen_perfil, last_login, logentry, nacionalidad, password, profesion, rut, sexo, telefono
I share you my code,
This is the model
class Usuario(AbstractBaseUser):
centro = models.ForeignKey(Centro, on_delete=models.CASCADE, verbose_name='Centro o Unidad', blank=True, null=True)
telefono = models.CharField(max_length=100, unique=True, verbose_name='Teléfono')
rut = models.CharField(max_length=100, unique=True, verbose_name='RUT')
profesion = models.CharField(max_length=150, verbose_name='Profesión')
cargo = models.CharField(max_length=150, verbose_name='Cargo')
nacionalidad = models.CharField(max_length=100, verbose_name='Nacionalidad')
sexo = models.CharField(max_length=10, choices=SEXO_CHOICES ,verbose_name='Sexo')
fecha_nacimiento = models.DateField(auto_now=False, auto_now_add=False,verbose_name='Fecha de nacimiento', blank=True, null=True)
imagen_perfil = models.ImageField(upload_to='perfil/', verbose_name='Imágen de Perfil', max_length='255', blank=True, null=True)
direccion = models.CharField(max_length=250, blank=True, null=True, verbose_name='Dirección')
objects = UsuarioManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['email', 'nombre']
def __str__(self):
return self.first_name
def get_email_user(self):
return self.email.lower()
def save(self, *args, **kwargs):
self.email = self.email.lower()
return super(Usuario, self).save(*args, **kwargs)
def has_perm(self, perm, obj = None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_staff
This is the serializer
class UsuarioSerializer(serializers.ModelSerializer):
class Meta:
model = Usuario
fields = '__all__'
extra_kwargs = {'password': {'write_only': True}, 'id': {'read_only': True}}
I was searching for a answer in several internet forums
答案1
得分: 1
自定义用户模型中,is_active
是 Django 的 AbstractBaseUser
模型中的必需字段。
您应该在 Usuario
模型中添加它,像这样:
class Usuario(AbstractBaseUser):
centro = models.ForeignKey(Centro, on_delete=models.CASCADE, verbose_name='Centro o Unidad', blank=True, null=True)
telefono = models.CharField(max_length=100, unique=True, verbose_name='Teléfono')
rut = models.CharField(max_length=100, unique=True, verbose_name='RUT')
profesion = models.CharField(max_length=150, verbose_name='Profesión')
cargo = models.CharField(max_length=150, verbose_name='Cargo')
nacionalidad = models.CharField(max_length=100, verbose_name='Nacionalidad')
sexo = models.CharField(max_length=10, choices=SEXO_CHOICES, verbose_name='Sexo')
fecha_nacimiento = models.DateField(auto_now=False, auto_now_add=False, verbose_name='Fecha de nacimiento', blank=True, null=True)
imagen_perfil = models.ImageField(upload_to='perfil/', verbose_name='Imagen de Perfil', max_length='255', blank=True, null=True)
direccion = models.CharField(max_length=250, blank=True, null=True, verbose_name='Dirección')
is_active = models.BooleanField(default=True)
objects = UsuarioManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['email', 'nombre']
def __str__(self):
return self.first_name
def get_email_user(self):
return self.email.lower()
def save(self, *args, **kwargs):
self.email = self.email.lower()
return super(Usuario, self).save(*args, **kwargs)
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_staff
然后运行迁移命令。
英文:
Since you are specifying the custom User model, the is_active
is a required field for the AbstractBaseUser
model in Django.
You should add it in the Usuario
model, like so:
<pre>
class Usuario(AbstractBaseUser):
centro = models.ForeignKey(Centro, on_delete=models.CASCADE, verbose_name='Centro o Unidad', blank=True, null=True)
telefono = models.CharField(max_length=100, unique=True, verbose_name='Teléfono')
rut = models.CharField(max_length=100, unique=True, verbose_name='RUT')
profesion = models.CharField(max_length=150, verbose_name='Profesión')
cargo = models.CharField(max_length=150, verbose_name='Cargo')
nacionalidad = models.CharField(max_length=100, verbose_name='Nacionalidad')
sexo = models.CharField(max_length=10, choices=SEXO_CHOICES ,verbose_name='Sexo')
fecha_nacimiento = models.DateField(auto_now=False, auto_now_add=False,verbose_name='Fecha de nacimiento', blank=True, null=True)
imagen_perfil = models.ImageField(upload_to='perfil/', verbose_name='Imágen de Perfil', max_length='255', blank=True, null=True)
direccion = models.CharField(max_length=250, blank=True, null=True, verbose_name='Dirección')
<b>is_active = models.BooleanField(default=True)</b>
objects = UsuarioManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['email', 'nombre']
def __str__(self):
return self.first_name
def get_email_user(self):
return self.email.lower()
def save(self, *args, **kwargs):
self.email = self.email.lower()
return super(Usuario, self).save(*args, **kwargs)
def has_perm(self, perm, obj = None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_staff
</pre>
Then run migration commands.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论