Ticket #14951: 14951.diff

File 14951.diff, 9.1 KB (added by Ramiro Morales, 8 years ago)
  • django/db/models/base.py

    diff -r 2635b9494efc django/db/models/base.py
    a b  
    648648        called from a ModelForm, some fields may have been excluded; we can't
    649649        perform a unique check on a model that is missing fields involved
    650650        in that check.
    651         Fields that did not validate should also be exluded, but they need
     651        Fields that did not validate should also be excluded, but they need
    652652        to be passed in via the exclude argument.
    653653        """
    654654        if exclude is None:
     
    740740            # there's a ticket to add a date lookup, we can remove this special
    741741            # case if that makes it's way in
    742742            date = getattr(self, unique_for)
     743            if date is None:
     744                continue
    743745            if lookup_type == 'date':
    744746                lookup_kwargs['%s__day' % unique_for] = date.day
    745747                lookup_kwargs['%s__month' % unique_for] = date.month
  • tests/modeltests/model_forms/mforms.py

    diff -r 2635b9494efc tests/modeltests/model_forms/mforms.py
    a b  
    11from django import forms
    22from django.forms import ModelForm
    33
    4 from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost, Writer
     4from models import (Product, Price, Book, DerivedBook, ExplicitPK, Post,
     5        DerivedPost, Writer, FlexibleDatePost)
    56
    67class ProductForm(ModelForm):
    78    class Meta:
     
    3738
    3839   class Meta:
    3940       model = Writer
     41
     42class FlexDatePostForm(ModelForm):
     43    class Meta:
     44        model = FlexibleDatePost
  • tests/modeltests/model_forms/models.py

    diff -r 2635b9494efc tests/modeltests/model_forms/models.py
    a b  
    236236    name = models.CharField(max_length=10)
    237237    markup = MarkupField()
    238238
     239class FlexibleDatePost(models.Model):
     240    title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
     241    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
     242    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
     243    posted = models.DateField(blank=True, null=True)
     244
    239245__test__ = {'API_TESTS': """
    240246>>> from django import forms
    241247>>> from django.forms.models import ModelForm, model_to_dict
  • tests/modeltests/model_forms/tests.py

    diff -r 2635b9494efc tests/modeltests/model_forms/tests.py
    a b  
    11import datetime
    22from django.test import TestCase
    33from django import forms
    4 from models import Category, Writer, Book, DerivedBook, Post
    5 from mforms import (ProductForm, PriceForm, BookForm, DerivedBookForm,
    6                    ExplicitPKForm, PostForm, DerivedPostForm, CustomWriterForm)
     4from models import Category, Writer, Book, DerivedBook, Post, FlexibleDatePost
     5from mforms import (ProductForm, PriceForm, BookForm, DerivedBookForm,
     6                   ExplicitPKForm, PostForm, DerivedPostForm, CustomWriterForm,
     7                   FlexDatePostForm)
    78
    89
    910class IncompleteCategoryFormWithFields(forms.ModelForm):
     
    183184            "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
    184185        self.assertTrue(form.is_valid())
    185186
     187    def test_unique_for_date_with_nullable_date(self):
     188        p = FlexibleDatePost.objects.create(title="Django 1.0 is released",
     189            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
     190
     191        form = FlexDatePostForm({'title': "Django 1.0 is released"})
     192        self.assertTrue(form.is_valid())
     193        form = FlexDatePostForm({'slug': "Django 1.0"})
     194        self.assertTrue(form.is_valid())
     195        form = FlexDatePostForm({'subtitle': "Finally"})
     196        self.assertTrue(form.is_valid())
     197        form = FlexDatePostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
     198            "slug": "Django 1.0"}, instance=p)
     199        self.assertTrue(form.is_valid())
  • tests/modeltests/validation/models.py

    diff -r 2635b9494efc tests/modeltests/validation/models.py
    a b  
    6363    def clean(self):
    6464        if self.pub_date is None:
    6565            self.pub_date = datetime.now()
     66
     67class Post(models.Model):
     68    title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
     69    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
     70    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
     71    posted = models.DateField()
     72
     73    def __unicode__(self):
     74        return self.name
     75
     76class FlexibleDatePost(models.Model):
     77    title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
     78    slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
     79    subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
     80    posted = models.DateField(blank=True, null=True)
  • tests/modeltests/validation/test_unique.py

    diff -r 2635b9494efc tests/modeltests/validation/test_unique.py
    a b  
    11import datetime
    22
    33from django.conf import settings
     4from django.core.exceptions import ValidationError
    45from django.db import connection
    56from django.test import TestCase
    67from django.utils import unittest
    78
    89from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
    9     UniqueForDateModel, ModelToValidate)
     10    UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
    1011
    1112
    1213class GetUniqueCheckTests(unittest.TestCase):
     
    7677            mtv = ModelToValidate(number=10, name='Some Name')
    7778            mtv.full_clean()
    7879        self.assertNumQueries(0, test)
     80
     81    def test_unique_for_date(self):
     82        p1 = Post.objects.create(title="Django 1.0 is released",
     83            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
     84
     85        p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3))
     86        try:
     87            p.full_clean()
     88        except ValidationError, e:
     89            self.assertEqual(e.message_dict, {'title': [u'Title must be unique for Posted date.']})
     90        else:
     91            self.fail('unique_for_date checks should catch this.')
     92
     93        # Should work without errors
     94        p = Post(title="Work on Django 1.1 begins", posted=datetime.date(2008, 9, 3))
     95        p.full_clean()
     96
     97        # Should work without errors
     98        p = Post(title="Django 1.0 is released", posted=datetime.datetime(2008, 9,4))
     99        p.full_clean()
     100
     101        p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
     102        try:
     103            p.full_clean()
     104        except ValidationError, e:
     105            self.assertEqual(e.message_dict, {'slug': [u'Slug must be unique for Posted year.']})
     106        else:
     107            self.fail('unique_for_year checks should catch this.')
     108
     109        p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
     110        try:
     111            p.full_clean()
     112        except ValidationError, e:
     113            self.assertEqual(e.message_dict, {'subtitle': [u'Subtitle must be unique for Posted month.']})
     114        else:
     115            self.fail('unique_for_month checks should catch this.')
     116
     117        p = Post(title="Django 1.0 is released")
     118        try:
     119            p.full_clean()
     120        except ValidationError, e:
     121            self.assertEqual(e.message_dict, {'posted': [u'This field cannot be null.']})
     122        else:
     123            self.fail("Model validation shouldn't allow an absent value for a DateField without null=True.")
     124
     125    def test_unique_for_date_with_nullable_date(self):
     126        p1 = FlexibleDatePost.objects.create(title="Django 1.0 is released",
     127            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
     128
     129        p = FlexibleDatePost(title="Django 1.0 is released")
     130        try:
     131            p.full_clean()
     132        except ValidationError, e:
     133            self.fail("unique_for_date checks shouldn't trigger when the associated DateField is None.")
     134        except:
     135            self.fail("unique_for_date checks shouldn't explode when the associated DateField is None.")
     136
     137        p = FlexibleDatePost(slug="Django 1.0")
     138        try:
     139            p.full_clean()
     140        except ValidationError, e:
     141            self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.")
     142        except:
     143            self.fail("unique_for_year checks shouldn't explode when the associated DateField is None.")
     144
     145        p = FlexibleDatePost(subtitle="Finally")
     146        try:
     147            p.full_clean()
     148        except ValidationError, e:
     149            self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
     150        except:
     151            self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
Back to Top