Commit 0a0472f5 authored by Ramiro Morales's avatar Ramiro Morales
Browse files

[1.2.X] Fixed #14951 -- Made the unique_for_{date,month,year} model field...

[1.2.X] Fixed #14951 -- Made the unique_for_{date,month,year} model field constraints to not fail when the related DateField is empty.

Existing modelforms tests were extended to cover this case and an equivalent set of tests was added for the model functionality.

Backport of [15167] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15182 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 0e49b8b8
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -730,7 +730,7 @@ class Model(object):
        called from a ModelForm, some fields may have been excluded; we can't
        perform a unique check on a model that is missing fields involved
        in that check.
        Fields that did not validate should also be exluded, but they need
        Fields that did not validate should also be excluded, but they need
        to be passed in via the exclude argument.
        """
        if exclude is None:
@@ -822,6 +822,8 @@ class Model(object):
            # there's a ticket to add a date lookup, we can remove this special
            # case if that makes it's way in
            date = getattr(self, unique_for)
            if date is None:
                continue
            if lookup_type == 'date':
                lookup_kwargs['%s__day' % unique_for] = date.day
                lookup_kwargs['%s__month' % unique_for] = date.month
+6 −1
Original line number Diff line number Diff line
from django import forms
from django.forms import ModelForm

from models import Product, Price, Book, DerivedBook, ExplicitPK, Post, DerivedPost, Writer
from models import (Product, Price, Book, DerivedBook, ExplicitPK, Post,
        DerivedPost, Writer, FlexibleDatePost)

class ProductForm(ModelForm):
    class Meta:
@@ -37,3 +38,7 @@ class CustomWriterForm(ModelForm):

   class Meta:
       model = Writer

class FlexDatePostForm(ModelForm):
    class Meta:
        model = FlexibleDatePost
+6 −0
Original line number Diff line number Diff line
@@ -236,6 +236,12 @@ class CustomFieldForExclusionModel(models.Model):
    name = models.CharField(max_length=10)
    markup = MarkupField()

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)

__test__ = {'API_TESTS': """
>>> from django import forms
>>> from django.forms.models import ModelForm, model_to_dict
+17 −3
Original line number Diff line number Diff line
import datetime
from django.test import TestCase
from django import forms
from models import Category, Writer, Book, DerivedBook, Post
from models import Category, Writer, Book, DerivedBook, Post, FlexibleDatePost
from mforms import (ProductForm, PriceForm, BookForm, DerivedBookForm,
                   ExplicitPKForm, PostForm, DerivedPostForm, CustomWriterForm)
                   ExplicitPKForm, PostForm, DerivedPostForm, CustomWriterForm,
                   FlexDatePostForm)


class IncompleteCategoryFormWithFields(forms.ModelForm):
@@ -183,3 +184,16 @@ class UniqueTest(TestCase):
            "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p)
        self.assertTrue(form.is_valid())

    def test_unique_for_date_with_nullable_date(self):
        p = FlexibleDatePost.objects.create(title="Django 1.0 is released",
            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))

        form = FlexDatePostForm({'title': "Django 1.0 is released"})
        self.assertTrue(form.is_valid())
        form = FlexDatePostForm({'slug': "Django 1.0"})
        self.assertTrue(form.is_valid())
        form = FlexDatePostForm({'subtitle': "Finally"})
        self.assertTrue(form.is_valid())
        form = FlexDatePostForm({'subtitle': "Finally", "title": "Django 1.0 is released",
            "slug": "Django 1.0"}, instance=p)
        self.assertTrue(form.is_valid())
+15 −0
Original line number Diff line number Diff line
@@ -63,3 +63,18 @@ class Article(models.Model):
    def clean(self):
        if self.pub_date is None:
            self.pub_date = datetime.now()

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)
Loading