Skip to content

If order_with_respect_to is a foreign key of a foreign key it generates aditional queries. #293

@rafammpp

Description

@rafammpp

Example

class Site(models.Model):
    ...

class Category(models.Model):
    site = models.ForeignKey(Site)

class Article(OrderedModel):
    order_with_respect_to = 'category__site'
    category = models.ForeignKey(Category)

If I do a queryset of Articles, it make an aditional query for every article, even if I use Article.objects.select_related('category__site').

@ibaguio manage to fix this problem in c4630dc for a simple foreign key, but if you order with respect to a foreign key of a foreign key it makes a query to retrieve the category.

If I understand it well, you save the initial value of the wrt_map to the instance at init time to check for changes later on save, and then do the proper arragments if needed. wrt_map is a dict of every item in order_with_respect_to with it's respective values. But you only need it on save. My proposal is to get the saved value from the db, and then check if it's different from the current instance to be saved. Yes, we make an aditional query, but only when we need it (on save) and can be the cheapest query if we defer all values other than the pk.

I've a view that list articles. Currently it makes over a thousand queries, and with this patch implemented, or with the previous version of this package, it gets down to eight.

I'm gonna make a pull request with my proposal, see you there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions