0

I have a Model that I want to only link to one site. I'll use the documentation example:

from django.db import models
from django.contrib.sites.models import Site

class Article(models.Model):
    # ...
    site = models.ForeignKey(Site)

But in the admin for each site, all the objects show up, regardless of their site setting. I want to:

  • Limit the admin changelist dataset to instances where the site is the current site
  • Automatically set the new form with the current site set (and optionally hide it).

To complicate things, the model is also a adminsortable.Sortable but I don't forsee that causing serious issues here.

Oli
  • 235,628
  • 64
  • 220
  • 299

1 Answers1

1

The first part is quite easy. We can customise the queryset on the ModelAdmin (subclassed by StortableAdmin here).

from django.contrib.sites.shortcuts import get_current_site

@admin.register(Article)
class ArticleAdmin(SortableAdmin):
    def queryset(self, request):
        return super(ArticleAdmin, self).queryset(request).filter(
            site=get_current_site(request)
        )

This doesn't handle the default values but is enough for me right now.

Oli
  • 235,628
  • 64
  • 220
  • 299