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
440505cb
Kaydet (Commit)
440505cb
authored
Mar 01, 2019
tarafından
Hasan Ramezani
Kaydeden (comit)
Mariusz Felisiak
Mar 01, 2019
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #29408 -- Added validation of related fields and lookups in model Meta.ordering.
üst
4dcbe6eb
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
125 additions
and
6 deletions
+125
-6
base.py
django/db/models/base.py
+29
-3
checks.txt
docs/ref/checks.txt
+2
-2
test_models.py
tests/invalid_models_tests/test_models.py
+85
-1
test_json.py
tests/postgres_tests/test_json.py
+9
-0
No files found.
django/db/models/base.py
Dosyayı görüntüle @
440505cb
...
...
@@ -1643,9 +1643,35 @@ class Model(metaclass=ModelBase):
# Convert "-field" to "field".
fields
=
((
f
[
1
:]
if
f
.
startswith
(
'-'
)
else
f
)
for
f
in
fields
)
# Skip ordering in the format field1__field2 (FIXME: checking
# this format would be nice, but it's a little fiddly).
fields
=
(
f
for
f
in
fields
if
LOOKUP_SEP
not
in
f
)
# Separate related field and non related fields.
_fields
=
[]
related_fields
=
[]
for
f
in
fields
:
if
LOOKUP_SEP
in
f
:
related_fields
.
append
(
f
)
else
:
_fields
.
append
(
f
)
fields
=
_fields
# Check related fields.
for
field
in
related_fields
:
_cls
=
cls
fld
=
None
for
part
in
field
.
split
(
LOOKUP_SEP
):
try
:
fld
=
_cls
.
_meta
.
get_field
(
part
)
if
fld
.
is_relation
:
_cls
=
fld
.
get_path_info
()[
-
1
]
.
to_opts
.
model
except
(
FieldDoesNotExist
,
AttributeError
):
if
fld
is
None
or
fld
.
get_transform
(
part
)
is
None
:
errors
.
append
(
checks
.
Error
(
"'ordering' refers to the nonexistent field, "
"related field or lookup '
%
s'."
%
field
,
obj
=
cls
,
id
=
'models.E015'
,
)
)
# Skip ordering on pk. This is always a valid order_by field
# but is an alias and therefore won't be found by opts.get_field.
...
...
docs/ref/checks.txt
Dosyayı görüntüle @
440505cb
...
...
@@ -277,8 +277,8 @@ Models
supported for that option.
* **models.E014**: ``ordering`` must be a tuple or list (even if you want to
order by only one field).
* **models.E015**: ``ordering`` refers to the nonexistent field
``<field name>``.
* **models.E015**: ``ordering`` refers to the nonexistent field
, related field
or lookup
``<field name>``.
* **models.E016**: ``indexes/index_together/unique_together`` refers to field
``<field_name>`` which is not local to model ``<model>``.
* **models.E017**: Proxy model ``<model>`` contains model fields.
...
...
tests/invalid_models_tests/test_models.py
Dosyayı görüntüle @
440505cb
...
...
@@ -5,9 +5,10 @@ from django.core.checks import Error, Warning
from
django.core.checks.model_checks
import
_check_lazy_references
from
django.core.exceptions
import
ImproperlyConfigured
from
django.db
import
connection
,
connections
,
models
from
django.db.models.functions
import
Lower
from
django.db.models.signals
import
post_init
from
django.test
import
SimpleTestCase
from
django.test.utils
import
isolate_apps
,
override_settings
from
django.test.utils
import
isolate_apps
,
override_settings
,
register_lookup
def
get_max_column_name_length
():
...
...
@@ -665,6 +666,89 @@ class OtherModelTests(SimpleTestCase):
)
])
def
test_ordering_pointing_to_missing_related_field
(
self
):
class
Model
(
models
.
Model
):
test
=
models
.
IntegerField
()
class
Meta
:
ordering
=
(
'missing_related__id'
,)
self
.
assertEqual
(
Model
.
check
(),
[
Error
(
"'ordering' refers to the nonexistent field, related field "
"or lookup 'missing_related__id'."
,
obj
=
Model
,
id
=
'models.E015'
,
)
])
def
test_ordering_pointing_to_missing_related_model_field
(
self
):
class
Parent
(
models
.
Model
):
pass
class
Child
(
models
.
Model
):
parent
=
models
.
ForeignKey
(
Parent
,
models
.
CASCADE
)
class
Meta
:
ordering
=
(
'parent__missing_field'
,)
self
.
assertEqual
(
Child
.
check
(),
[
Error
(
"'ordering' refers to the nonexistent field, related field "
"or lookup 'parent__missing_field'."
,
obj
=
Child
,
id
=
'models.E015'
,
)
])
def
test_ordering_pointing_to_non_related_field
(
self
):
class
Child
(
models
.
Model
):
parent
=
models
.
IntegerField
()
class
Meta
:
ordering
=
(
'parent__missing_field'
,)
self
.
assertEqual
(
Child
.
check
(),
[
Error
(
"'ordering' refers to the nonexistent field, related field "
"or lookup 'parent__missing_field'."
,
obj
=
Child
,
id
=
'models.E015'
,
)
])
def
test_ordering_pointing_to_two_related_model_field
(
self
):
class
Parent2
(
models
.
Model
):
pass
class
Parent1
(
models
.
Model
):
parent2
=
models
.
ForeignKey
(
Parent2
,
models
.
CASCADE
)
class
Child
(
models
.
Model
):
parent1
=
models
.
ForeignKey
(
Parent1
,
models
.
CASCADE
)
class
Meta
:
ordering
=
(
'parent1__parent2__missing_field'
,)
self
.
assertEqual
(
Child
.
check
(),
[
Error
(
"'ordering' refers to the nonexistent field, related field "
"or lookup 'parent1__parent2__missing_field'."
,
obj
=
Child
,
id
=
'models.E015'
,
)
])
def
test_ordering_allows_registered_lookups
(
self
):
class
Model
(
models
.
Model
):
test
=
models
.
CharField
(
max_length
=
100
)
class
Meta
:
ordering
=
(
'test__lower'
,)
with
register_lookup
(
models
.
CharField
,
Lower
):
self
.
assertEqual
(
Model
.
check
(),
[])
def
test_ordering_pointing_to_foreignkey_field
(
self
):
class
Parent
(
models
.
Model
):
pass
...
...
tests/postgres_tests/test_json.py
Dosyayı görüntüle @
440505cb
...
...
@@ -19,6 +19,15 @@ except ImportError:
pass
class
TestModelMetaOrdering
(
PostgreSQLTestCase
):
def
test_ordering_by_json_field_value
(
self
):
class
TestJSONModel
(
JSONModel
):
class
Meta
:
ordering
=
[
'field__value'
]
self
.
assertEqual
(
TestJSONModel
.
check
(),
[])
class
TestSaveLoad
(
PostgreSQLTestCase
):
def
test_null
(
self
):
instance
=
JSONModel
()
...
...
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