You probably don't need get_user_model (#4879)

* You probably don't need `get_user_model`
Fixes #4872

* Fix Flake8 (add space after import)

* (try to) fix ruff

* Fix import format
This commit is contained in:
Matthew Foster Walsh 2024-02-21 05:01:56 -05:00 committed by GitHub
parent 8c60674654
commit b756d904d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 14 additions and 24 deletions

View File

@ -1115,7 +1115,7 @@
"twitter_username": "Qoyyuum" "twitter_username": "Qoyyuum"
}, },
{ {
"name": "mfosterw", "name": "Matthew Foster Walsh",
"github_login": "mfosterw", "github_login": "mfosterw",
"twitter_username": "" "twitter_username": ""
}, },

View File

@ -1,19 +1,17 @@
from django.conf import settings from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import admin as auth_admin from django.contrib.auth import admin as auth_admin
from django.contrib.auth import decorators from django.contrib.auth.decorators import login_required
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from {{ cookiecutter.project_slug }}.users.forms import UserAdminChangeForm from {{ cookiecutter.project_slug }}.users.forms import UserAdminChangeForm
from {{ cookiecutter.project_slug }}.users.forms import UserAdminCreationForm from {{ cookiecutter.project_slug }}.users.forms import UserAdminCreationForm
from {{ cookiecutter.project_slug }}.users.models import User
User = get_user_model()
if settings.DJANGO_ADMIN_FORCE_ALLAUTH: if settings.DJANGO_ADMIN_FORCE_ALLAUTH:
# Force the `admin` sign in process to go through the `django-allauth` workflow: # Force the `admin` sign in process to go through the `django-allauth` workflow:
# https://docs.allauth.org/en/latest/common/admin.html#admin # https://docs.allauth.org/en/latest/common/admin.html#admin
admin.site.login = decorators.login_required(admin.site.login) # type: ignore[method-assign] admin.site.login = login_required(admin.site.login) # type: ignore[method-assign]
@admin.register(User) @admin.register(User)

View File

@ -1,12 +1,9 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers from rest_framework import serializers
from {{ cookiecutter.project_slug }}.users.models import User as UserType from {{ cookiecutter.project_slug }}.users.models import User
User = get_user_model()
class UserSerializer(serializers.ModelSerializer[UserType]): class UserSerializer(serializers.ModelSerializer[User]):
class Meta: class Meta:
model = User model = User
{%- if cookiecutter.username_type == "email" %} {%- if cookiecutter.username_type == "email" %}

View File

@ -1,4 +1,3 @@
from django.contrib.auth import get_user_model
from rest_framework import status from rest_framework import status
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.mixins import ListModelMixin from rest_framework.mixins import ListModelMixin
@ -7,9 +6,9 @@ from rest_framework.mixins import UpdateModelMixin
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet from rest_framework.viewsets import GenericViewSet
from .serializers import UserSerializer from {{ cookiecutter.project_slug }}.users.models import User
User = get_user_model() from .serializers import UserSerializer
class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet): class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet):

View File

@ -1,13 +1,12 @@
from allauth.account.forms import SignupForm from allauth.account.forms import SignupForm
from allauth.socialaccount.forms import SignupForm as SocialSignupForm from allauth.socialaccount.forms import SignupForm as SocialSignupForm
from django.contrib.auth import forms as admin_forms from django.contrib.auth import forms as admin_forms
from django.contrib.auth import get_user_model
{%- if cookiecutter.username_type == "email" %} {%- if cookiecutter.username_type == "email" %}
from django.forms import EmailField from django.forms import EmailField
{%- endif %} {%- endif %}
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
User = get_user_model() from {{ cookiecutter.project_slug }}.users.models import User
class UserAdminChangeForm(admin_forms.UserChangeForm): class UserAdminChangeForm(admin_forms.UserChangeForm):

View File

@ -1,8 +1,5 @@
from django.contrib.auth import get_user_model
from config import celery_app from config import celery_app
from {{ cookiecutter.project_slug }}.users.models import User
User = get_user_model()
@celery_app.task() @celery_app.task()

View File

@ -1,11 +1,12 @@
from collections.abc import Sequence from collections.abc import Sequence
from typing import Any from typing import Any
from django.contrib.auth import get_user_model
from factory import Faker from factory import Faker
from factory import post_generation from factory import post_generation
from factory.django import DjangoModelFactory from factory.django import DjangoModelFactory
from {{ cookiecutter.project_slug }}.users.models import User
class UserFactory(DjangoModelFactory): class UserFactory(DjangoModelFactory):
{%- if cookiecutter.username_type == "username" %} {%- if cookiecutter.username_type == "username" %}
@ -38,5 +39,5 @@ class UserFactory(DjangoModelFactory):
instance.save() instance.save()
class Meta: class Meta:
model = get_user_model() model = User
django_get_or_create = ["{{cookiecutter.username_type}}"] django_get_or_create = ["{{cookiecutter.username_type}}"]

View File

@ -1,4 +1,3 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse from django.urls import reverse
@ -7,7 +6,7 @@ from django.views.generic import DetailView
from django.views.generic import RedirectView from django.views.generic import RedirectView
from django.views.generic import UpdateView from django.views.generic import UpdateView
User = get_user_model() from {{ cookiecutter.project_slug }}.users.models import User
class UserDetailView(LoginRequiredMixin, DetailView): class UserDetailView(LoginRequiredMixin, DetailView):