4

Little new to the Django way of doing things, but I have a series of loops that I am sure can be done more efficiently. Essentially I have a group of related objects that I need to drill down the graph to retrieve the values from the end related object. I am looping down the objects to eventually get the values I want. Please see below code:

            for media in campaign.media_set.all():
                media_key = media.key
                for target in media.target_set.all():
                    x = target.x
                    y = target.y
                    target_id = target.id
                    for metatag in target.metatag_set.all():
                        body = metatag.body
                        hdr = metatag.header
                        ftr = metatag.footer
                        ct_url = metatag.clickthrough
                        point_url = metatag.point.image_file

Any pointers on a way to do this in less DB selects?

3 Answers 3

4

Yes, you're going about it backwards. This is a problem I've had many times when working with Django. It's easiest to start with the object you want to retrieve and work backwards to meet all the conditions. i.e., it looks like you want some information about this metatag, so start with that.

MetaTag.objects.filter(conditions_here)

And then to retrieve the other stuff like this target x/y just use the automatic related_name properties that are created on the MetaTag object. It's hard to say exactly without seeing your models.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thanks for the advice. This boiled down to: MetaTag.objects.filter(target__media__campaign__in=campaigns):
1

In general this is a great read: https://docs.djangoproject.com/en/dev/topics/db/optimization/

Possibly just adding a select_related before your initial search may help significantly.

1 Comment

select_related would do nothing in this case. He's following foreignkeys backwards in his loops select_related helps if you're following them forward
0

This is pretty much impossible to answer without seeing the model definitions. If these are ForiegnKey relationships than you can most likely eliminate the outer loops altogether and just generate all your data by following the foreign keys up (with select_related of course to get all the data at once)

However more than likely we're looking at manytomany relationships and for that you can't just navigate back up the foreign keys and the answer you have may be the best you can do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.