I'm experimenting with the Django Site framework. In an UpdateView form, the Site model _str_ representation is self.domain
, but I want to use self.name
. For example, instead of 'www.test.com', I'd like 'Test, Inc.' to appear.
I guess I could change the Django Site model code itself (shown below), but I don't really want to do that. I'd rather overwrite the setting in my app or the form view, but I can't figure out how to do either.
class Site(models.Model):
domain = models.CharField(
_('domain name'),
max_length=100,
validators=[_simple_domain_name_validator],
unique=True,
)
name = models.CharField(_('display name'), max_length=50)
objects = SiteManager()
class Meta:
db_table = 'django_site'
verbose_name = _('site')
verbose_name_plural = _('sites')
ordering = ['domain']
def __str__(self):
return self.domain
def natural_key(self):
return (self.domain,)
When I subclass the Site model, my CustomSite tries to do a join with the Site model and causes complications, so it seems like that approach isn't ideal.
class CustomSite(Site):
objects = SiteManager()
def __str__(self):
return self.name
class Meta:
db_table = 'django_customsite'
A simplified version of the form I was testing with is below. The form works. It updates the object with the right site, but I'd still like to know how to use the name
as the representation instead of the domain
.
class StoreUpdateView(UpdateView):
model = Site
fields = [
'store',
'site_id',
]
template_name = 'app/group-updateview.html'
I've basically made an intermediary table (Site > Store > Products), but I'd prefer to know how to change the _str_ setting of the Site model. How can I overwrite the _str_ setting of the Site model to return the Site name
field instead of the domain
?