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

Django Allauth处理重复邮箱登录报错:AttributeError问题排查

Fixing the AttributeError: 'QuerySet' object has no attribute 'profile'

Hey there, let's break down what's causing this error and fix it step by step.

The Root Cause

Your line users = User.objects.all().filter(email=email_address) returns a QuerySet (a collection of User objects), even if only one user matches the email. When you try to access users.profile, you're trying to get a profile attribute from the entire collection—not a single User instance. That's why you're hitting the AttributeError.

How to Fix It

Since you already check if users.exists() to confirm there's at least one matching user, you can grab the first (and presumably only) user from the QuerySet using .first(). Then you can safely access their profile attribute.

Here's the updated version of your handleDuplicateEmail receiver, including your email fallback logic for phone-based social accounts:

from django.contrib.auth.models import User
from django.dispatch import receiver
from social_core.signals import pre_social_login
from django.http import ImmediateHttpResponse
from django.shortcuts import render

@receiver(pre_social_login)
def handleDuplicateEmail(sender, request, sociallogin, **kwargs):
    # Get email from social account, handle cases where email isn't available
    if sociallogin.account.provider in ['facebook', 'twitter']:
        try:
            email_address = sociallogin.account.extra_data['email']
        except KeyError:
            email_address = None
    else:  # LinkedIn case
        try:
            email_address = sociallogin.account.extra_data['email-address']
        except KeyError:
            email_address = None

    if email_address:
        # Get all users with matching email (should be 0 or 1 if email is unique)
        users = User.objects.filter(email=email_address)
        if users.exists():
            # Grab the first matching user (use .get() if email is unique in DB)
            existing_user = users.first()
            # Check if the existing user's provider matches the current social login
            if existing_user.profile.provider != sociallogin.account.provider:
                response = 'Your social account email address is already registered to some account. Please choose a different one.'
                raise ImmediateHttpResponse(render(request, 'index.html', {'type': True, 'response': response}))
    else:
        # No email provided in social account
        response = 'You have not provided an email address in your social account. Please register as local user or use a different social account.'
        raise ImmediateHttpResponse(render(request, 'index.html', {'type': True, 'response': response}))

Extra Tips to Avoid Future Issues

  • Enforce Email Uniqueness: Make sure your User model's email field is set to unique=True (if it isn't already). This ensures only one user can have a given email, so .first() will always return the correct user, and you can even use .get(email=email_address) directly (just wrap it in a try/except for User.DoesNotExist if you skip the exists() check).
  • Ensure Profile Exists for All Users: If you haven't already, add a signal to automatically create a Profile instance whenever a new User is created. This prevents errors where a User might not have a linked Profile:
    from django.db.models.signals import post_save
    from django.dispatch import receiver
    
    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)
    
    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.profile.save()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:38:23