Opened 10 years ago

Closed 10 years ago

#24774 closed Bug (fixed)

Django sites framework, domain field unique

Reported by: arieve Owned by: Piotr Jakimiak
Component: contrib.sites Version: 1.8
Severity: Normal Keywords: sites framework, domain field, unique
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by arieve)

In the django.contrib.sites.models the domain field of the Site model is defined like this:

domain = models.CharField(_('domain name'), max_length=100,
        validators=[_simple_domain_name_validator])

I'd like to stress that the field doesn't have the requirement to be unique. Neither by having unique = True or by the _simple_domain_name_validator, which only checks for whitespaces and typos.

In the case that a user (or two different users) would add the same domain (ie "example.com") then the following example from the documentation would go wrong or at least give a completely wrong result:

from django.contrib.sites.shortcuts import get_current_site

def my_view(request):
    current_site = get_current_site(request)
    if current_site.domain == 'foo.com':
        # Do something
        pass
    else:
        # Do something else.
        pass

I don't see any reason why the domain field shouldn't be unique and hence I consider this to be a bug and I believe that there should be a validation that the value of the domain field is unique.

Quick work around:

from django.contrib.sites.models import Site

class CmsSite(Site):
       
    def clean(self, *args, **kwargs):
        if self.__class__._default_manager.filter(domain = self.domain).exists():
            raise ValidationError('Domain already exits')
        super(CmsSite, self).clean(*args, **kwargs)
        
    def save(self, *args, **kwargs):
        self.full_clean()
        super(CmsSite, self).save(*args, **kwargs)

Change History (5)

comment:1 by arieve, 10 years ago

Description: modified (diff)

comment:2 by arieve, 10 years ago

Description: modified (diff)

comment:3 by Aymeric Augustin, 10 years ago

Triage Stage: UnreviewedAccepted

Yes, the field should be changed to unique=True.

comment:4 by Piotr Jakimiak, 10 years ago

Owner: changed from nobody to Piotr Jakimiak
Status: newassigned

comment:5 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 70e3e2e:

Fixed #24774 -- Made contrib.site's Site.domain field unique

Note: See TracTickets for help on using tickets.
Back to Top