我有以下代碼:如何使示例代碼更幹?
for e in Contact.objects.filter(contact_owner=batch.user, group=batch.group):
msg = Message.objects.create(
recipient_number=e.mobile,
)
不過,我想這樣做下面我檢查,其中第一,如果對象具有group
如果不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,
)
無論你的代碼塊都做同樣的事情,那麼爲什麼你有,如果檢查? –
@BurhanKhalid它在過濾的參數上有所不同。 –
@Burhan Khalid過濾contact = batch.group另一個是group = batch.group,其他的都是一樣的。 – GrantU