我现在查询 - 相互独立 - 型号价格和持续时间(向下滚动模型定义)通过几个过滤器。 最后,我要合并的价格和持续时间的查询集,这样一个价格是与特定时间有关。 例:
价格 - 在查询集_prices:
id | provider | servicelevel | price | currency
1 | 1 | 1 | 10 | 1
2 | 1 | 1 | 20 | 2
3 | 2 | 2 | 15 | 1
持续时间 - 在查询集_duration:
id | servicelevel | country_in | country_out | duration | duration_details
1 | 1 | 83 | 236 | 5 | 'only on working days'
2 | 2 | 83 | 236 | 3 | 'Mon-Thu'
我需要有:价格+时间加入了对Servicelevel
id | provider | servicelevel | price | currency | duration | duration_details
1 | 1 | 1 | 10 | 1 | 5 | 'only on working days'
2 | 1 | 1 | 20 | 2 | 5 | 'only on working days'
3 | 2 | 2 | 15 | 1 | 3 | 'Mon-Thu'
我周围玩annotate()
extra()
但没有找到一个解决方案。
楷模:
class Price(models.Model):
...
provider = models.ForeignKey(Provider)
servicelevel = models.ForeignKey(Servicelevel)
price = models.DecimalField(max_digits=8, decimal_places=2)
currency = models.ForeignKey('api.Currency')
...
class Servicelevel(models.Model):
name = models.CharField(max_length=200)
provider = models.ForeignKey(Provider)
terms = models.TextField(blank=True)
insurance = models.BooleanField(default=False)
...
class Duration(models.Model):
servicelevel = models.ForeignKey(Servicelevel)
country_out = models.ForeignKey('Country', related_name='duration_out_country_relation', null=True)
country_in = models.ForeignKey('Country', related_name='duration_out_country_relation', null=True)
duration = models.IntegerField()
duration_details = models.TextField(blank=True)
...
KH