2010-01-05 11:56:19 +08:00
|
|
|
from datetime import datetime
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
def validate_answer_to_universe(value):
|
|
|
|
if value != 42:
|
|
|
|
raise ValidationError('This is not the answer to life, universe and everything!', code='not42')
|
|
|
|
|
|
|
|
class ModelToValidate(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
created = models.DateTimeField(default=datetime.now)
|
2010-01-30 02:38:28 +08:00
|
|
|
number = models.IntegerField(db_column='number_val')
|
2010-01-21 11:26:14 +08:00
|
|
|
parent = models.ForeignKey('self', blank=True, null=True, limit_choices_to={'number': 10})
|
2010-01-05 11:56:19 +08:00
|
|
|
email = models.EmailField(blank=True)
|
|
|
|
url = models.URLField(blank=True)
|
2011-09-10 08:47:00 +08:00
|
|
|
url_verify = models.URLField(blank=True, verify_exists=True)
|
2010-01-05 11:56:19 +08:00
|
|
|
f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe])
|
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
def clean(self):
|
|
|
|
super(ModelToValidate, self).clean()
|
2010-01-05 11:56:19 +08:00
|
|
|
if self.number == 11:
|
|
|
|
raise ValidationError('Invalid number supplied!')
|
|
|
|
|
|
|
|
class UniqueFieldsModel(models.Model):
|
|
|
|
unique_charfield = models.CharField(max_length=100, unique=True)
|
|
|
|
unique_integerfield = models.IntegerField(unique=True)
|
|
|
|
non_unique_field = models.IntegerField()
|
|
|
|
|
|
|
|
class CustomPKModel(models.Model):
|
|
|
|
my_pk_field = models.CharField(max_length=100, primary_key=True)
|
|
|
|
|
|
|
|
class UniqueTogetherModel(models.Model):
|
|
|
|
cfield = models.CharField(max_length=100)
|
|
|
|
ifield = models.IntegerField()
|
|
|
|
efield = models.EmailField()
|
|
|
|
|
|
|
|
class Meta:
|
2010-02-10 08:49:33 +08:00
|
|
|
unique_together = (('ifield', 'cfield',), ['ifield', 'efield'])
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
class UniqueForDateModel(models.Model):
|
|
|
|
start_date = models.DateField()
|
|
|
|
end_date = models.DateTimeField()
|
|
|
|
count = models.IntegerField(unique_for_date="start_date", unique_for_year="end_date")
|
|
|
|
order = models.IntegerField(unique_for_month="end_date")
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class CustomMessagesModel(models.Model):
|
|
|
|
other = models.IntegerField(blank=True, null=True)
|
2010-01-30 02:38:28 +08:00
|
|
|
number = models.IntegerField(db_column='number_val',
|
2010-01-05 11:56:19 +08:00
|
|
|
error_messages={'null': 'NULL', 'not42': 'AAARGH', 'not_equal': '%s != me'},
|
|
|
|
validators=[validate_answer_to_universe]
|
|
|
|
)
|
2010-01-12 10:29:45 +08:00
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
author = models.ForeignKey(Author)
|
|
|
|
pub_date = models.DateTimeField(blank=True)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if self.pub_date is None:
|
|
|
|
self.pub_date = datetime.now()
|
2011-01-09 21:26:39 +08:00
|
|
|
|
|
|
|
class Post(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
|
|
|
posted = models.DateField()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class FlexibleDatePost(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
|
|
|
|
posted = models.DateField(blank=True, null=True)
|
2011-06-09 23:05:13 +08:00
|
|
|
|
|
|
|
class UniqueErrorsModel(models.Model):
|
|
|
|
name = models.CharField(max_length=100, unique=True, error_messages={'unique': u'Custom unique name message.'})
|
2011-07-04 06:15:39 +08:00
|
|
|
no = models.IntegerField(unique=True, error_messages={'unique': u'Custom unique number message.'})
|
2011-06-11 21:48:24 +08:00
|
|
|
|
|
|
|
class GenericIPAddressTestModel(models.Model):
|
2011-06-14 04:15:13 +08:00
|
|
|
generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True)
|
|
|
|
v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4")
|
|
|
|
v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6")
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2011-06-14 01:48:02 +08:00
|
|
|
class GenericIPAddrUnpackUniqueTest(models.Model):
|
2011-06-11 21:48:24 +08:00
|
|
|
generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True)
|
2011-10-13 19:53:59 +08:00
|
|
|
|
|
|
|
|
2011-10-16 14:57:25 +08:00
|
|
|
# A model can't have multiple AutoFields
|
|
|
|
# Refs #12467.
|
|
|
|
assertion_error = None
|
2011-10-13 19:53:59 +08:00
|
|
|
try:
|
|
|
|
class MultipleAutoFields(models.Model):
|
|
|
|
auto1 = models.AutoField(primary_key=True)
|
|
|
|
auto2 = models.AutoField(primary_key=True)
|
2011-10-16 14:57:25 +08:00
|
|
|
except AssertionError, assertion_error:
|
|
|
|
pass # Fail silently
|
2011-10-28 12:11:41 +08:00
|
|
|
assert str(assertion_error) == u"A model can't have more than one AutoField."
|