我有以下代码:
for e in Contact.objects.filter(contact_owner=batch.user, group=batch.group):
msg = Message.objects.create(
recipient_number=e.mobile,
)
但是,我想做类似下面的事情,首先我检查对象是否有一个group
if not 过滤器,contact
而不是其他过滤器组。
如您所见,这不是很干燥。怎样才能做得更好?
if self.group == None:
for e in Contact.objects.filter(contact_owner=batch.user, contact=batch.group):
msg = Message.objects.create(
recipient_number=e.mobile,
)
else:
for e in Contact.objects.filter(contact_owner=batch.user, group=batch.group):
msg = Message.objects.create(
recipient_number=e.mobile,
)