Commit bb2ca9fe authored by Anubhav Joshi's avatar Anubhav Joshi Committed by Tim Graham
Browse files

Fixed #22172 -- Allowed index_together to be a single list (rather than list of lists)..

Thanks EmilStenstrom for the suggestion.
parent 3273bd7b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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>
+3 −2
Original line number Diff line number Diff line
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):
+5 −2
Original line number Diff line number Diff line
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
+14 −9
Original line number Diff line number Diff line
@@ -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.
+7 −0
Original line number Diff line number Diff line
@@ -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``
----------------

Loading