Django:将auth用户导入模型

时间:2020-12-31 19:17:58

I have one problem. In Django I created a new model:

我有一个问题。在Django,我创建了一个新模型:

from django.db import models
from django.contrib.auth import user

class Workers(models.Model):
    user = models.OneToOneField(User, primary_key=True)
        work_group = models.CharField(max_length=20)
        card_num = models.IntegerField()
    def __unicode__(self):
            return self.user

But it doesn't work: ImportError: cannot import name user

但是它不起作用:ImportError:不能导入名称用户

How to fix it?

如何修复它吗?

So, I want to create a new table "workers" in db, which has a OneToOne relationship with table "auth_user". Thanks.

因此,我希望在db中创建一个新的表“workers”,它与表“auth_user”有一个一对一的关系。谢谢。

4 个解决方案

#1


145  

from django.contrib.auth.models import User

You missed the models - and user is capitalized.

您错过了模型——并且用户是资本化的。

If you use a custom user model you should use:

如果您使用自定义用户模型,您应该使用:

from django.contrib.auth import get_user_model
User = get_user_model()

More details can be found in the docs.

更多的细节可以在文档中找到。

Changed in Django 1.11:

改变了Django 1.11:

The ability to call get_user_model() at import time was added.

添加了在导入时调用get_user_model()的功能。

#2


63  

If you're using a custom User model, do the following to reference it:

如果您正在使用自定义用户模型,请执行以下操作:

from django.contrib.auth import get_user_model
User = get_user_model()

Or if using it in foreign key or many-to-many relations:

或在外键或多对多关系中使用:

from django.conf import settings
....
user = models.ForeignKey(settings.AUTH_USER_MODEL)

docs

文档

#3


1  

In order to keep your code generic, use the get_user_model() method to retrieve the user model and the AUTH_USER_MODEL setting to refer to it when defining model's relations to the user model, instead of referring to the auth user model directly.

为了保持代码的通用性,使用get_user_model()方法检索用户模型和AUTH_USER_MODEL设置,在定义模型与用户模型的关系时引用它,而不是直接引用auth用户模型。

ref: Django By Example Book

ref: Django By Example Book

#4


0  

AUTH_USER_MODEL is a good solution. here is the complete solution as per the question.

AUTH_USER_MODEL是一个很好的解决方案。这是根据问题的完整答案。

from django.db import models
from django.conf import settings

class Workers(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    work_group = models.CharField(max_length=20)
    card_num = models.IntegerField()

    def __unicode__(self):
        return self.user.id

#1


145  

from django.contrib.auth.models import User

You missed the models - and user is capitalized.

您错过了模型——并且用户是资本化的。

If you use a custom user model you should use:

如果您使用自定义用户模型,您应该使用:

from django.contrib.auth import get_user_model
User = get_user_model()

More details can be found in the docs.

更多的细节可以在文档中找到。

Changed in Django 1.11:

改变了Django 1.11:

The ability to call get_user_model() at import time was added.

添加了在导入时调用get_user_model()的功能。

#2


63  

If you're using a custom User model, do the following to reference it:

如果您正在使用自定义用户模型,请执行以下操作:

from django.contrib.auth import get_user_model
User = get_user_model()

Or if using it in foreign key or many-to-many relations:

或在外键或多对多关系中使用:

from django.conf import settings
....
user = models.ForeignKey(settings.AUTH_USER_MODEL)

docs

文档

#3


1  

In order to keep your code generic, use the get_user_model() method to retrieve the user model and the AUTH_USER_MODEL setting to refer to it when defining model's relations to the user model, instead of referring to the auth user model directly.

为了保持代码的通用性,使用get_user_model()方法检索用户模型和AUTH_USER_MODEL设置,在定义模型与用户模型的关系时引用它,而不是直接引用auth用户模型。

ref: Django By Example Book

ref: Django By Example Book

#4


0  

AUTH_USER_MODEL is a good solution. here is the complete solution as per the question.

AUTH_USER_MODEL是一个很好的解决方案。这是根据问题的完整答案。

from django.db import models
from django.conf import settings

class Workers(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    work_group = models.CharField(max_length=20)
    card_num = models.IntegerField()

    def __unicode__(self):
        return self.user.id