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
181f492a
Kaydet (Commit)
181f492a
authored
Ara 06, 2016
tarafından
Hiroki Kiyohara
Kaydeden (comit)
Tim Graham
Ara 06, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #27416 -- Prevented ModelFormSet from creating objects for invalid PKs in data.
üst
373140b0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
5 deletions
+28
-5
models.py
django/forms/models.py
+6
-5
tests.py
tests/model_formsets/tests.py
+22
-0
No files found.
django/forms/models.py
Dosyayı görüntüle @
181f492a
...
@@ -755,12 +755,13 @@ class BaseModelFormSet(BaseFormSet):
...
@@ -755,12 +755,13 @@ class BaseModelFormSet(BaseFormSet):
forms_to_delete
=
self
.
deleted_forms
forms_to_delete
=
self
.
deleted_forms
for
form
in
self
.
initial_forms
:
for
form
in
self
.
initial_forms
:
obj
=
form
.
instance
obj
=
form
.
instance
# If the pk is None, it means either:
# 1. The object is an unexpected empty model, created by invalid
# POST data such as an object outside the formset's queryset.
# 2. The object was already deleted from the database.
if
obj
.
pk
is
None
:
continue
if
form
in
forms_to_delete
:
if
form
in
forms_to_delete
:
# If the pk is None, it means that the object can't be
# deleted again. Possible reason for this is that the
# object was already deleted from the DB. Refs #14877.
if
obj
.
pk
is
None
:
continue
self
.
deleted_objects
.
append
(
obj
)
self
.
deleted_objects
.
append
(
obj
)
self
.
delete_existing
(
obj
,
commit
=
commit
)
self
.
delete_existing
(
obj
,
commit
=
commit
)
elif
form
.
has_changed
():
elif
form
.
has_changed
():
...
...
tests/model_formsets/tests.py
Dosyayı görüntüle @
181f492a
...
@@ -1631,6 +1631,28 @@ class ModelFormsetTest(TestCase):
...
@@ -1631,6 +1631,28 @@ class ModelFormsetTest(TestCase):
[
'Please correct the duplicate data for subtitle which must be unique for the month in posted.'
]
[
'Please correct the duplicate data for subtitle which must be unique for the month in posted.'
]
)
)
def
test_prevent_change_outer_model_and_create_invalid_data
(
self
):
author
=
Author
.
objects
.
create
(
name
=
'Charles'
)
other_author
=
Author
.
objects
.
create
(
name
=
'Walt'
)
AuthorFormSet
=
modelformset_factory
(
Author
,
fields
=
'__all__'
)
data
=
{
'form-TOTAL_FORMS'
:
'2'
,
'form-INITIAL_FORMS'
:
'2'
,
'form-MAX_NUM_FORMS'
:
''
,
'form-0-id'
:
str
(
author
.
id
),
'form-0-name'
:
'Charles'
,
'form-1-id'
:
str
(
other_author
.
id
),
# A model not in the formset's queryset.
'form-1-name'
:
'Changed name'
,
}
# This formset is only for Walt Whitman and shouldn't accept data for
# other_author.
formset
=
AuthorFormSet
(
data
=
data
,
queryset
=
Author
.
objects
.
filter
(
id__in
=
(
author
.
id
,)))
self
.
assertTrue
(
formset
.
is_valid
())
formset
.
save
()
# The name of other_author shouldn't be changed and new models aren't
# created.
self
.
assertQuerysetEqual
(
Author
.
objects
.
all
(),
[
'<Author: Charles>'
,
'<Author: Walt>'
])
class
TestModelFormsetOverridesTroughFormMeta
(
TestCase
):
class
TestModelFormsetOverridesTroughFormMeta
(
TestCase
):
def
test_modelformset_factory_widgets
(
self
):
def
test_modelformset_factory_widgets
(
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