联系人重复项合并问题:邮箱字段合并失效求助(附代码)
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_emailequals the new contact'sprimary_email - Or their
secondary_emailequals the new contact'ssecondary_email
This means cases like:
- New contact's
primary_emailmatches old contact'ssecondary_email - New contact's
secondary_emailmatches old contact'sprimary_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
Email Query Logic Overhaul:
- For the new contact's
primary_email, we now check if it matches either the old contact'sprimary_emailORsecondary_email - For the new contact's
secondary_email, we check if it matches either the old contact'sprimary_emailORsecondary_email - This mirrors the phone number's matching behavior, ensuring all potential email duplicates are detected
- For the new contact's
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_emailif it's currently empty. If you intended to always overwrite the secondary email regardless of its current value, you can revert this line back toold_contact.secondary_email = contact.primary_email.
- 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
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
相关产品推荐
相关产品推荐

