224
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:
The ability to call get_user_model() at import time was added.
83
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)
- [Django]-How to change User representation in Django Admin when used as Foreign Key?
- [Django]-Django @login_required decorator for a superuser
- [Django]-Django check if a related object exists error: RelatedObjectDoesNotExist
5
AUTH_USER_MODEL
is a good solution. here is the complete solution as per the question.
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
- [Django]-Class Based Views VS Function Based Views
- [Django]-Django CSRF Cookie Not Set
- [Django]-Django : How can I find a list of models that the ORM knows?
3
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.
ref: Django By Example Book
- [Django]-ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
- [Django]-How can I activate the unaccent extension on an already existing model
- [Django]-VueJS + Django Channels
Source:stackexchange.com