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

联系人重复项合并问题:邮箱字段合并失效求助(附代码)

Fixing Email Merging Failure in Contact Duplicate Handling

Let's break down why your email merging isn't working and fix it step by step.

Root Cause

Looking at your query_params method, the phone number logic handles bidirectional matches (new contact's primary phone matches old's secondary, and vice versa when secondary exists), but the email logic only checks exact one-way matches:

  • It only looks for old contacts where their primary_email equals the new contact's primary_email
  • Or their secondary_email equals the new contact's secondary_email

This means cases like:

  • New contact's primary_email matches old contact's secondary_email
  • New contact's secondary_email matches old contact's primary_email

...won't trigger a match, so the email merging code never runs for these scenarios.

Fixed Code

Here's the updated version of your methods with the email logic aligned to the phone number's bidirectional matching pattern:

def merge_or_delete_contact(self, contact, contact_list):
    query_params = self.query_params(contact)
    if len(query_params):
        old_contact = contact_list.contacts.filter(query_params).exclude(id=contact.id).first()
        if old_contact:
            # Handle phone merging as before
            old_contact.secondary_phone_number = contact.primary_phone_number
            # Align email merging with your stated phone rule: fill secondary email if empty with new primary
            if not old_contact.secondary_email:
                old_contact.secondary_email = contact.primary_email
            # Keep name merging logic
            old_contact.name = contact.name if contact.name else old_contact.name
            contact.delete()
            old_contact.save()
            return old_contact
    return contact

def query_params(self, obj):
    # Start with phone number query logic (unchanged)
    query = Q(secondary_phone_number=obj.primary_phone_number)
    if obj.secondary_phone_number:
        query = query | Q(primary_phone_number=obj.secondary_phone_number)
    
    # Updated email query logic to handle bidirectional matches
    if obj.primary_email:
        # Match old's primary OR secondary email to new's primary email
        query = query | Q(primary_email=obj.primary_email) | Q(secondary_email=obj.primary_email)
    if obj.secondary_email:
        # Match old's primary OR secondary email to new's secondary email
        query = query | Q(primary_email=obj.secondary_email) | Q(secondary_email=obj.secondary_email)
    
    return query

Key Changes Explained

  1. Email Query Logic Overhaul:

    • For the new contact's primary_email, we now check if it matches either the old contact's primary_email OR secondary_email
    • For the new contact's secondary_email, we check if it matches either the old contact's primary_email OR secondary_email
    • This mirrors the phone number's matching behavior, ensuring all potential email duplicates are detected
  2. Email Merging Alignment with Your Rule:

    • I adjusted the email merging line to follow your stated phone rule: "若p2字段为空,新行的p1值需填入p2" (If secondary field is empty, fill with new row's primary value). Now it only sets old_contact.secondary_email if it's currently empty. If you intended to always overwrite the secondary email regardless of its current value, you can revert this line back to old_contact.secondary_email = contact.primary_email.

Testing Tips

  • Test cases where:
    • New contact's primary email matches old contact's secondary email
    • New contact's secondary email matches old contact's primary email
    • Old contact's secondary email is empty (to verify the fill logic works)
    • Both contacts have matching primary emails (existing case that should still work)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:14:09