Opened 5 years ago

Last modified 5 years ago

#31083 closed New feature

Add select_related support for Site.objects.get_current — at Version 1

Reported by: Kris Ciccarello Owned by: nobody
Component: contrib.sites Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Kris Ciccarello)

Since we cannot extend Site, a common pattern is to use a OneToOneField to extend a Site (i.e. with a SiteDetail class). When performing request.site = get_current_site(request) in middleware, it would be nice to be able to efficiently load any extended Site objects onto request.site at that time.

Given this class:

from django.contrib.sites.models import Site

class SiteDetail(models.Model):
    site = models.OneToOneField(
        Site, on_delete=models.CASCADE, primary_key=True, related_name="detail"
    )

The following does not work:

Site.objects.select_related("detail").get_current(request)
> AttributeError: 'QuerySet' object has no attribute 'get_current'

We could add support for something like this:

Site.objects.get_current(request, select_related=["detail"])

Which would allow the user to pass through any custom Site extensions without having to re-write the code in the Site manager. Given the use of SITE_CACHE inside of SiteManager, it seems like making this addition to its API would be a good step.

Does this make sense, or would another approach be better? Happy to make a PR if this would be considered.

Change History (1)

comment:1 by Kris Ciccarello, 5 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top