簡單的場景,但無法弄清楚如何爲它查詢:基本的Django查詢
我們有一個Store
模型和Transaction
模型。 Foreign Key
在Transaction
模型有關回Store
。
我想查詢的列表:已完成至少一個交易的那一天只有商店。所有其他商店應排除在外。
Store.objects.filter(transaction__gt=0, transaction__date_created__gt='2016-06-01')
當我試圖前者查詢,我得到了一個長長的清單回:
[<Store: TrialStore>, <Store: TrialStore>, <Store: TrialStore>, ... ]
它幾乎一樣,如果它的上市店裏每次交易的一個實例。我想要回來的是每個商店的列表,該列表已完成至少一天的交易。
現在,數據庫中只有一個商店,所以我應該只能得到一個結果。
編輯
Store
型號:
class Store(models.Model):
status = models.IntegerField(choices=status_choices, default=ACTIVE_STATUS)
legal_name = models.TextField(verbose_name='Legal Name')
mobile_number = PhoneNumberField(blank=True)
email_address = models.EmailField(blank=True)
Transaction
型號:
class Transaction(models.Model):
store = models.ForeignKey(Store)
date_created = models.DateTimeField(auto_now_add=True, verbose_name='Created')
status = models.IntegerField(choices=status_choices)
燦你請示範你的模特? –
新增型號代碼 – qarthandso