nehem
nehem

Reputation: 13662

Adding extra fields to Django site

I have a project that uses Django's django.contrib.sites.models.Site framework, Now I need to store an additional value per site.

Currently, site contains the below fields

 id     | integer                | not null 
 domain | character varying(100) | not null                                                 | extended |              |
 name   | character varying(50)  | not null

And here is the model

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)

What is the cleaner way of adding an extra field to the Site with a minimal footprint?

Upvotes: 0

Views: 802

Answers (1)

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13753

Looks like you can simply create a subclass of Site and add your extra field there:

class CustomSite(Site):
     custom_field = models.CharField(max_length=150)

After that, make sure to make and run migrations:

python manage.py makemigrations
python manage.py migrate

Hope it helps!

Upvotes: 2

Related Questions