简体   繁体   English

我如何使用Django将其他模型字段添加到默认auth_user模型

[英]How can i add extra model fields to default auth_user model using django

I'm new to Django. 我是Django的新手。 I need to include an extra model fields in my User Model (auth_user table in the database) as well as i want to include ForeignKey fields also inside auth_user 我需要在用户模型(数据库中的auth_user表)中包括一个额外的模型字段,以及我想在auth_user中也包括ForeignKey字段

i have four models classes like Semestertype, Programtype, Programchoice: 我有四个模型类,如Semestertype,Programtype,Programchoice:

import datetime
from django.utils import timezone

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

from django.contrib.auth.models import AbstractUser

class Semestertype(models.Model):
value = models.CharField(max_length=100, null=True, blank=True)

class Meta:
    db_table = 'semestertype'


class Programtype(models.Model):
value = models.CharField(max_length=100, null=True)

class Meta:
    db_table = 'programtype'

class Programchoice(models.Model):
value = models.CharField(max_length=100, null=True, blank=True)

class Meta:
    db_table = 'programchoice'

I included Semestertype , Programtype, Programchoice models in my auth_user models using AbstractUser like below 我使用下面的AbstractUser在auth_user模型中包括Semestertype,Programtype和Programchoice模型

class User(AbstractUser):
   semestertype = models.ForeignKey(Semestertype, on_delete=models.CASCADE,null=True, blank=True)
   programtype = models.ForeignKey(Programtype, on_delete=models.CASCADE,null=True, blank=True)
   programchoice = models.ForeignKey(Programchoice, on_delete=models.CASCADE,null=True, blank=True)
   filenumber = models.CharField(null=True, max_length=9, blank=True)
   personalemail = models.EmailField(max_length=254, blank=True)
   nameofbachelorsdegree = models.CharField(max_length=200, null=True, blank=True)

And This is my profile model: 这是我的个人资料模型:

class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,null=True, blank=True)
description = models.CharField(max_length=100,  default = '')
city = models.CharField(max_length=100, default = '')
phone = models.IntegerField(default=0)

def __str__(self):
    return self.user.username

def create_profile(sender, **kwargs):
if kwargs['created']:
    user_profile = UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=User)

Finally i add my settings.py file like this: 最后,我添加我的settings.py文件,如下所示:

AUTH_USER_MODEL = 'polls.User'# changes built-in user model to ours

I tried to adding ForeignKey fields and normal fields to auth_user and create profile form also but here i am getting error like below 我试图向auth_user添加ForeignKey字段和普通字段并创建配置文件表单,但是在这里我得到如下错误

AttributeError at /admin/polls/userprofile/add/
'NoneType' object has no attribute 'username'

I am new to Django i dont know where i did wrong pls help me anyone proper way to add extra fields to auth_user and add profile form 我是Django的新手,我不知道我在哪里做错了,请帮助我任何向auth_user添加额外字段并添加配置文件表单的正确方法

Thanks in advance... 提前致谢...

The error is raising because of the __str__() method tries to return self.user.username but, some of the UserProfile instances don't have a related user instance, (because you defined it as null=True in the model) 由于__str__()方法尝试返回self.user.usernameself.user.username但是某些UserProfile实例没有相关的user实例(因为在模型中将其定义为null=True

So, change your __str__() method of UserProfile class as below, 因此,如下更改UserProfile类的__str__()方法,

class UserProfile(models.Model):
    # your fields

    def __str__(self):
        if self.user:
            return self.user.username
        return "no user related to this profile"


Or, change your UserProfile model as below and do migration, 或者,如下更改您的UserProfile模型并进行迁移,

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)  # <<< change is here
    description = models.CharField(max_length=100, default='')
    city = models.CharField(max_length=100, default='')
    phone = models.IntegerField(default=0)

    def __str__(self):
        return self.user.username

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Django中使用auth_user模型和cutom模型字段创建自定义注册表单 - How to create custom registration form using auth_user model and cutom model fields in django 如何在 django 中的用户模型中将“移动”字段添加到默认的 auth_user 表? - How to add 'mobile' field to default auth_user table in User model in django? 如何为auth_user模型django添加索引 - How to add indexing to auth_user model django 如何更新Standart Django模型(auth_user) - How to update standart Django model (auth_user) 在 django 如何自定义 model 管理员以同时保存在 auth_user 和另一个不相关的 model 中? - In django how to customize model admin to save in auth_user and another unrelated model simultaneously? 在Django中创建新表(模型)并将其与auth_user相关 - Creating a New Table (Model) and Relating it to auth_user in Django 我如何使用 mysql 上的“用户”表,而不是 django 站点默认的“auth_user”表? - How can i use the “user” table on mysql instead of the “auth_user” table which is the default from the django site? Django:我们如何在 Group 模型上添加额外的字段 - Django : How we can add extra fields on the Group model 如何通过扩展将额外的字段添加到django模型中? - How to add extra fields to a django model by extending it? 如何在 Django 中向用户 model 添加具有默认值的字段 - How to add fields with default values ​to the User model in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM