class Supplier(Model) :
pass
class Customer(Model) :
pass
class Dock(Model) :
pass
class SupplierDockAccess(Model) :
supplier = ForeignKey(Supplier)
dock = ForeignKey(Dock)
class SupplierCustomerAccess(Model):
supplier = ForeignKey(Supplier)
customer = ForeignKey(Customer)
我有客戶的實例,我想獲得的所有碼頭,客戶可以訪問。客戶可以通過SupplierCustomerAccess訪問供應商,供應商可以通過SupplierDockAccess訪問Docks。我能做到這一點,像這樣:
# get the suppliers the customer has access to
supplier_customer_accesses = SupplierCustomerAccess.objects.filter(customer=customer)
suppliers = [s.supplier for s in supplier_customer_accesses]
# get the docks those suppliers have access to
supplier_dock_accesses = SupplierDockAccess.objects.filter(supplier__in=suppliers)
docks = [s.dock for s in supplier_dock_accesses]
...但隨後產生的碼頭的清單包含重複的,我真的認爲這oughtta有可能做到這一點一氣呵成。任何人都想展示一些強大的django-fu?
這也限制你的JOIN到兩個表,而不是像OP的答案那樣。 – 2010-08-11 16:22:04