有没有最好的方法来创建自定义用户模型并扩展到 Django 中的生产配置文件?因为我在生产中不断遇到这些错误

Is there a best way to create a custom user model and extend to Profile for production in django? Because I keep getting these error in production

您好,有人可以突出显示将自定义用户模型扩展到配置文件的最佳方法或解释我做错了什么吗?我创建了一个自定义用户模型,并将其扩展到一个配置文件应用程序,该应用程序一切正常并通过了开发。然而,在 heroku 的生产环境中,当我点击配置文件时,我一直 运行 进入服务器错误,所以我将 debug 设置为 True 并且它看到了这个回溯;

ProgrammingError at /u/profile/
can't adapt type 'MyUser'

所以我想,嘿,这可能是在抱怨传递整个模型而不是模型中的对象。但是,我的视图中的过滤器将检索到的对象指定为与请求的用户相关的对象。以下是我的观点:

User = get_user_model()

class ProfileUserView(View):
    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated():
           user = get_object_or_404(User, username=request.user)
           post_list = Post.objects.filter(author__username__iexact=user).order_by('-submission_time')
           paginator = Paginator(post_list, 10)
           page = request.GET.get('page')

           try:
                post = paginator.page(page)
           except PageNotAnInteger:
                post = paginator.page(1)
           except EmptyPage:
                post = paginator.page(paginator.num_pages)
           profile = Profile.objects.get(user=request.user)
           template = 'profiles/profile_user.html'
           context = {
                    'profile': profile,
                    'post': post
                   }
           return render(request, template, context)
        else:
           return HttpResponseRedirect(reverse('account_login'))

任何 help/explanation 关于我在通过扩展自定义用户模型从配置文件模型中检索关联数据时做错了什么,将不胜感激。

这是配置文件模型块;

class Profile(models.Model):
    user = models.OneToOneField(User)
    image = models.ImageField(upload_to='images/', null=True, blank=True)
    rank = models.CharField(max_length=50, default="Beginner")

这是完整的回溯

请求方法:GET 请求 URL:https://sustenergy.herokuapp.com/u/profile/

回溯:

validate_number 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py” 41.数字=整数(数字)

在处理上述异常时(int() 参数必须是字符串、类字节对象或数字,不能'NoneType'),又发生异常:

获取文件“/app/profiles/views.py” 37. post = paginator.page(页数)

页面中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py” 57.数字=self.validate_number(数字)

validate_number 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py” 43. 提高 PageNotAnInteger(_('That page number is not an integer'))

在处理上述异常时(该页码不是整数),又出现异常:

执行文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py” 65.returnself.cursor.execute(sql,参数)

上述异常(无法适配类型 'MyUser')是以下异常的直接原因:

内部文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py” 41.响应=get_response(请求)

_get_response 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py” 187. 响应 = self.process_exception_by_middleware(e, 请求)

_get_response 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py” 185.响应=wrapped_callback(请求,*callback_args,**callback_kwargs)

视图中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/base.py” 68. return self.dispatch(请求,*args,**kwargs)

调度文件“/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/base.py” 88. return 处理程序(请求,*args,**kwargs)

获取文件“/app/profiles/views.py” 39.post=paginator.page(1)

页面中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py” 57.数字=self.validate_number(数字)

validate_number 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py” 46. 如果数字 > self.num_pages:

get 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py” 35. res = instance.dict[self.name] = self.func(实例)

num_pages 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py” 91. 如果 self.count == 0 而不是 self.allow_empty_first_page:

get 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py” 35. res = instance.dict[self.name] = self.func(实例)

文件 "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" 计数 79.returnself.object_list.count()

文件 "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" 计数 364.returnself.query.get_count(using=self.db)

get_count 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py” 499. number = obj.get_aggregation(using, ['__count'])['__count']

get_aggregation 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py” 480.结果=compiler.execute_sql(单)

execute_sql 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py” 886.提高original_exception

execute_sql 中的文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py” 876.cursor.execute(sql, 参数)

执行文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py” 80. return super(CursorDebugWrapper, self).execute(sql, 参数)

执行文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py” 65.returnself.cursor.execute(sql,参数)

文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py”在退出 94. six.reraise(dj_exc_type, dj_exc_value, 回溯)

重新加注文件“/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py” 685.提高value.with_traceback(tb)

执行文件“/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py” 65.returnself.cursor.execute(sql,参数)

异常类型:/u/profile/ 处的编程错误 异常值:无法适配类型 'MyUser'

在您的自定义模型中继承 AbstractBaseUser django 内置模型,并在模型中创建必填字段。检查下面自定义用户模型的 django 示例

https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#django.contrib.auth.models.CustomUser

行中需要更改,

user = get_object_or_404(User, username=request.user.username)
                                                    #^^^^^^^^^^
post_list = Post.objects.filter(author__username__iexact=user.username).order_by('-submission_time')
                                                              #^^^^^^^