Opened 10 years ago

Last modified 10 years ago

#24774 closed Bug

Django sites framework, domain field unique — at Version 1

Reported by: arieve Owned by: nobody
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 domain 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 (1)

comment:1 by arieve, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top