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
bb5c7e4e
Kaydet (Commit)
bb5c7e4e
authored
Nis 12, 2014
tarafından
Mike Fogel
Kaydeden (comit)
Tim Graham
Nis 30, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
[1.7.x] Fixed #22537 -- Add tests and improved docs for field subclass with choices.
Backport of
7fd1b35e
from master
üst
6d4df45e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
5 deletions
+30
-5
fields.txt
docs/ref/models/fields.txt
+2
-2
fields.py
tests/field_subclassing/fields.py
+5
-0
models.py
tests/field_subclassing/models.py
+11
-1
tests.py
tests/field_subclassing/tests.py
+12
-2
No files found.
docs/ref/models/fields.txt
Dosyayı görüntüle @
bb5c7e4e
...
...
@@ -83,8 +83,8 @@ two items (e.g. ``[(A, B), (A, B) ...]``) to use as choices for this field. If
this is given, the default form widget will be a select box with these choices
instead of the standard text field.
The first element in each tuple is the actual value to be s
tored, and the
second element is the human-readable name. For example::
The first element in each tuple is the actual value to be s
et on the model,
and the
second element is the human-readable name. For example::
YEAR_IN_SCHOOL_CHOICES = (
('FR', 'Freshman'),
...
...
tests/field_subclassing/fields.py
Dosyayı görüntüle @
bb5c7e4e
...
...
@@ -20,6 +20,11 @@ class Small(object):
def
__str__
(
self
):
return
'
%
s
%
s'
%
(
force_text
(
self
.
first
),
force_text
(
self
.
second
))
def
__eq__
(
self
,
other
):
if
isinstance
(
other
,
self
.
__class__
):
return
self
.
first
==
other
.
first
and
self
.
second
==
other
.
second
return
False
class
SmallField
(
six
.
with_metaclass
(
models
.
SubfieldBase
,
models
.
Field
)):
"""
...
...
tests/field_subclassing/models.py
Dosyayı görüntüle @
bb5c7e4e
...
...
@@ -5,7 +5,7 @@ Tests for field subclassing.
from
django.db
import
models
from
django.utils.encoding
import
force_text
from
.fields
import
SmallField
,
SmallerField
,
JSONField
from
.fields
import
Small
,
Small
Field
,
SmallerField
,
JSONField
from
django.utils.encoding
import
python_2_unicode_compatible
...
...
@@ -22,5 +22,15 @@ class OtherModel(models.Model):
data
=
SmallerField
()
class
ChoicesModel
(
models
.
Model
):
SMALL_AB
=
Small
(
'a'
,
'b'
)
SMALL_CD
=
Small
(
'c'
,
'd'
)
SMALL_CHOICES
=
(
(
SMALL_AB
,
str
(
SMALL_AB
)),
(
SMALL_CD
,
str
(
SMALL_CD
)),
)
data
=
SmallField
(
'small field'
,
choices
=
SMALL_CHOICES
)
class
DataModel
(
models
.
Model
):
data
=
JSONField
()
tests/field_subclassing/tests.py
Dosyayı görüntüle @
bb5c7e4e
...
...
@@ -2,12 +2,12 @@ from __future__ import unicode_literals
import
inspect
from
django.core
import
serializers
from
django.core
import
exceptions
,
serializers
from
django.db
import
connection
from
django.test
import
TestCase
from
.fields
import
Small
,
CustomTypedField
from
.models
import
DataModel
,
MyModel
,
OtherModel
from
.models
import
ChoicesModel
,
DataModel
,
MyModel
,
OtherModel
class
CustomField
(
TestCase
):
...
...
@@ -106,6 +106,16 @@ class CustomField(TestCase):
self
.
assertIn
(
'__module__'
,
data
)
self
.
assertEqual
(
data
[
'__module__'
],
'field_subclassing.models'
)
def
test_validation_of_choices_for_custom_field
(
self
):
# a valid choice
o
=
ChoicesModel
.
objects
.
create
(
data
=
Small
(
'a'
,
'b'
))
o
.
full_clean
()
# an invalid choice
o
=
ChoicesModel
.
objects
.
create
(
data
=
Small
(
'd'
,
'e'
))
with
self
.
assertRaises
(
exceptions
.
ValidationError
):
o
.
full_clean
()
class
TestDbType
(
TestCase
):
...
...
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