﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24774	Django sites framework, domain field unique	arieve	nobody	"In the django.contrib.sites.models the domain field of the Site model is defined like this: 
{{{#!python 
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:

{{{#!python
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:

{{{#!python
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)
}}}"	Bug	new	contrib.sites	1.8	Normal		sites framework, domain field, unique		Unreviewed	0	0	0	0	0	0
