无法导入名称get_user_model

时间:2014-02-01 12:13:49

标签: python django django-admin django-registration

我使用django-registrations,而我在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)

我收到错误:

" cannot import name get_user_model "
in admin.py

我做错了什么?

1 个答案:

答案 0 :(得分:4)

Django get_user_model中有{p> version >= 1.5,您可能正在运行Django version < 1.5。升级Django,问题就会消失。

或者使用它代替Django版本&lt; 1.5:

from django.contrib.auth.models import User