Fixed #5460 -- unique_together now accepts a single tuple for convenience. Thanks, Deryck Hodge

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6213 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-09-14 20:36:53 +00:00
parent 3da4c0ab92
commit 9cccf59db5
2 changed files with 17 additions and 0 deletions

View File

@ -52,9 +52,19 @@ class Options(object):
del meta_attrs['__doc__'] del meta_attrs['__doc__']
for attr_name in DEFAULT_NAMES: for attr_name in DEFAULT_NAMES:
setattr(self, attr_name, meta_attrs.pop(attr_name, getattr(self, attr_name))) setattr(self, attr_name, meta_attrs.pop(attr_name, getattr(self, attr_name)))
# unique_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.
ut = meta_attrs.pop('unique_together', getattr(self, 'unique_together'))
if ut and not isinstance(ut[0], (tuple, list)):
ut = (ut,)
setattr(self, 'unique_together', ut)
# verbose_name_plural is a special case because it uses a 's' # verbose_name_plural is a special case because it uses a 's'
# by default. # by default.
setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's'))) setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's')))
# Any leftover attributes must be invalid. # Any leftover attributes must be invalid.
if meta_attrs != {}: if meta_attrs != {}:
raise TypeError, "'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()) raise TypeError, "'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys())

View File

@ -1226,6 +1226,13 @@ together. It's used in the Django admin and is enforced at the database
level (i.e., the appropriate ``UNIQUE`` statements are included in the level (i.e., the appropriate ``UNIQUE`` statements are included in the
``CREATE TABLE`` statement). ``CREATE TABLE`` statement).
**New in Django development version**
For convenience, unique_together can be a single list when dealing
with a single set of fields::
unique_together = ("driver", "restaurant")
``verbose_name`` ``verbose_name``
---------------- ----------------