How do you extend the Site model in django?

后端 未结 5 1316
情书的邮戳
情书的邮戳 2021-02-07 23:40

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

5条回答
  •  离开以前
    2021-02-08 00:17

    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.

提交回复
热议问题