I have the following models and I'm trying to work out how to do backward relationships.
I want a distinct CostRate queryset showing which costrates are associated with a particular SalesEvent. The CostFixedList has all the sales that occurred on the different days.
So I'd filter the CostFixedList for a particular SalesEvent, create a list of the [distinct] cost rates it has and then match that up with the CostRate model.
I could do this easily in SQL, but can't get my head around how to do it [or even start] in ORM.
class SalesEvent(models.Model):
event_type = models.ForeignKey(EventType, verbose_name="Event Type")
retailer = models.ForeignKey(Retailer, verbose_name="Retailer")
....
class CostRate(models.Model):
cost_item = models.ForeignKey(CostItem, verbose_name="Item")
valid_from = models.DateField("From")
valid_till = models.DateField("Till")
unit_amount = models.DecimalField("Price Per Unit", max_digits=5, decimal_places=2)
class CostFixedList(models.Model):
sales_event = models.ForeignKey(SalesEvent)
cost_rate = models.ForeignKey(CostRate)
units = models.IntegerField()
appointment = models.ForeignKey(Appointment, null=True, blank=True)