FAQ
#17615: Unique field validation with multi-table inheritance
-------------------------------------+-------------------------------------
Reporter: ungenio | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by johan.holman@…):

* status: closed => new
* resolution: fixed =>


Comment:

I'm sorry didn't find the time yesterday to create a complete example, but
here it is:

a table with some values:

{{{
CREATE TABLE test_contact (
`id` int(11) NOT NULL auto_increment,
`subject` varchar(64) NOT NULL,
`email` varchar(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `subject` (`subject`)
) ENGINE=InnoDB;
INSERT INTO test_contact VALUES (1,'aaa','aaa@test.com');
INSERT INTO test_contact VALUES (2,'bbb','bbb@test.com');
INSERT INTO test_contact VALUES (3,'ccc','ccc@test.com');
}}}

python manage.py shell

{{{
import django
django.get_version()
'1.5'

from django.db import models

class ContactSubjectManager(models.Manager):
def get_query_set(self):
return Contact.objects.values('id','subject').all()

class ContactSubject(models.Model):
subject = models.CharField(max_length=64, unique=True)
objects = ContactSubjectManager()
class Meta:
abstract = True
app_label = 'test'

class ContactEmailManager(models.Manager):
def get_query_set(self):
return Contact.objects.values('id','email').all()

class ContactEmail(models.Model):
email = models.CharField(max_length=64)
objects = ContactEmailManager()
class Meta:
abstract = True
app_label = 'test'

class Contact(ContactSubject,ContactEmail):
objects = models.Manager()
class Meta:
app_label = 'test'
def __unicode__(self):
return u'%s - %s' % (self.subject, self.email)

from django import forms

class ContactSubjectForm(forms.ModelForm):
subject = forms.RegexField(
regex = r'^[ 0-9a-zA-Z()-]+$',
max_length = 30,
min_length = 3,
error_messages = {'invalid': u'Please enter a valid subject.'})
class Meta:
abstract = True
model = ContactSubject

class ContactEmailForm(forms.ModelForm):
email = forms.EmailField(
error_messages = {'invalid': u'Please enter a valid email
address.'})
class Meta:
abstract = True
model = ContactEmail

class ContactForm(ContactSubjectForm,ContactEmailForm):
class Meta:
model = Contact

f = Contact.objects.all()
f
[<Contact: aaa - aaa@test.com>, <Contact: bbb - bbb@test.com>, <Contact:
ccc - ccc@test.com>]

data = { 'subject': 'aaa' , 'email' : 'aaa@test.com' }
f = ContactForm(data)
f.is_valid()
False
f.errors
{'subject': [u'Contact with this Subject already exists.']}

data = { 'subject': 'aaa'}
f = ContactSubjectForm(data)
f.is_valid()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "…/lib/python2.7/site-packages/django/forms/forms.py", line 126, in
is_valid
return self.is_bound and not bool(self.errors)
File "…/lib/python2.7/site-packages/django/forms/forms.py", line 117, in
_get_errors
self.full_clean()
File "…l/lib/python2.7/site-packages/django/forms/forms.py", line 274,
in full_clean
self._post_clean()
File "…/lib/python2.7/site-packages/django/forms/models.py", line 344,
in _post_clean
self.validate_unique()
File "…/lib/python2.7/site-packages/django/forms/models.py", line 353,
in validate_unique
self.instance.validate_unique(exclude=exclude)
File "…/lib/python2.7/site-packages/django/db/models/base.py", line 731,
in validate_unique
errors = self._perform_unique_checks(unique_checks)
File "…/lib/python2.7/site-packages/django/db/models/base.py", line 823,
in _perform_unique_checks
model_class_pk = self._get_pk_val(model_class._meta)
File "…/lib/python2.7/site-packages/django/db/models/base.py", line 466,
in _get_pk_val
return getattr(self, meta.pk.attname)
AttributeError: 'NoneType' object has no attribute 'attname'
}}}

The output should be:

{{{
f.is_valid()
False
f.errors
{'subject': [u'Contact subject with this Subject already exists.']}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/17615#comment:6>
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 django-updates+unsubscribe@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 1 | next ›
Discussion Overview
groupdjango-updates @
categoriesdjango, python
postedMar 7, '13 at 10:13a
activeMar 7, '13 at 10:13a
posts1
users1
websitedjangoproject.com

1 user in discussion

Django: 1 post

People

Translate

site design / logo © 2019 Grokbase