What is the best approach to extending the Site model in django? Creating a new model and ForeignKey the Site or there another approach that allows me to subclass the Site model
As of Django 2.2 there still no simple straight way to extend Site
as can be done for User
. Best way to do it now is to create new entity and put parameters there. This is the only way if you want to leverage existing sites support.
class SiteProfile(models.Model):
title = models.TextField()
site = models.OneToOneField(Site, on_delete=models.CASCADE)
You will have to create admin for SiteProfile
. Then add some SiteProfile
records with linked Site
. Now you can use site.siteprofile.title
anywhere where you have access to current site from model.