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

如何在Django视图中为潜在用户获取并显示业主信息

解决Django视图中潜在用户无法正确显示房产业主信息的问题

原代码核心问题

  1. 未先获取目标房产实例就赋值property_owner,导致该值与当前查看的房产无关——房东/中介分支错误使用自身Profile,潜在用户分支直接设为None
  2. 中介分支错误将自身Profile作为property_owner,而非当前房产的房东(真正业主)
  3. 消息接收者逻辑错误,原代码用用户自身Profile作为接收方,不符合“给房产业主发消息”的业务需求

修正后的代码

def property_detail(request, property_id):
    user = request.user
    if user.is_authenticated:
        # 优先获取当前查看的房产实例,所有逻辑基于该房产展开
        property_instance = get_object_or_404(Property, pk=property_id)
        
        # 根据用户类型处理推荐房产和业主信息
        if hasattr(user, 'landlord'):
            # 房东:推荐自己的其他房产,业主为当前房产的房东(即自身)
            properties = Property.objects.filter(landlord=user.landlord).exclude(pk=property_id).prefetch_related('agent').order_by('-last_updated')[:4]
            property_owner = property_instance.landlord.profile if hasattr(property_instance.landlord, 'profile') else None
        elif hasattr(user, 'agent'):
            # 中介:推荐自己代理的其他房产,业主为当前房产的房东
            properties = Property.objects.filter(agent=user.agent).exclude(pk=property_id).prefetch_related('landlord').order_by('-last_updated')[:4]
            property_owner = property_instance.landlord.profile if hasattr(property_instance.landlord, 'profile') else None
        else:
            # 潜在用户:推荐最新房产,按需求隐藏业主信息(设为None)
            properties = Property.objects.exclude(pk=property_id).order_by('-last_updated')[:4]
            property_owner = None  # 若需让潜在用户看到业主,替换为 property_instance.landlord.profile if hasattr(...) else None

        # 处理消息发送逻辑
        if request.method == 'POST':
            form = MessageForm(request.POST)
            if form.is_valid():
                content = form.cleaned_data['content']
                subject = form.cleaned_data['subject']
                # 消息接收者为当前房产的房东用户
                recipient = property_instance.landlord.user if hasattr(property_instance.landlord, 'user') else None
                Message.objects.create(
                    property=property_instance,
                    sender=user,
                    recipient=recipient,
                    subject=subject,
                    content=content
                )
                messages.success(request, '通知已成功发送,包含您的联系方式。')
        else:
            form = MessageForm()
        
        context = {
            'form': form,
            'properties': properties,
            'property_owner': property_owner,
            'page_title': '房产详情',
            'property': property_instance,
        }
        return render(request, 'realestate/landlord_agent_property.html', context)
    # 未登录用户处理
    logout(request)
    messages.warning(request, '会话已过期,请重新登录。')
    return redirect(reverse('account-login'))

关键修正说明

  • 业主信息逻辑:所有property_owner赋值都基于当前房产的landlord关联模型,确保显示的是该房产的真实业主
  • 推荐房产优化:添加exclude(pk=property_id)避免重复推荐当前查看的房产
  • 消息接收者修正:将接收方改为房产房东的User实例,符合“给业主发消息”的业务逻辑
  • 潜在用户控制:保留property_owner = None满足需求,若需开放业主信息,替换为对应房产的业主Profile即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 09:49:55