Show all model instances regardless of site in django admin

110 views Asked by At

I've enabled the sites framework (including the middleware) and setup two sites (ID=1 and ID=2). One of my models uses a foreign key to Site. For arguments sake, lets say it looks like this

class Person(models.Model):
    name = models.CharField(max_length=20)
    site = models.ForeignKey(Site)
    on_site = CurrentSiteManager()

In my settings.py I have SITE_ID = 1

If I create an instance of person in the admin screens, and set its site to the site with ID=2, it IS created, but it is hidden in the current admin screen.

This seems counterintuitive to me. I want to use my single django admin screen to manage my two sites. In my list_display I have 'site', so if it showed me all the instances I'd known which site it was related to.

Is there some way to ensure that in the admin screens ALL instances of my model are shown, regardless of Site ID?

I do not want to have to login to two separate admin URLs to manage all the model instances.

1

There are 1 answers

1
doniyor On BEST ANSWER

you actually dont need CurrentSiteManager, simply connect the model to Site Model thru ForeignKey like;

from django.contrib.sites.models import Site
class Person(models.Model):
   name = models.CharField(max_length=20)
   site = models.ForeignKey(Site)
   on_site = models.ForeignKey(Site,related_name="site_users")

and in admin, you will see a dropdown to choose to which site the Person should belong to.

dont forget to migrate the model since you are changing the schema