#24774: Django sites framework, domain field unique
-------------------------+-------------------------------------------------
     Reporter:  arieve   |      Owner:  nobody
         Type:  Bug      |     Status:  new
    Component:           |    Version:  1.8
  contrib.sites          |   Keywords:  sites framework, domain field,
     Severity:  Normal   |  unique
 Triage Stage:           |  Has patch:  0
  Unreviewed             |
Easy pickings:  0        |      UI/UX:  0
-------------------------+-------------------------------------------------
 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).validate_unique(*args, **kwargs)

     def save(self, *args, **kwargs):
         self.full_clean()
         super(CmsSite, self).save(*args, **kwargs)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24774>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.284b3269a7ba8a8893f8e439a7887ac7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to