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
71ebcb85
Kaydet (Commit)
71ebcb85
authored
Eyl 17, 2015
tarafından
Simon Charette
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #25417 -- Added a field check for invalid default values.
üst
75ed5900
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
0 deletions
+89
-0
__init__.py
django/db/models/fields/__init__.py
+17
-0
checks.txt
docs/ref/checks.txt
+1
-0
1.9.txt
docs/releases/1.9.txt
+3
-0
test_ordinary_fields.py
tests/invalid_models_tests/test_ordinary_fields.py
+68
-0
No files found.
django/db/models/fields/__init__.py
Dosyayı görüntüle @
71ebcb85
...
...
@@ -207,6 +207,7 @@ class Field(RegisterLookupMixin):
errors
=
[]
errors
.
extend
(
self
.
_check_field_name
())
errors
.
extend
(
self
.
_check_choices
())
errors
.
extend
(
self
.
_check_default
())
errors
.
extend
(
self
.
_check_db_index
())
errors
.
extend
(
self
.
_check_null_allowed_for_primary_keys
())
errors
.
extend
(
self
.
_check_backend_specific_checks
(
**
kwargs
))
...
...
@@ -283,6 +284,22 @@ class Field(RegisterLookupMixin):
else
:
return
[]
def
_check_default
(
self
):
if
self
.
has_default
():
default
=
self
.
get_default
()
try
:
self
.
clean
(
default
,
None
)
except
exceptions
.
ValidationError
as
messages
:
return
[
checks
.
Error
(
"Invalid 'default' value:
%
s"
%
message
,
hint
=
None
,
obj
=
self
,
id
=
'fields.E008'
,
)
for
message
in
messages
]
return
[]
def
_check_db_index
(
self
):
if
self
.
db_index
not
in
(
None
,
True
,
False
):
return
[
...
...
docs/ref/checks.txt
Dosyayı görüntüle @
71ebcb85
...
...
@@ -140,6 +140,7 @@ Fields
human readable name)`` tuples.
* **fields.E006**: ``db_index`` must be ``None``, ``True`` or ``False``.
* **fields.E007**: Primary keys must not have ``null=True``.
* **fields.E008**: Invalid ``default`` value.
* **fields.E100**: ``AutoField``\s must set primary_key=True.
* **fields.E110**: ``BooleanField``\s do not accept null values.
* **fields.E120**: ``CharField``\s must define a ``max_length`` attribute.
...
...
docs/releases/1.9.txt
Dosyayı görüntüle @
71ebcb85
...
...
@@ -512,6 +512,9 @@ Models
* ``connection.queries`` shows queries with substituted parameters on SQLite.
* Added a new model field check that makes sure
:attr:`~django.db.models.Field.default` is a valid value.
CSRF
^^^^
...
...
tests/invalid_models_tests/test_ordinary_fields.py
Dosyayı görüntüle @
71ebcb85
# -*- encoding: utf-8 -*-
from
__future__
import
unicode_literals
import
decimal
import
unittest
from
django.core.checks
import
Error
,
Warning
as
DjangoWarning
...
...
@@ -64,6 +65,22 @@ class BooleanFieldTests(IsolatedModelsTestCase):
]
self
.
assertEqual
(
errors
,
expected
)
def
test_invalid_default
(
self
):
class
Model
(
models
.
Model
):
field
=
models
.
BooleanField
(
default
=
'invalid'
)
field
=
Model
.
_meta
.
get_field
(
'field'
)
errors
=
field
.
check
()
expected
=
[
Error
(
"Invalid 'default' value: 'invalid' value must be either True or False."
,
hint
=
None
,
obj
=
field
,
id
=
'fields.E008'
,
),
]
self
.
assertEqual
(
errors
,
expected
)
class
CharFieldTests
(
IsolatedModelsTestCase
,
TestCase
):
...
...
@@ -215,6 +232,22 @@ class CharFieldTests(IsolatedModelsTestCase, TestCase):
]
self
.
assertEqual
(
errors
,
expected
)
def
test_invalid_default
(
self
):
class
Model
(
models
.
Model
):
field
=
models
.
CharField
(
max_length
=
10
,
default
=
None
)
field
=
Model
.
_meta
.
get_field
(
'field'
)
errors
=
field
.
check
()
expected
=
[
Error
(
"Invalid 'default' value: This field cannot be null."
,
hint
=
None
,
obj
=
field
,
id
=
'fields.E008'
,
),
]
self
.
assertEqual
(
errors
,
expected
)
class
DateFieldTests
(
IsolatedModelsTestCase
,
TestCase
):
...
...
@@ -281,6 +314,25 @@ class DateFieldTests(IsolatedModelsTestCase, TestCase):
def
test_fix_default_value_tz
(
self
):
self
.
test_fix_default_value
()
def
test_invalid_default
(
self
):
class
Model
(
models
.
Model
):
field
=
models
.
DateField
(
default
=
'invalid'
)
field
=
Model
.
_meta
.
get_field
(
'field'
)
errors
=
field
.
check
()
message
=
(
"Invalid 'default' value: 'invalid' value has an invalid date format. It must be in YYYY-MM-DD format."
)
expected
=
[
Error
(
message
,
hint
=
None
,
obj
=
field
,
id
=
'fields.E008'
,
),
]
self
.
assertEqual
(
errors
,
expected
)
class
DateTimeFieldTests
(
IsolatedModelsTestCase
,
TestCase
):
...
...
@@ -419,6 +471,22 @@ class DecimalFieldTests(IsolatedModelsTestCase):
expected
=
[]
self
.
assertEqual
(
errors
,
expected
)
def
test_invalid_default
(
self
):
class
Model
(
models
.
Model
):
field
=
models
.
DecimalField
(
max_digits
=
3
,
decimal_places
=
2
,
default
=
decimal
.
Decimal
(
'20.00'
))
field
=
Model
.
_meta
.
get_field
(
'field'
)
errors
=
field
.
check
()
expected
=
[
Error
(
"Invalid 'default' value: Ensure that there are no more than 3 digits in total."
,
hint
=
None
,
obj
=
field
,
id
=
'fields.E008'
,
),
]
self
.
assertEqual
(
errors
,
expected
)
class
FileFieldTests
(
IsolatedModelsTestCase
):
...
...
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