比方說,我有這樣的模式:如何在Django或SQL中查詢非唯一值?
class Contact(BaseModel):
order = models.ForeignKey(Order, related_name='contacts', blank=True, null=True)
type = models.CharField(max_length=15, choices=TYPES, blank=True))
我想找到所有的訂單,其中order
和type
不是唯一的一起。
例如,有order A
並有相關聯繫人:
Contact(order=orderA, type='broker')
Contact(order=orderA, type='broker')
Contact(order=orderA, type='delivery')
我想找到這個orderA
因爲這種秩序和type='broker'
在一起並不是唯一在Contact
模型。
再有就是orderB
和這些相關的聯繫人:
Contact(order=orderB, type='broker')
Contact(order=orderB, type='delivery')
我不想要這個orderB
,因爲它和現場type
在Contact
模式是獨一無二的。
我嘗試使用Django的annonate()
但未能涉及這兩個領域。
Django查詢可以做到這一點嗎?
如果不是,我將不勝感激。
非常感謝。
'order'應該是'order_id',你應該添加別名的子查詢,但除此之外,這應該工作。 –
@rd_nielsen謝謝。如果您通過Назар建議更正,我會接受它。謝謝你們兩位。 –