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
bb2ca9fe
Kaydet (Commit)
bb2ca9fe
authored
Mar 01, 2014
tarafından
Anubhav Joshi
Kaydeden (comit)
Tim Graham
Mar 01, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #22172 -- Allowed index_together to be a single list (rather than list of lists)..
Thanks EmilStenstrom for the suggestion.
üst
3273bd7b
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
67 additions
and
19 deletions
+67
-19
AUTHORS
AUTHORS
+1
-0
models.py
django/db/migrations/operations/models.py
+3
-2
state.py
django/db/migrations/state.py
+5
-2
options.py
django/db/models/options.py
+14
-9
options.txt
docs/ref/models/options.txt
+7
-0
1.7.txt
docs/releases/1.7.txt
+3
-0
models.py
tests/indexes/models.py
+8
-0
tests.py
tests/indexes/tests.py
+7
-1
test_models.py
tests/invalid_models_tests/test_models.py
+17
-4
test_state.py
tests/migrations/test_state.py
+2
-1
No files found.
AUTHORS
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -330,6 +330,7 @@ answer newbie questions, and generally made Django that much better:
Zak Johnson <zakj@nox.cx>
Nis Jørgensen <nis@superlativ.dk>
Michael Josephson <http://www.sdjournal.com/>
Anubhav Joshi <anubhav9042@gmail.com>
jpellerin@gmail.com
junzhang.jn@gmail.com
Krzysztof Jurewicz <krzysztof.jurewicz@gmail.com>
...
...
django/db/migrations/operations/models.py
Dosyayı görüntüle @
bb2ca9fe
from
django.db
import
models
,
router
from
django.db.models.options
import
normalize_
unique_
together
from
django.db.models.options
import
normalize_together
from
django.db.migrations.state
import
ModelState
from
django.db.migrations.operations.base
import
Operation
from
django.utils
import
six
...
...
@@ -183,7 +183,7 @@ class AlterUniqueTogether(Operation):
def
__init__
(
self
,
name
,
unique_together
):
self
.
name
=
name
unique_together
=
normalize_
unique_
together
(
unique_together
)
unique_together
=
normalize_together
(
unique_together
)
self
.
unique_together
=
set
(
tuple
(
cons
)
for
cons
in
unique_together
)
def
state_forwards
(
self
,
app_label
,
state
):
...
...
@@ -220,6 +220,7 @@ class AlterIndexTogether(Operation):
def
__init__
(
self
,
name
,
index_together
):
self
.
name
=
name
index_together
=
normalize_together
(
index_together
)
self
.
index_together
=
set
(
tuple
(
cons
)
for
cons
in
index_together
)
def
state_forwards
(
self
,
app_label
,
state
):
...
...
django/db/migrations/state.py
Dosyayı görüntüle @
bb2ca9fe
from
django.apps
import
AppConfig
from
django.apps.registry
import
Apps
from
django.db
import
models
from
django.db.models.options
import
DEFAULT_NAMES
,
normalize_
unique_
together
from
django.db.models.options
import
DEFAULT_NAMES
,
normalize_together
from
django.utils
import
six
from
django.utils.module_loading
import
import_string
...
...
@@ -145,7 +145,10 @@ class ModelState(object):
elif
name
in
model
.
_meta
.
original_attrs
:
if
name
==
"unique_together"
:
ut
=
model
.
_meta
.
original_attrs
[
"unique_together"
]
options
[
name
]
=
set
(
normalize_unique_together
(
ut
))
options
[
name
]
=
set
(
normalize_together
(
ut
))
elif
name
==
"index_together"
:
it
=
model
.
_meta
.
original_attrs
[
"index_together"
]
options
[
name
]
=
set
(
normalize_together
(
it
))
else
:
options
[
name
]
=
model
.
_meta
.
original_attrs
[
name
]
# Make our record
...
...
django/db/models/options.py
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -24,24 +24,26 @@ DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
'select_on_save'
)
def
normalize_
unique_together
(
unique
_together
):
def
normalize_
together
(
option
_together
):
"""
unique
_together can be either a tuple of tuples, or a single
option
_together can be either a tuple of tuples, or a single
tuple of two strings. Normalize it to a tuple of tuples, so that
calling code can uniformly expect that.
"""
try
:
if
not
unique
_together
:
if
not
option
_together
:
return
()
first_element
=
next
(
iter
(
unique_together
))
if
not
isinstance
(
option_together
,
(
tuple
,
list
)):
raise
TypeError
first_element
=
next
(
iter
(
option_together
))
if
not
isinstance
(
first_element
,
(
tuple
,
list
)):
unique_together
=
(
unique
_together
,)
option_together
=
(
option
_together
,)
# Normalize everything to tuples
return
tuple
(
tuple
(
ut
)
for
ut
in
unique
_together
)
return
tuple
(
tuple
(
ot
)
for
ot
in
option
_together
)
except
TypeError
:
# If the value of
unique
_together isn't valid, return it
# If the value of
option
_together isn't valid, return it
# verbatim; this will be picked up by the check framework later.
return
unique
_together
return
option
_together
@python_2_unicode_compatible
...
...
@@ -140,7 +142,10 @@ class Options(object):
self
.
original_attrs
[
attr_name
]
=
getattr
(
self
,
attr_name
)
ut
=
meta_attrs
.
pop
(
'unique_together'
,
self
.
unique_together
)
self
.
unique_together
=
normalize_unique_together
(
ut
)
self
.
unique_together
=
normalize_together
(
ut
)
it
=
meta_attrs
.
pop
(
'index_together'
,
self
.
index_together
)
self
.
index_together
=
normalize_together
(
it
)
# verbose_name_plural is a special case because it uses a 's'
# by default.
...
...
docs/ref/models/options.txt
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -340,6 +340,13 @@ Django quotes column and table names behind the scenes.
This list of fields will be indexed together (i.e. the appropriate
``CREATE INDEX`` statement will be issued.)
.. versionchanged:: 1.7
For convenience, ``index_together`` can be a single list when dealing with a single
set of fields::
index_together = ["pub_date", "deadline"]
``verbose_name``
----------------
...
...
docs/releases/1.7.txt
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -642,6 +642,9 @@ Models
an error (before that, it would either result in a database error or
incorrect data).
* You can use a single list for :attr:`~django.db.models.Options.index_together`
(rather than a list of lists) when specifying a single set of fields.
Signals
^^^^^^^
...
...
tests/indexes/models.py
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -12,6 +12,14 @@ class Article(models.Model):
]
# Model for index_together being used only with single list
class
IndexTogetherSingleList
(
models
.
Model
):
headline
=
models
.
CharField
(
max_length
=
100
)
pub_date
=
models
.
DateTimeField
()
class
Meta
:
index_together
=
[
"headline"
,
"pub_date"
]
# Indexing a TextField on Oracle or MySQL results in index creation error.
if
connection
.
vendor
==
'postgresql'
:
class
IndexedArticle
(
models
.
Model
):
...
...
tests/indexes/tests.py
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -4,7 +4,7 @@ from django.core.management.color import no_style
from
django.db
import
connections
,
DEFAULT_DB_ALIAS
from
django.test
import
TestCase
from
.models
import
Article
from
.models
import
Article
,
IndexTogetherSingleList
class
IndexesTests
(
TestCase
):
...
...
@@ -13,6 +13,12 @@ class IndexesTests(TestCase):
index_sql
=
connection
.
creation
.
sql_indexes_for_model
(
Article
,
no_style
())
self
.
assertEqual
(
len
(
index_sql
),
1
)
def
test_index_together_single_list
(
self
):
# Test for using index_together with a single list (#22172)
connection
=
connections
[
DEFAULT_DB_ALIAS
]
index_sql
=
connection
.
creation
.
sql_indexes_for_model
(
IndexTogetherSingleList
,
no_style
())
self
.
assertEqual
(
len
(
index_sql
),
1
)
@skipUnless
(
connections
[
DEFAULT_DB_ALIAS
]
.
vendor
==
'postgresql'
,
"This is a postgresql-specific issue"
)
def
test_postgresql_text_indexes
(
self
):
...
...
tests/invalid_models_tests/test_models.py
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -45,10 +45,7 @@ class IndexTogetherTests(IsolatedModelsTestCase):
def
test_list_containing_non_iterable
(
self
):
class
Model
(
models
.
Model
):
class
Meta
:
index_together
=
[
'non-iterable'
,
'second-non-iterable'
,
]
index_together
=
[(
'a'
,
'b'
),
42
]
errors
=
Model
.
check
()
expected
=
[
...
...
@@ -139,6 +136,22 @@ class UniqueTogetherTests(IsolatedModelsTestCase):
]
self
.
assertEqual
(
errors
,
expected
)
def
test_non_list
(
self
):
class
Model
(
models
.
Model
):
class
Meta
:
unique_together
=
'not-a-list'
errors
=
Model
.
check
()
expected
=
[
Error
(
'"unique_together" must be a list or tuple.'
,
hint
=
None
,
obj
=
Model
,
id
=
'E008'
,
),
]
self
.
assertEqual
(
errors
,
expected
)
def
test_valid_model
(
self
):
class
Model
(
models
.
Model
):
one
=
models
.
IntegerField
()
...
...
tests/migrations/test_state.py
Dosyayı görüntüle @
bb2ca9fe
...
...
@@ -25,6 +25,7 @@ class StateTests(TestCase):
app_label
=
"migrations"
apps
=
new_apps
unique_together
=
[
"name"
,
"bio"
]
index_together
=
[
"bio"
,
"age"
]
class
AuthorProxy
(
Author
):
class
Meta
:
...
...
@@ -63,7 +64,7 @@ class StateTests(TestCase):
self
.
assertEqual
(
author_state
.
fields
[
1
][
1
]
.
max_length
,
255
)
self
.
assertEqual
(
author_state
.
fields
[
2
][
1
]
.
null
,
False
)
self
.
assertEqual
(
author_state
.
fields
[
3
][
1
]
.
null
,
True
)
self
.
assertEqual
(
author_state
.
options
,
{
"unique_together"
:
set
([(
"name"
,
"bio"
)])})
self
.
assertEqual
(
author_state
.
options
,
{
"unique_together"
:
set
([(
"name"
,
"bio"
)])
,
"index_together"
:
set
([(
"bio"
,
"age"
)])
})
self
.
assertEqual
(
author_state
.
bases
,
(
models
.
Model
,
))
self
.
assertEqual
(
book_state
.
app_label
,
"migrations"
)
...
...
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