1

I am trying to develop a ModelManager with an operation similar to the Sites Framework. Depending on a user's field, the ModelManager returns a queryset. I tried to imitate the operation of the Sites Framework but I do not understand how the SITE_ID is obtained dynamically with this function:

    def get_queryset(self):
    return super(CurrentSiteManager, self).get_queryset().filter(
        **{self._get_field_name() + '__id': settings.SITE_ID})

It seems to be static :/.

I capture the user's field through a Middleware and assign it to request.field. How can I retrieve that field in the ModelManager and perform the query?

paralosreg
  • 141
  • 1
  • 15

1 Answers1

0

I think you are missing the dynamic way to get the current site instance. From the documentation example:

from django.contrib.sites.shortcuts import get_current_site

def article_detail(request, article_id):
    try:
        a = Article.objects.get(id=article_id, 
            sites__id=get_current_site(request).id)
    except Article.DoesNotExist:
        raise Http404("Article does not exist on this site")
    # ...

You should use the get_current_site method to get the current site.

It should be noted that it's not working if you actually define the current site in the settings like SITE_ID=1.

It looks up the current site based on request.get_host() if the SITE_ID setting is not defined.

You should read this part of the documentation actually explaining how django can get the current site in a dynamic fashion:

shortcuts.get_current_site(request)

A function that checks if django.contrib.sites is installed and returns either the current Site object or a RequestSite object based on the request. It looks up the current site based on request.get_host() if the SITE_ID setting is not defined.

Both a domain and a port may be returned by request.get_host() when the Host header has a port explicitly specified, e.g. example.com:80. In such cases, if the lookup fails because the host does not match a record in the database, the port is stripped and the lookup is retried with the domain part only. This does not apply to RequestSite which will always use the unmodified host.

And here is the code of the get_current actually called by get_current_site:

def get_current(self, request=None):
    """
    Return the current Site based on the SITE_ID in the project's settings.
    If SITE_ID isn't defined, return the site with domain matching
    request.get_host(). The ``Site`` object is cached the first time it's
    retrieved from the database.
    """
    from django.conf import settings
    if getattr(settings, 'SITE_ID', ''):
        site_id = settings.SITE_ID
        return self._get_site_by_id(site_id)
    elif request:
        return self._get_site_by_request(request)

    raise ImproperlyConfigured(
        "You're using the Django \"sites framework\" without having "
        "set the SITE_ID setting. Create a site in your database and "
        "set the SITE_ID setting or pass a request to "
        "Site.objects.get_current() to fix this error."
    )
Community
  • 1
  • 1
Clément Denoix
  • 1,504
  • 11
  • 18
  • Ok. This function is dynamic. But how it made dynamic the CurrentSiteManager??? https://docs.djangoproject.com/en/1.11/ref/contrib/sites/#django.contrib.sites.managers.CurrentSiteManager – paralosreg Nov 16 '17 at 09:05
  • Your question title is "How sites framework works?" and in the details of your question you wrote "I do not understand how the SITE_ID is obtained dynamically with this function:" and "It seems to be static :/." I answer both of theses questions... – Clément Denoix Nov 16 '17 at 09:10
  • is obtained dynamically... with this fuction. The function is in CurrentSiteManager. CurrentSiteManager returns allways the static SITE_ID var? – paralosreg Nov 16 '17 at 11:22