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
81963b37
Kaydet (Commit)
81963b37
authored
Tem 11, 2016
tarafından
Andrey Fedoseev
Kaydeden (comit)
Tim Graham
Tem 12, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #17657 -- Made ModelForm respect ModelMultipleChoiceField's to_field_name.
Follow up to
67d98441
.
üst
1f82b857
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
78 deletions
+24
-78
related.py
django/db/models/fields/related.py
+2
-5
test_manytomanyfield.py
tests/model_fields/test_manytomanyfield.py
+3
-1
tests.py
tests/model_forms/tests.py
+19
-72
No files found.
django/db/models/fields/related.py
Dosyayı görüntüle @
81963b37
...
@@ -1663,11 +1663,8 @@ class ManyToManyField(RelatedField):
...
@@ -1663,11 +1663,8 @@ class ManyToManyField(RelatedField):
Return the value of this field in the given model instance.
Return the value of this field in the given model instance.
"""
"""
if
obj
.
pk
is
None
:
if
obj
.
pk
is
None
:
return
[]
return
self
.
related_model
.
objects
.
none
()
qs
=
getattr
(
obj
,
self
.
attname
)
.
all
()
return
getattr
(
obj
,
self
.
attname
)
.
all
()
if
qs
.
_result_cache
is
not
None
:
return
[
item
.
pk
for
item
in
qs
]
return
list
(
qs
.
values_list
(
'pk'
,
flat
=
True
))
def
save_form_data
(
self
,
instance
,
data
):
def
save_form_data
(
self
,
instance
,
data
):
getattr
(
instance
,
self
.
attname
)
.
set
(
data
)
getattr
(
instance
,
self
.
attname
)
.
set
(
data
)
...
...
tests/model_fields/test_manytomanyfield.py
Dosyayı görüntüle @
81963b37
...
@@ -12,7 +12,9 @@ class ManyToManyFieldTests(SimpleTestCase):
...
@@ -12,7 +12,9 @@ class ManyToManyFieldTests(SimpleTestCase):
m2m
=
models
.
ManyToManyField
(
'self'
,
models
.
CASCADE
)
m2m
=
models
.
ManyToManyField
(
'self'
,
models
.
CASCADE
)
instance
=
ManyToManyModel
()
instance
=
ManyToManyModel
()
self
.
assertEqual
(
instance
.
_meta
.
get_field
(
'm2m'
)
.
value_from_object
(
instance
),
[])
qs
=
instance
.
_meta
.
get_field
(
'm2m'
)
.
value_from_object
(
instance
)
self
.
assertEqual
(
qs
.
model
,
ManyToManyModel
)
self
.
assertEqual
(
list
(
qs
),
[])
def
test_abstract_model_pending_operations
(
self
):
def
test_abstract_model_pending_operations
(
self
):
"""
"""
...
...
tests/model_forms/tests.py
Dosyayı görüntüle @
81963b37
...
@@ -963,78 +963,6 @@ class UniqueTest(TestCase):
...
@@ -963,78 +963,6 @@ class UniqueTest(TestCase):
self
.
assertEqual
(
form
.
errors
[
'title'
],
[
"Post's Title not unique for Posted date."
])
self
.
assertEqual
(
form
.
errors
[
'title'
],
[
"Post's Title not unique for Posted date."
])
class
ModelToDictTests
(
TestCase
):
"""
Tests for forms.models.model_to_dict
"""
def
test_model_to_dict_many_to_many
(
self
):
categories
=
[
Category
(
name
=
'TestName1'
,
slug
=
'TestName1'
,
url
=
'url1'
),
Category
(
name
=
'TestName2'
,
slug
=
'TestName2'
,
url
=
'url2'
),
Category
(
name
=
'TestName3'
,
slug
=
'TestName3'
,
url
=
'url3'
)
]
for
c
in
categories
:
c
.
save
()
writer
=
Writer
(
name
=
'Test writer'
)
writer
.
save
()
art
=
Article
(
headline
=
'Test article'
,
slug
=
'test-article'
,
pub_date
=
datetime
.
date
(
1988
,
1
,
4
),
writer
=
writer
,
article
=
'Hello.'
)
art
.
save
()
for
c
in
categories
:
art
.
categories
.
add
(
c
)
art
.
save
()
with
self
.
assertNumQueries
(
1
):
d
=
model_to_dict
(
art
)
# Ensure all many-to-many categories appear in model_to_dict
for
c
in
categories
:
self
.
assertIn
(
c
.
pk
,
d
[
'categories'
])
# Ensure many-to-many relation appears as a list
self
.
assertIsInstance
(
d
[
'categories'
],
list
)
def
test_reuse_prefetched
(
self
):
# model_to_dict should not hit the database if it can reuse
# the data populated by prefetch_related.
categories
=
[
Category
(
name
=
'TestName1'
,
slug
=
'TestName1'
,
url
=
'url1'
),
Category
(
name
=
'TestName2'
,
slug
=
'TestName2'
,
url
=
'url2'
),
Category
(
name
=
'TestName3'
,
slug
=
'TestName3'
,
url
=
'url3'
)
]
for
c
in
categories
:
c
.
save
()
writer
=
Writer
(
name
=
'Test writer'
)
writer
.
save
()
art
=
Article
(
headline
=
'Test article'
,
slug
=
'test-article'
,
pub_date
=
datetime
.
date
(
1988
,
1
,
4
),
writer
=
writer
,
article
=
'Hello.'
)
art
.
save
()
for
c
in
categories
:
art
.
categories
.
add
(
c
)
art
=
Article
.
objects
.
prefetch_related
(
'categories'
)
.
get
(
pk
=
art
.
pk
)
with
self
.
assertNumQueries
(
0
):
d
=
model_to_dict
(
art
)
# Ensure all many-to-many categories appear in model_to_dict
for
c
in
categories
:
self
.
assertIn
(
c
.
pk
,
d
[
'categories'
])
# Ensure many-to-many relation appears as a list
self
.
assertIsInstance
(
d
[
'categories'
],
list
)
class
ModelFormBasicTests
(
TestCase
):
class
ModelFormBasicTests
(
TestCase
):
def
create_basic_data
(
self
):
def
create_basic_data
(
self
):
self
.
c1
=
Category
.
objects
.
create
(
self
.
c1
=
Category
.
objects
.
create
(
...
@@ -1788,6 +1716,25 @@ class ModelMultipleChoiceFieldTests(TestCase):
...
@@ -1788,6 +1716,25 @@ class ModelMultipleChoiceFieldTests(TestCase):
sql
,
params
=
queryset
.
query
.
sql_with_params
()
sql
,
params
=
queryset
.
query
.
sql_with_params
()
self
.
assertEqual
(
len
(
params
),
1
)
self
.
assertEqual
(
len
(
params
),
1
)
def
test_to_field_name_with_initial_data
(
self
):
class
ArticleCategoriesForm
(
forms
.
ModelForm
):
categories
=
forms
.
ModelMultipleChoiceField
(
Category
.
objects
.
all
(),
to_field_name
=
'slug'
)
class
Meta
:
model
=
Article
fields
=
[
'categories'
]
article
=
Article
.
objects
.
create
(
headline
=
'Test article'
,
slug
=
'test-article'
,
pub_date
=
datetime
.
date
(
1988
,
1
,
4
),
writer
=
Writer
.
objects
.
create
(
name
=
'Test writer'
),
article
=
'Hello.'
,
)
article
.
categories
.
add
(
self
.
c2
,
self
.
c3
)
form
=
ArticleCategoriesForm
(
instance
=
article
)
self
.
assertEqual
(
form
[
'categories'
]
.
value
(),
[
self
.
c2
.
slug
,
self
.
c3
.
slug
])
class
ModelOneToOneFieldTests
(
TestCase
):
class
ModelOneToOneFieldTests
(
TestCase
):
def
test_modelform_onetoonefield
(
self
):
def
test_modelform_onetoonefield
(
self
):
...
...
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