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
244cc401
Kaydet (Commit)
244cc401
authored
Tem 05, 2017
tarafından
Krzysztof Nazarewski
Kaydeden (comit)
Tim Graham
Kas 19, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #26184 -- Allowed using any lookups in ModelAdmin.search_fields.
Thanks Krzysztof Nazarewski for the initial patch.
üst
3af305e8
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
148 additions
and
70 deletions
+148
-70
options.py
django/contrib/admin/options.py
+21
-2
utils.py
django/contrib/admin/utils.py
+14
-13
constants.py
django/db/models/sql/constants.py
+0
-10
index.txt
docs/ref/contrib/admin/index.txt
+29
-41
2.1.txt
docs/releases/2.1.txt
+2
-1
models.py
tests/admin_changelist/models.py
+5
-0
tests.py
tests/admin_changelist/tests.py
+77
-3
No files found.
django/contrib/admin/options.py
Dosyayı görüntüle @
244cc401
...
@@ -943,8 +943,27 @@ class ModelAdmin(BaseModelAdmin):
...
@@ -943,8 +943,27 @@ class ModelAdmin(BaseModelAdmin):
return
"
%
s__iexact"
%
field_name
[
1
:]
return
"
%
s__iexact"
%
field_name
[
1
:]
elif
field_name
.
startswith
(
'@'
):
elif
field_name
.
startswith
(
'@'
):
return
"
%
s__search"
%
field_name
[
1
:]
return
"
%
s__search"
%
field_name
[
1
:]
else
:
# Use field_name if it includes a lookup.
return
"
%
s__icontains"
%
field_name
opts
=
queryset
.
model
.
_meta
lookup_fields
=
field_name
.
split
(
LOOKUP_SEP
)
# Go through the fields, following all relations.
prev_field
=
None
for
path_part
in
lookup_fields
:
if
path_part
==
'pk'
:
path_part
=
opts
.
pk
.
name
try
:
field
=
opts
.
get_field
(
path_part
)
except
FieldDoesNotExist
:
# Use valid query lookups.
if
prev_field
and
prev_field
.
get_lookup
(
path_part
):
return
field_name
else
:
prev_field
=
field
if
hasattr
(
field
,
'get_path_info'
):
# Update opts to follow the relation.
opts
=
field
.
get_path_info
()[
-
1
]
.
to_opts
# Otherwise, use the field with icontains.
return
"
%
s__icontains"
%
field_name
use_distinct
=
False
use_distinct
=
False
search_fields
=
self
.
get_search_fields
(
request
)
search_fields
=
self
.
get_search_fields
(
request
)
...
...
django/contrib/admin/utils.py
Dosyayı görüntüle @
244cc401
...
@@ -7,7 +7,6 @@ from django.core.exceptions import FieldDoesNotExist
...
@@ -7,7 +7,6 @@ from django.core.exceptions import FieldDoesNotExist
from
django.db
import
models
from
django.db
import
models
from
django.db.models.constants
import
LOOKUP_SEP
from
django.db.models.constants
import
LOOKUP_SEP
from
django.db.models.deletion
import
Collector
from
django.db.models.deletion
import
Collector
from
django.db.models.sql.constants
import
QUERY_TERMS
from
django.forms.utils
import
pretty_name
from
django.forms.utils
import
pretty_name
from
django.urls
import
NoReverseMatch
,
reverse
from
django.urls
import
NoReverseMatch
,
reverse
from
django.utils
import
formats
,
timezone
from
django.utils
import
formats
,
timezone
...
@@ -26,21 +25,23 @@ def lookup_needs_distinct(opts, lookup_path):
...
@@ -26,21 +25,23 @@ def lookup_needs_distinct(opts, lookup_path):
Return True if 'distinct()' should be used to query the given lookup path.
Return True if 'distinct()' should be used to query the given lookup path.
"""
"""
lookup_fields
=
lookup_path
.
split
(
LOOKUP_SEP
)
lookup_fields
=
lookup_path
.
split
(
LOOKUP_SEP
)
# Remove the last item of the lookup path if it is a query term
# Go through the fields (following all relations) and look for an m2m.
if
lookup_fields
[
-
1
]
in
QUERY_TERMS
:
lookup_fields
=
lookup_fields
[:
-
1
]
# Now go through the fields (following all relations) and look for an m2m
for
field_name
in
lookup_fields
:
for
field_name
in
lookup_fields
:
if
field_name
==
'pk'
:
if
field_name
==
'pk'
:
field_name
=
opts
.
pk
.
name
field_name
=
opts
.
pk
.
name
field
=
opts
.
get_field
(
field_name
)
try
:
if
hasattr
(
field
,
'get_path_info'
):
field
=
opts
.
get_field
(
field_name
)
# This field is a relation, update opts to follow the relation
except
FieldDoesNotExist
:
path_info
=
field
.
get_path_info
()
# Ignore query lookups.
opts
=
path_info
[
-
1
]
.
to_opts
continue
if
any
(
path
.
m2m
for
path
in
path_info
):
else
:
# This field is a m2m relation so we know we need to call distinct
if
hasattr
(
field
,
'get_path_info'
):
return
True
# This field is a relation; update opts to follow the relation.
path_info
=
field
.
get_path_info
()
opts
=
path_info
[
-
1
]
.
to_opts
if
any
(
path
.
m2m
for
path
in
path_info
):
# This field is a m2m relation so distinct must be called.
return
True
return
False
return
False
...
...
django/db/models/sql/constants.py
Dosyayı görüntüle @
244cc401
...
@@ -4,16 +4,6 @@ Constants specific to the SQL storage portion of the ORM.
...
@@ -4,16 +4,6 @@ Constants specific to the SQL storage portion of the ORM.
import
re
import
re
# Valid query types (a set is used for speedy lookups). These are (currently)
# considered SQL-specific; other storage systems may choose to use different
# lookup types.
QUERY_TERMS
=
{
'exact'
,
'iexact'
,
'contains'
,
'icontains'
,
'gt'
,
'gte'
,
'lt'
,
'lte'
,
'in'
,
'startswith'
,
'istartswith'
,
'endswith'
,
'iendswith'
,
'range'
,
'year'
,
'month'
,
'day'
,
'week_day'
,
'hour'
,
'minute'
,
'second'
,
'isnull'
,
'search'
,
'regex'
,
'iregex'
,
}
# Size of each "chunk" for get_iterator calls.
# Size of each "chunk" for get_iterator calls.
# Larger values are slightly faster at the expense of more storage space.
# Larger values are slightly faster at the expense of more storage space.
GET_ITERATOR_CHUNK_SIZE
=
100
GET_ITERATOR_CHUNK_SIZE
=
100
...
...
docs/ref/contrib/admin/index.txt
Dosyayı görüntüle @
244cc401
...
@@ -1238,51 +1238,39 @@ subclass::
...
@@ -1238,51 +1238,39 @@ subclass::
When somebody does a search in the admin search box, Django splits the
When somebody does a search in the admin search box, Django splits the
search query into words and returns all objects that contain each of the
search query into words and returns all objects that contain each of the
words, case insensitive, where each word must be in at least one of
words, case-insensitive (using the :lookup:`icontains` lookup), where each
``search_fields``. For example, if ``search_fields`` is set to
word must be in at least one of ``search_fields``. For example, if
``['first_name', 'last_name']`` and a user searches for ``john lennon``,
``search_fields`` is set to ``['first_name', 'last_name']`` and a user
Django will do the equivalent of this SQL ``WHERE`` clause::
searches for ``john lennon``, Django will do the equivalent of this SQL
``WHERE`` clause::
WHERE (first_name ILIKE '%john%' OR last_name ILIKE '%john%')
WHERE (first_name ILIKE '%john%' OR last_name ILIKE '%john%')
AND (first_name ILIKE '%lennon%' OR last_name ILIKE '%lennon%')
AND (first_name ILIKE '%lennon%' OR last_name ILIKE '%lennon%')
For faster and/or more restrictive searches, prefix the field name
If you don't want to use ``icontains`` as the lookup, you can use any
with an operator:
lookup by appending it the field. For example, you could use :lookup:`exact`
by setting ``search_fields`` to ``['first_name__exact']``.
``^``
Use the '^' operator to match starting at the beginning of the
Beware that because query terms are split and ANDed as described earlier,
field. For example, if ``search_fields`` is set to
searching with :lookup:`exact` only works with a single search word since
``['^first_name', '^last_name']`` and a user searches for
two or more words can't all be an exact match unless all words are the same.
``john lennon``, Django will do the equivalent of this SQL ``WHERE``
clause::
.. versionadded:: 2.1
WHERE (first_name ILIKE 'john%' OR last_name ILIKE 'john%')
The ability to specify a field lookup was added.
AND (first_name ILIKE 'lennon%' OR last_name ILIKE 'lennon%')
Some (older) shortcuts for specifying a field lookup are also available.
This query is more efficient than the normal ``'%john%'`` query,
You can prefix a field in ``search_fields`` with the following characters
because the database only needs to check the beginning of a column's
and it's equivalent to adding ``__<lookup>`` to the field:
data, rather than seeking through the entire column's data. Plus, if
the column has an index on it, some databases may be able to use the
====== ====================
index for this query, even though it's a ``LIKE`` query.
Prefix Lookup
====== ====================
``=``
^ :lookup:`startswith`
Use the '=' operator for case-insensitive exact matching. For
= :lookup:`iexact`
example, if ``search_fields`` is set to
@ :lookup:`search`
``['=first_name', '=last_name']`` and a user searches for
None :lookup:`icontains`
``john lennon``, Django will do the equivalent of this SQL
====== ====================
``WHERE`` clause::
WHERE (first_name ILIKE 'john' OR last_name ILIKE 'john')
AND (first_name ILIKE 'lennon' OR last_name ILIKE 'lennon')
Note that the query input is split by spaces, so, following this
example, it's currently not possible to search for all records in which
``first_name`` is exactly ``'john winston'`` (containing a space).
``@``
Using the '@' operator to perform a full text match. This is like the
default search method but uses an index. Currently this is only
available for MySQL.
If you need to customize search you can use
If you need to customize search you can use
:meth:`ModelAdmin.get_search_results` to provide additional or alternate
:meth:`ModelAdmin.get_search_results` to provide additional or alternate
...
...
docs/releases/2.1.txt
Dosyayı görüntüle @
244cc401
...
@@ -32,7 +32,8 @@ Minor features
...
@@ -32,7 +32,8 @@ Minor features
:mod:`django.contrib.admin`
:mod:`django.contrib.admin`
~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ...
* :attr:`.ModelAdmin.search_fields` now accepts any lookup such as
``field__exact``.
:mod:`django.contrib.admindocs`
:mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
...
tests/admin_changelist/models.py
Dosyayı görüntüle @
244cc401
...
@@ -28,6 +28,7 @@ class Band(models.Model):
...
@@ -28,6 +28,7 @@ class Band(models.Model):
class
Musician
(
models
.
Model
):
class
Musician
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
30
)
name
=
models
.
CharField
(
max_length
=
30
)
age
=
models
.
IntegerField
(
null
=
True
,
blank
=
True
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
name
return
self
.
name
...
@@ -111,3 +112,7 @@ class OrderedObject(models.Model):
...
@@ -111,3 +112,7 @@ class OrderedObject(models.Model):
class
CustomIdUser
(
models
.
Model
):
class
CustomIdUser
(
models
.
Model
):
uuid
=
models
.
AutoField
(
primary_key
=
True
)
uuid
=
models
.
AutoField
(
primary_key
=
True
)
class
CharPK
(
models
.
Model
):
char_pk
=
models
.
CharField
(
max_length
=
100
,
primary_key
=
True
)
tests/admin_changelist/tests.py
Dosyayı görüntüle @
244cc401
...
@@ -8,6 +8,8 @@ from django.contrib.admin.tests import AdminSeleniumTestCase
...
@@ -8,6 +8,8 @@ from django.contrib.admin.tests import AdminSeleniumTestCase
from
django.contrib.admin.views.main
import
ALL_VAR
,
SEARCH_VAR
from
django.contrib.admin.views.main
import
ALL_VAR
,
SEARCH_VAR
from
django.contrib.auth.models
import
User
from
django.contrib.auth.models
import
User
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.models
import
ContentType
from
django.db.models.fields
import
Field
,
IntegerField
from
django.db.models.lookups
import
Contains
,
Exact
from
django.template
import
Context
,
Template
from
django.template
import
Context
,
Template
from
django.test
import
TestCase
,
override_settings
from
django.test
import
TestCase
,
override_settings
from
django.test.client
import
RequestFactory
from
django.test.client
import
RequestFactory
...
@@ -24,9 +26,9 @@ from .admin import (
...
@@ -24,9 +26,9 @@ from .admin import (
site
as
custom_site
,
site
as
custom_site
,
)
)
from
.models
import
(
from
.models
import
(
Band
,
Ch
ild
,
ChordsBand
,
ChordsMusician
,
Concert
,
CustomIdUser
,
Event
,
Band
,
Ch
arPK
,
Child
,
ChordsBand
,
ChordsMusician
,
Concert
,
CustomIdUser
,
Genre
,
Group
,
Invitation
,
Membership
,
Musician
,
OrderedObject
,
Paren
t
,
Event
,
Genre
,
Group
,
Invitation
,
Membership
,
Musician
,
OrderedObjec
t
,
Quartet
,
Swallow
,
SwallowOneToOne
,
UnorderedObject
,
Parent
,
Quartet
,
Swallow
,
SwallowOneToOne
,
UnorderedObject
,
)
)
...
@@ -403,6 +405,78 @@ class ChangeListTests(TestCase):
...
@@ -403,6 +405,78 @@ class ChangeListTests(TestCase):
cl
=
m
.
get_changelist_instance
(
request
)
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertEqual
(
cl
.
queryset
.
count
(),
0
)
self
.
assertEqual
(
cl
.
queryset
.
count
(),
0
)
def
test_builtin_lookup_in_search_fields
(
self
):
band
=
Group
.
objects
.
create
(
name
=
'The Hype'
)
concert
=
Concert
.
objects
.
create
(
name
=
'Woodstock'
,
group
=
band
)
m
=
ConcertAdmin
(
Concert
,
custom_site
)
m
.
search_fields
=
[
'name__iexact'
]
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'woodstock'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[
concert
])
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'wood'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[])
def
test_custom_lookup_in_search_fields
(
self
):
band
=
Group
.
objects
.
create
(
name
=
'The Hype'
)
concert
=
Concert
.
objects
.
create
(
name
=
'Woodstock'
,
group
=
band
)
m
=
ConcertAdmin
(
Concert
,
custom_site
)
m
.
search_fields
=
[
'group__name__cc'
]
Field
.
register_lookup
(
Contains
,
'cc'
)
try
:
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'Hype'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[
concert
])
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'Woodstock'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[])
finally
:
Field
.
_unregister_lookup
(
Contains
,
'cc'
)
def
test_spanning_relations_with_custom_lookup_in_search_fields
(
self
):
hype
=
Group
.
objects
.
create
(
name
=
'The Hype'
)
concert
=
Concert
.
objects
.
create
(
name
=
'Woodstock'
,
group
=
hype
)
vox
=
Musician
.
objects
.
create
(
name
=
'Vox'
,
age
=
20
)
Membership
.
objects
.
create
(
music
=
vox
,
group
=
hype
)
# Register a custom lookup on IntegerField to ensure that field
# traversing logic in ModelAdmin.get_search_results() works.
IntegerField
.
register_lookup
(
Exact
,
'exactly'
)
try
:
m
=
ConcertAdmin
(
Concert
,
custom_site
)
m
.
search_fields
=
[
'group__members__age__exactly'
]
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'20'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[
concert
])
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'21'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[])
finally
:
IntegerField
.
_unregister_lookup
(
Exact
,
'exactly'
)
def
test_custom_lookup_with_pk_shortcut
(
self
):
self
.
assertEqual
(
CharPK
.
_meta
.
pk
.
name
,
'char_pk'
)
# Not equal to 'pk'.
m
=
admin
.
ModelAdmin
(
CustomIdUser
,
custom_site
)
abc
=
CharPK
.
objects
.
create
(
char_pk
=
'abc'
)
abcd
=
CharPK
.
objects
.
create
(
char_pk
=
'abcd'
)
m
=
admin
.
ModelAdmin
(
CharPK
,
custom_site
)
m
.
search_fields
=
[
'pk__exact'
]
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'abc'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[
abc
])
request
=
self
.
factory
.
get
(
'/'
,
data
=
{
SEARCH_VAR
:
'abcd'
})
cl
=
m
.
get_changelist_instance
(
request
)
self
.
assertCountEqual
(
cl
.
queryset
,
[
abcd
])
def
test_no_distinct_for_m2m_in_list_filter_without_params
(
self
):
def
test_no_distinct_for_m2m_in_list_filter_without_params
(
self
):
"""
"""
If a ManyToManyField is in list_filter but isn't in any lookup params,
If a ManyToManyField is in list_filter but isn't in any lookup params,
...
...
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