You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

django rest-auth自定义用户模型报错:RelatedObjectDoesNotExist

解决django-rest-auth注册时未自动创建UserProfile的问题

这个问题我之前帮好几个开发者排查过——本质是django-rest-auth默认的注册流程只会创建User实例,不会自动生成关联的UserProfile,所以当你后续尝试访问user.userprofile时就会抛出RelatedObjectDoesNotExist错误。下面给你两种最实用的解决思路,你可以根据项目需求选择:

方法一:使用Django信号自动创建Profile(推荐,侵入性低)

这是最通用的方案,不需要修改rest-auth的核心逻辑,利用Django的post_save信号,在User被创建的同时自动生成对应的UserProfile。

  1. 在你的UserProfile模型文件中添加信号接收器
    打开你定义UserProfile的models.py,加入以下代码:

    from django.db import models
    from django.contrib.auth.models import User
    from django.db.models.signals import post_save
    from django.dispatch import receiver
    
    class UserProfile(models.Model):
        user = models.OneToOneField(
            User, 
            on_delete=models.CASCADE, 
            related_name='userprofile'  # 这个related_name要和你报错里的名称对应
        )
        # 这里是你的其他字段,比如:
        # avatar = models.ImageField(upload_to='avatars/', blank=True)
        # bio = models.TextField(max_length=500, blank=True)
    
    # 当User被创建时,自动创建UserProfile
    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            UserProfile.objects.create(user=instance)
    
    # 当User被保存时,同步保存UserProfile
    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.userprofile.save()
    
  2. 处理已存在的无Profile的用户(可选)
    如果你的数据库里已经有一些没有对应UserProfile的老用户,需要先手动补全:
    打开Django Shell(python manage.py shell),执行:

    from django.contrib.auth.models import User
    from your_app.models import UserProfile
    
    for user in User.objects.all():
        UserProfile.objects.get_or_create(user=user)
    

方法二:自定义注册序列化器(适合需要在注册时收集Profile字段的场景)

如果你希望用户在注册时就能填写Profile的相关信息(比如头像、简介),可以通过重写rest-auth的注册序列化器,在创建User的同时直接创建并配置UserProfile。

  1. 创建自定义注册序列化器
    在你的项目中新建serializers.py,写入:

    from rest_auth.registration.serializers import RegisterSerializer
    from rest_framework import serializers
    from your_app.models import UserProfile
    
    class CustomRegisterSerializer(RegisterSerializer):
        # 如果你需要在注册时接收Profile字段,在这里添加,比如:
        # bio = serializers.CharField(required=False, max_length=500)
    
        def save(self, request):
            # 先调用父类方法创建User
            user = super().save(request)
            # 创建关联的UserProfile
            profile = UserProfile.objects.create(user=user)
            # 如果有自定义字段,在这里赋值并保存
            # profile.bio = self.validated_data.get('bio', '')
            # profile.save()
            return user
    
  2. 配置rest-auth使用自定义序列化器
    在项目的settings.py中添加:

    REST_AUTH_REGISTER_SERIALIZERS = {
        'REGISTER_SERIALIZER': 'your_project_name.serializers.CustomRegisterSerializer',
    }
    

关键注意点

  • 确保UserProfile中OneToOneField的related_name设置为userprofile,和你报错信息中的字段名一致,否则访问user.userprofile会出错。
  • 如果使用信号方案,记得确保信号被正确加载——如果把信号放在单独的signals.py文件中,需要在app的apps.py的ready()方法中导入信号模块。

内容的提问来源于stack exchange,提问作者Vasile

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:29:08