Saturday, June 18, 2016

Re: Prefetch Related with Multiple Column Join

Hi Ram,

I don't have a complete solution for you but I suggest you take a look at the
django.db.models.fields.related.ForeignObject class.

If I'm not mistaken it should allow you to define multi-column relationships
on both your Article and PublisherMediumBilling models and refer to them using
the `select_related()` and `prefetch_related()``API while the later uses Python
to perform in-memory JOINs.

class Article(models.Model):
    publisher = ForeignKey(Publisher, related_name='articles')
    medium = ForeignKey(Medium)
    billing = ForeignObject(
        'PublisherMediumBilling', from_fields=['publisher', 'medium'], to_fields=['publisher', 'medium']
    )

publisher.articles.select_related('billing')

Cheers,
Simon

Le samedi 18 juin 2016 07:09:03 UTC-4, Ram Jayaraman a écrit :
I apologize if this is an old question, I did search SO/google for a good few hours.

I have the following models.

Publisher()
  name = CharField()

Medium()
  name = CharField()

Article()
  publisher = ForeignKey(Publisher, related_name="articles")
  medium = ForeignKey(Medium, related_name="articles")
  text = TextField()

PublisherMediumBilling()
  publisher = ForeignKey(Publisher()
  medium = ForeignKey(Article)
  lots_of_other_columns
 ...
 
class Meta:
      unique_together
= ['publisher', 'medium']



I am looking to query all Article from a given publisher, and also fetch the related PublisherMediumBilling joining on the columns Publisher and Medium, Prefetch() object doesn't seem to quite work for this case.

I can of course filter-query for all PublisherMediumBilling and cache it and do a in-memory Python JOIN. Is there a way to do through one of the ORM Apis ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7d2176cb-8cba-4087-bf22-5529430c0b9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment