karlheinz_sch
karlheinz_sch

Reputation: 500

two QuerySets - merge/join on common foreign key of third model

I am now querying - independent of each other - the Models Price and Duration (scroll down for the model definition) through several filters. Finally, I want to merge Price and Duration Querysets so that one price is associated with the specific duration. Example:

Price - in QuerySet _prices:

id | provider | servicelevel | price | currency
1  | 1        | 1            | 10    | 1
2  | 1        | 1            | 20    | 2
3  | 2        | 2            | 15    | 1

Duration - in QuerySet _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'

What I need to have: Price + Duration joined on 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'

I've played around with annotate(), extra(), but did not find a solution.

Models:

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

Upvotes: 0

Views: 1564

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174624

I have two QuerySets: One of Price, and one of Duration. In the Price QuerySet, there are n prices with one Servicelevel each. In the Duration QuerySet, there are n rows with Servicelevel unique. I want to add the data of the Duration QuerySet to the Price QuerySet

I think as this is for display purposes, you need to restructure your data:

results = {}

for p in price_queryset:
   results.setdefault(p.pk, []).append(p)
   for d in duration_queryset.filter(servicelevel=p.servicelevel):
       results[p.pk].append(d)

Upvotes: 2

jpwagner
jpwagner

Reputation: 553

If Duration and Servicelevel are one-to-one, then you could refactor your data model. If they are not, do you want every permutation in the final report?

Check out the _set "backward" relationship manager:

https://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

Upvotes: 0

Related Questions