Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
d321d1ac
Kaydet (Commit)
d321d1ac
authored
May 28, 2013
tarafından
Tim Graham
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #20228 - Documented unique_for_date and exclude behavior.
Thanks Deepak Thukral for the patch.
üst
8365d76d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
3 deletions
+41
-3
fields.txt
docs/ref/models/fields.txt
+6
-1
models.py
tests/model_forms/models.py
+11
-1
tests.py
tests/model_forms/tests.py
+24
-1
No files found.
docs/ref/models/fields.txt
Dosyayı görüntüle @
d321d1ac
...
...
@@ -293,7 +293,12 @@ records with the same ``title`` and ``pub_date``.
Note that if you set this to point to a :class:`DateTimeField`, only the date
portion of the field will be considered.
This is enforced by model validation but not at the database level.
This is enforced by :meth:`Model.validate_unique()` during model validation
but not at the database level. If any :attr:`~Field.unique_for_date` constraint
involves fields that are not part of a :class:`~django.forms.ModelForm` (for
example, if one of the fields is listed in ``exclude`` or has
:attr:`editable=False<Field.editable>`), :meth:`Model.validate_unique()` will
skip validation for that particular constraint.
``unique_for_month``
--------------------
...
...
tests/model_forms/models.py
Dosyayı görüntüle @
d321d1ac
...
...
@@ -207,7 +207,17 @@ class Post(models.Model):
posted
=
models
.
DateField
()
def
__str__
(
self
):
return
self
.
name
return
self
.
title
@python_2_unicode_compatible
class
DateTimePost
(
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
.
DateTimeField
(
editable
=
False
)
def
__str__
(
self
):
return
self
.
title
class
DerivedPost
(
Post
):
pass
...
...
tests/model_forms/tests.py
Dosyayı görüntüle @
d321d1ac
...
...
@@ -24,7 +24,7 @@ from .models import (Article, ArticleStatus, BetterAuthor, BigInt,
DerivedPost
,
ExplicitPK
,
FlexibleDatePost
,
ImprovedArticle
,
ImprovedArticleWithParentLink
,
Inventory
,
Post
,
Price
,
Product
,
TextFile
,
AuthorProfile
,
Colour
,
ColourfulItem
,
ArticleStatusNote
,
test_images
)
ArticleStatusNote
,
DateTimePost
,
test_images
)
if
test_images
:
from
.models
import
ImageFile
,
OptionalImageFile
...
...
@@ -76,6 +76,12 @@ class PostForm(forms.ModelForm):
fields
=
'__all__'
class
DateTimePostForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
DateTimePost
fields
=
'__all__'
class
DerivedPostForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
DerivedPost
...
...
@@ -682,6 +688,23 @@ class UniqueTest(TestCase):
self
.
assertEqual
(
len
(
form
.
errors
),
1
)
self
.
assertEqual
(
form
.
errors
[
'posted'
],
[
'This field is required.'
])
def
test_unique_for_date_in_exclude
(
self
):
"""If the date for unique_for_* constraints is excluded from the
ModelForm (in this case 'posted' has editable=False, then the
constraint should be ignored."""
p
=
DateTimePost
.
objects
.
create
(
title
=
"Django 1.0 is released"
,
slug
=
"Django 1.0"
,
subtitle
=
"Finally"
,
posted
=
datetime
.
datetime
(
2008
,
9
,
3
,
10
,
10
,
1
))
# 'title' has unique_for_date='posted'
form
=
DateTimePostForm
({
'title'
:
"Django 1.0 is released"
,
'posted'
:
'2008-09-03'
})
self
.
assertTrue
(
form
.
is_valid
())
# 'slug' has unique_for_year='posted'
form
=
DateTimePostForm
({
'slug'
:
"Django 1.0"
,
'posted'
:
'2008-01-01'
})
self
.
assertTrue
(
form
.
is_valid
())
# 'subtitle' has unique_for_month='posted'
form
=
DateTimePostForm
({
'subtitle'
:
"Finally"
,
'posted'
:
'2008-09-30'
})
self
.
assertTrue
(
form
.
is_valid
())
def
test_inherited_unique_for_date
(
self
):
p
=
Post
.
objects
.
create
(
title
=
"Django 1.0 is released"
,
slug
=
"Django 1.0"
,
subtitle
=
"Finally"
,
posted
=
datetime
.
date
(
2008
,
9
,
3
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment