我有4个字段,主要电话(p1),次要电话(p2),主要电子邮件(e1),次要电子邮件(e2)。 如果p2字段为空,则新行的p1值应转到p2。
我能够合并P编号字段,但是无法合并P电子邮件和SE电子邮件 请找到以下代码:
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:
old_contact.secondary_phone_number = contact.primary_phone_number
old_contact.secondary_email = contact.primary_email
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):
query = Q(secondary_phone_number=obj.primary_phone_number)
if obj.secondary_phone_number:
query = query | Q(primary_phone_number=obj.secondary_phone_number)
if obj.primary_email:
query = query | Q(primary_email=obj.primary_email)
if obj.secondary_email:
query = query | Q(secondary_email=obj.secondary_email)
return query