40๐
A safer way to do this is to use the AUTH_USER_MODEL
from the settings file.
Example:
from django.db import models
from django.conf import settings
class Article(models.Model):
headline = models.CharField(max_length=255)
article = models.TextField()
author = models.ForeignKey(settings.AUTH_USER_MODEL)
By default settings.AUTH_USER_MODEL
refers to django.contrib.auth.models.User
without requiring you to do anything.
The advantage of this approach is that your app will continue to work even if you use a custom user model without modification.
For more information on how to make use of custom user models check out this part of the Django docs
๐คrgeber
26๐
You just want:
from django.contrib.auth.models import User
class MyModel(models.Model):
...
user = models.ForeignKey(User)
...
๐คJoe
- Graphene-python performance issues for large data sets
- How can I test if my redis cache is working?
- Django ChoiceField populated from database values
- How to convert request.user into a proxy auth.User class?
- ImportError: cannot import name "urandom"
Source:stackexchange.com