cannot import name get_user_model

点点圈 提交于 2021-01-27 05:22:41

问题


I use django-registrations and while I add this code in my admin.py

   from django.contrib import admin
   from customer.models import Customer
   from .models import UserProfile
   from django.contrib.auth.admin import UserAdmin
   from django.contrib.auth import get_user_model

   class UserProfileInline(admin.StackedInline):
       model = UserProfile
       can_delete = False

   class UserProfileAdmin(UserAdmin):
       inlines=(UserProfileInline, )

   admin.site.unregister(get_user_model())
   admin.site.register(get_user_model(), UserProfileAdmin)
   admin.site.register(Customer)

I receive an error:

" cannot import name get_user_model "
in admin.py

what am I doing wrong?


回答1:


get_user_model is available in Django version >= 1.5 you are probably running Django version < 1.5. Upgrade the Django and the problem will be gone.

Or use this instead for Django version < 1.5:

from django.contrib.auth.models import User


来源:https://stackoverflow.com/questions/21497883/cannot-import-name-get-user-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!