Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
import re
|
|
|
|
from bisect import bisect
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db.models.related import RelatedObject
|
|
|
|
from django.db.models.fields.related import ManyToManyRel
|
|
|
|
from django.db.models.fields import AutoField, FieldDoesNotExist
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
from django.db.models.fields.proxy import OrderWrt
|
2007-08-18 12:16:04 +08:00
|
|
|
from django.db.models.loading import get_models, app_cache_ready
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import activate, deactivate_all, get_language, string_concat
|
|
|
|
from django.utils.encoding import force_unicode, smart_str
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
from django.utils.datastructures import SortedDict
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
try:
|
|
|
|
all
|
|
|
|
except NameError:
|
|
|
|
from django.utils.itercompat import all
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Calculate the verbose_name by converting from InitialCaps to "lowercase with spaces".
|
2006-05-16 02:50:54 +08:00
|
|
|
get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', class_name).lower().strip()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-11-17 20:09:21 +08:00
|
|
|
DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
|
2006-05-02 09:31:56 +08:00
|
|
|
'unique_together', 'permissions', 'get_latest_by',
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
'order_with_respect_to', 'app_label', 'db_tablespace',
|
2009-11-03 22:02:49 +08:00
|
|
|
'abstract', 'managed', 'proxy', 'auto_created')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class Options(object):
|
2008-06-29 10:35:08 +08:00
|
|
|
def __init__(self, meta, app_label=None):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self.local_fields, self.local_many_to_many = [], []
|
2008-09-02 23:26:00 +08:00
|
|
|
self.virtual_fields = []
|
2006-05-02 09:31:56 +08:00
|
|
|
self.module_name, self.verbose_name = None, None
|
|
|
|
self.verbose_name_plural = None
|
|
|
|
self.db_table = ''
|
|
|
|
self.ordering = []
|
|
|
|
self.unique_together = []
|
|
|
|
self.permissions = []
|
2008-06-29 10:35:08 +08:00
|
|
|
self.object_name, self.app_label = None, app_label
|
2006-05-02 09:31:56 +08:00
|
|
|
self.get_latest_by = None
|
|
|
|
self.order_with_respect_to = None
|
2007-12-02 03:23:49 +08:00
|
|
|
self.db_tablespace = settings.DEFAULT_TABLESPACE
|
2006-05-02 09:31:56 +08:00
|
|
|
self.admin = None
|
|
|
|
self.meta = meta
|
|
|
|
self.pk = None
|
2007-09-15 02:12:36 +08:00
|
|
|
self.has_auto_field, self.auto_field = False, None
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self.abstract = False
|
2009-03-09 11:35:02 +08:00
|
|
|
self.managed = True
|
2009-03-18 17:47:08 +08:00
|
|
|
self.proxy = False
|
|
|
|
self.proxy_for_model = None
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self.parents = SortedDict()
|
2008-06-29 10:36:18 +08:00
|
|
|
self.duplicate_targets = {}
|
2009-11-03 22:02:49 +08:00
|
|
|
self.auto_created = False
|
2009-03-18 17:47:08 +08:00
|
|
|
|
|
|
|
# To handle various inheritance situations, we need to track where
|
|
|
|
# managers came from (concrete or abstract base classes).
|
2008-09-02 17:04:54 +08:00
|
|
|
self.abstract_managers = []
|
2009-03-18 17:47:08 +08:00
|
|
|
self.concrete_managers = []
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name):
|
2008-06-29 10:35:08 +08:00
|
|
|
from django.db import connection
|
|
|
|
from django.db.backends.util import truncate_name
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
cls._meta = self
|
|
|
|
self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
|
|
|
|
# First, construct the default values for these options.
|
|
|
|
self.object_name = cls.__name__
|
|
|
|
self.module_name = self.object_name.lower()
|
|
|
|
self.verbose_name = get_verbose_name(self.object_name)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Next, apply any overridden values from 'class Meta'.
|
|
|
|
if self.meta:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
meta_attrs = self.meta.__dict__.copy()
|
2008-06-08 04:01:18 +08:00
|
|
|
for name in self.meta.__dict__:
|
|
|
|
# Ignore any private attributes that Django doesn't care about.
|
|
|
|
# NOTE: We can't modify a dictionary's contents while looping
|
|
|
|
# over it, so we loop over the *original* dictionary instead.
|
|
|
|
if name.startswith('_'):
|
|
|
|
del meta_attrs[name]
|
2006-05-02 09:31:56 +08:00
|
|
|
for attr_name in DEFAULT_NAMES:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if attr_name in meta_attrs:
|
|
|
|
setattr(self, attr_name, meta_attrs.pop(attr_name))
|
|
|
|
elif hasattr(self.meta, attr_name):
|
|
|
|
setattr(self, attr_name, getattr(self.meta, attr_name))
|
2007-09-15 04:36:53 +08:00
|
|
|
|
|
|
|
# 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.
|
2010-09-27 05:36:22 +08:00
|
|
|
ut = meta_attrs.pop('unique_together', self.unique_together)
|
2007-09-15 04:36:53 +08:00
|
|
|
if ut and not isinstance(ut[0], (tuple, list)):
|
|
|
|
ut = (ut,)
|
2010-09-27 05:36:22 +08:00
|
|
|
self.unique_together = ut
|
2007-09-15 04:36:53 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# verbose_name_plural is a special case because it uses a 's'
|
|
|
|
# by default.
|
2010-11-17 20:09:21 +08:00
|
|
|
if self.verbose_name_plural is None:
|
|
|
|
self.verbose_name_plural = string_concat(self.verbose_name, 's')
|
2007-09-15 04:36:53 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Any leftover attributes must be invalid.
|
|
|
|
if meta_attrs != {}:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
self.verbose_name_plural = string_concat(self.verbose_name, 's')
|
2006-05-02 09:31:56 +08:00
|
|
|
del self.meta
|
|
|
|
|
2008-06-29 10:35:08 +08:00
|
|
|
# If the db_table wasn't provided, use the app_label + module_name.
|
|
|
|
if not self.db_table:
|
|
|
|
self.db_table = "%s_%s" % (self.app_label, self.module_name)
|
|
|
|
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def _prepare(self, model):
|
|
|
|
if self.order_with_respect_to:
|
|
|
|
self.order_with_respect_to = self.get_field(self.order_with_respect_to)
|
|
|
|
self.ordering = ('_order',)
|
2010-04-22 00:34:33 +08:00
|
|
|
model.add_to_class('_order', OrderWrt())
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
self.order_with_respect_to = None
|
|
|
|
|
|
|
|
if self.pk is None:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if self.parents:
|
|
|
|
# Promote the first parent link in lieu of adding yet another
|
|
|
|
# field.
|
|
|
|
field = self.parents.value_for_index(0)
|
|
|
|
field.primary_key = True
|
2008-06-09 22:03:35 +08:00
|
|
|
self.setup_pk(field)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
else:
|
|
|
|
auto = AutoField(verbose_name='ID', primary_key=True,
|
|
|
|
auto_created=True)
|
|
|
|
model.add_to_class('id', auto)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-06-29 10:36:18 +08:00
|
|
|
# Determine any sets of fields that are pointing to the same targets
|
|
|
|
# (e.g. two ForeignKeys to the same remote model). The query
|
|
|
|
# construction code needs to know this. At the end of this,
|
|
|
|
# self.duplicate_targets will map each duplicate field column to the
|
|
|
|
# columns it duplicates.
|
|
|
|
collections = {}
|
|
|
|
for column, target in self.duplicate_targets.iteritems():
|
|
|
|
try:
|
|
|
|
collections[target].add(column)
|
|
|
|
except KeyError:
|
|
|
|
collections[target] = set([column])
|
|
|
|
self.duplicate_targets = {}
|
|
|
|
for elt in collections.itervalues():
|
|
|
|
if len(elt) == 1:
|
|
|
|
continue
|
|
|
|
for column in elt:
|
|
|
|
self.duplicate_targets[column] = elt.difference(set([column]))
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def add_field(self, field):
|
|
|
|
# Insert the given field in the order in which it was created, using
|
|
|
|
# the "creation_counter" attribute of the field.
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# Move many-to-many related fields from self.fields into
|
|
|
|
# self.many_to_many.
|
2006-05-02 09:31:56 +08:00
|
|
|
if field.rel and isinstance(field.rel, ManyToManyRel):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self.local_many_to_many.insert(bisect(self.local_many_to_many, field), field)
|
|
|
|
if hasattr(self, '_m2m_cache'):
|
|
|
|
del self._m2m_cache
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self.local_fields.insert(bisect(self.local_fields, field), field)
|
|
|
|
self.setup_pk(field)
|
|
|
|
if hasattr(self, '_field_cache'):
|
|
|
|
del self._field_cache
|
|
|
|
del self._field_name_cache
|
|
|
|
|
|
|
|
if hasattr(self, '_name_map'):
|
|
|
|
del self._name_map
|
|
|
|
|
2008-09-02 23:26:00 +08:00
|
|
|
def add_virtual_field(self, field):
|
|
|
|
self.virtual_fields.append(field)
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def setup_pk(self, field):
|
|
|
|
if not self.pk and field.primary_key:
|
|
|
|
self.pk = field
|
|
|
|
field.serialize = False
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
def setup_proxy(self, target):
|
|
|
|
"""
|
|
|
|
Does the internal setup so that the current model is a proxy for
|
|
|
|
"target".
|
|
|
|
"""
|
|
|
|
self.pk = target._meta.pk
|
|
|
|
self.proxy_for_model = target
|
|
|
|
self.db_table = target._meta.db_table
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return '<Options for %s>' % self.object_name
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2006-06-25 12:24:15 +08:00
|
|
|
def __str__(self):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name))
|
|
|
|
|
|
|
|
def verbose_name_raw(self):
|
|
|
|
"""
|
|
|
|
There are a few places where the untranslated verbose name is needed
|
|
|
|
(so that we get the same value regardless of currently active
|
|
|
|
locale).
|
|
|
|
"""
|
|
|
|
lang = get_language()
|
|
|
|
deactivate_all()
|
|
|
|
raw = force_unicode(self.verbose_name)
|
|
|
|
activate(lang)
|
|
|
|
return raw
|
|
|
|
verbose_name_raw = property(verbose_name_raw)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def _fields(self):
|
|
|
|
"""
|
|
|
|
The getter for self.fields. This returns the list of field objects
|
|
|
|
available to this model (including through parent models).
|
|
|
|
|
|
|
|
Callers are not permitted to modify this list, since it's a reference
|
|
|
|
to this instance (not a copy).
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self._field_name_cache
|
|
|
|
except AttributeError:
|
|
|
|
self._fill_fields_cache()
|
|
|
|
return self._field_name_cache
|
|
|
|
fields = property(_fields)
|
|
|
|
|
|
|
|
def get_fields_with_model(self):
|
|
|
|
"""
|
|
|
|
Returns a sequence of (field, model) pairs for all fields. The "model"
|
|
|
|
element is None for fields on the current model. Mostly of use when
|
|
|
|
constructing queries so that we know which model a field belongs to.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self._field_cache
|
|
|
|
except AttributeError:
|
|
|
|
self._fill_fields_cache()
|
|
|
|
return self._field_cache
|
|
|
|
|
|
|
|
def _fill_fields_cache(self):
|
|
|
|
cache = []
|
|
|
|
for parent in self.parents:
|
|
|
|
for field, model in parent._meta.get_fields_with_model():
|
|
|
|
if model:
|
|
|
|
cache.append((field, model))
|
|
|
|
else:
|
|
|
|
cache.append((field, parent))
|
|
|
|
cache.extend([(f, None) for f in self.local_fields])
|
|
|
|
self._field_cache = tuple(cache)
|
|
|
|
self._field_name_cache = [x for x, _ in cache]
|
|
|
|
|
|
|
|
def _many_to_many(self):
|
|
|
|
try:
|
|
|
|
self._m2m_cache
|
|
|
|
except AttributeError:
|
|
|
|
self._fill_m2m_cache()
|
|
|
|
return self._m2m_cache.keys()
|
|
|
|
many_to_many = property(_many_to_many)
|
|
|
|
|
|
|
|
def get_m2m_with_model(self):
|
|
|
|
"""
|
|
|
|
The many-to-many version of get_fields_with_model().
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self._m2m_cache
|
|
|
|
except AttributeError:
|
|
|
|
self._fill_m2m_cache()
|
|
|
|
return self._m2m_cache.items()
|
|
|
|
|
|
|
|
def _fill_m2m_cache(self):
|
|
|
|
cache = SortedDict()
|
|
|
|
for parent in self.parents:
|
|
|
|
for field, model in parent._meta.get_m2m_with_model():
|
|
|
|
if model:
|
|
|
|
cache[field] = model
|
|
|
|
else:
|
|
|
|
cache[field] = parent
|
|
|
|
for field in self.local_many_to_many:
|
|
|
|
cache[field] = None
|
|
|
|
self._m2m_cache = cache
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_field(self, name, many_to_many=True):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
|
|
|
Returns the requested field by name. Raises FieldDoesNotExist on error.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
|
|
|
|
for f in to_search:
|
|
|
|
if f.name == name:
|
|
|
|
return f
|
2010-01-11 02:36:20 +08:00
|
|
|
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, name))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def get_field_by_name(self, name):
|
|
|
|
"""
|
|
|
|
Returns the (field_object, model, direct, m2m), where field_object is
|
|
|
|
the Field instance for the given name, model is the model containing
|
|
|
|
this field (None for local fields), direct is True if the field exists
|
|
|
|
on this model, and m2m is True for many-to-many relations. When
|
|
|
|
'direct' is False, 'field_object' is the corresponding RelatedObject
|
|
|
|
for this field (since the field doesn't have an instance associated
|
|
|
|
with it).
|
|
|
|
|
|
|
|
Uses a cache internally, so after the first access, this is very fast.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return self._name_map[name]
|
|
|
|
except AttributeError:
|
|
|
|
cache = self.init_name_map()
|
2008-04-27 19:44:11 +08:00
|
|
|
return cache[name]
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
except KeyError:
|
|
|
|
raise FieldDoesNotExist('%s has no field named %r'
|
|
|
|
% (self.object_name, name))
|
|
|
|
|
|
|
|
def get_all_field_names(self):
|
|
|
|
"""
|
|
|
|
Returns a list of all field names that are possible for this model
|
2008-08-30 12:52:56 +08:00
|
|
|
(including reverse relation names). This is used for pretty printing
|
|
|
|
debugging output (a list of choices), so any internal-only field names
|
|
|
|
are not included.
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
cache = self._name_map
|
|
|
|
except AttributeError:
|
|
|
|
cache = self.init_name_map()
|
|
|
|
names = cache.keys()
|
|
|
|
names.sort()
|
2008-08-30 12:52:56 +08:00
|
|
|
# Internal-only names end with "+" (symmetrical m2m related names being
|
|
|
|
# the main example). Trim them.
|
|
|
|
return [val for val in names if not val.endswith('+')]
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
|
|
|
def init_name_map(self):
|
|
|
|
"""
|
|
|
|
Initialises the field name -> field object mapping.
|
|
|
|
"""
|
2008-06-26 18:50:25 +08:00
|
|
|
cache = {}
|
|
|
|
# We intentionally handle related m2m objects first so that symmetrical
|
|
|
|
# m2m accessor names can be overridden, if necessary.
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f, model in self.get_all_related_m2m_objects_with_model():
|
|
|
|
cache[f.field.related_query_name()] = (f, model, False, True)
|
|
|
|
for f, model in self.get_all_related_objects_with_model():
|
|
|
|
cache[f.field.related_query_name()] = (f, model, False, False)
|
2008-06-26 18:50:25 +08:00
|
|
|
for f, model in self.get_m2m_with_model():
|
|
|
|
cache[f.name] = (f, model, True, True)
|
|
|
|
for f, model in self.get_fields_with_model():
|
|
|
|
cache[f.name] = (f, model, True, False)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if app_cache_ready():
|
|
|
|
self._name_map = cache
|
|
|
|
return cache
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def get_add_permission(self):
|
|
|
|
return 'add_%s' % self.object_name.lower()
|
|
|
|
|
|
|
|
def get_change_permission(self):
|
|
|
|
return 'change_%s' % self.object_name.lower()
|
|
|
|
|
|
|
|
def get_delete_permission(self):
|
|
|
|
return 'delete_%s' % self.object_name.lower()
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def get_all_related_objects(self, local_only=False, include_hidden=False):
|
|
|
|
return [k for k, v in self.get_all_related_objects_with_model(
|
|
|
|
local_only=local_only, include_hidden=include_hidden)]
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
def get_all_related_objects_with_model(self, local_only=False,
|
|
|
|
include_hidden=False):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
|
|
|
Returns a list of (related-object, model) pairs. Similar to
|
|
|
|
get_fields_with_model().
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self._related_objects_cache
|
2006-05-02 09:31:56 +08:00
|
|
|
except AttributeError:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self._fill_related_objects_cache()
|
2010-11-10 00:46:42 +08:00
|
|
|
predicates = []
|
|
|
|
if local_only:
|
|
|
|
predicates.append(lambda k, v: not v)
|
|
|
|
if not include_hidden:
|
|
|
|
predicates.append(lambda k, v: not k.field.rel.is_hidden())
|
|
|
|
return filter(lambda t: all([p(*t) for p in predicates]),
|
|
|
|
self._related_objects_cache.items())
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
|
|
|
def _fill_related_objects_cache(self):
|
|
|
|
cache = SortedDict()
|
|
|
|
parent_list = self.get_parent_list()
|
|
|
|
for parent in self.parents:
|
|
|
|
for obj, model in parent._meta.get_all_related_objects_with_model():
|
|
|
|
if (obj.field.creation_counter < 0 or obj.field.rel.parent_link) and obj.model not in parent_list:
|
|
|
|
continue
|
|
|
|
if not model:
|
|
|
|
cache[obj] = parent
|
|
|
|
else:
|
|
|
|
cache[obj] = model
|
2010-11-10 00:46:42 +08:00
|
|
|
for klass in get_models(include_auto_created=True):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
for f in klass._meta.local_fields:
|
|
|
|
if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
|
|
|
|
cache[RelatedObject(f.rel.to, klass, f)] = None
|
|
|
|
self._related_objects_cache = cache
|
|
|
|
|
|
|
|
def get_all_related_many_to_many_objects(self, local_only=False):
|
|
|
|
try:
|
|
|
|
cache = self._related_many_to_many_cache
|
|
|
|
except AttributeError:
|
|
|
|
cache = self._fill_related_many_to_many_cache()
|
|
|
|
if local_only:
|
|
|
|
return [k for k, v in cache.items() if not v]
|
|
|
|
return cache.keys()
|
|
|
|
|
|
|
|
def get_all_related_m2m_objects_with_model(self):
|
|
|
|
"""
|
|
|
|
Returns a list of (related-m2m-object, model) pairs. Similar to
|
|
|
|
get_fields_with_model().
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
cache = self._related_many_to_many_cache
|
|
|
|
except AttributeError:
|
|
|
|
cache = self._fill_related_many_to_many_cache()
|
|
|
|
return cache.items()
|
|
|
|
|
|
|
|
def _fill_related_many_to_many_cache(self):
|
|
|
|
cache = SortedDict()
|
|
|
|
parent_list = self.get_parent_list()
|
|
|
|
for parent in self.parents:
|
|
|
|
for obj, model in parent._meta.get_all_related_m2m_objects_with_model():
|
|
|
|
if obj.field.creation_counter < 0 and obj.model not in parent_list:
|
|
|
|
continue
|
|
|
|
if not model:
|
|
|
|
cache[obj] = parent
|
|
|
|
else:
|
|
|
|
cache[obj] = model
|
|
|
|
for klass in get_models():
|
|
|
|
for f in klass._meta.local_many_to_many:
|
|
|
|
if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
|
|
|
|
cache[RelatedObject(f.rel.to, klass, f)] = None
|
|
|
|
if app_cache_ready():
|
|
|
|
self._related_many_to_many_cache = cache
|
|
|
|
return cache
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def get_base_chain(self, model):
|
|
|
|
"""
|
|
|
|
Returns a list of parent classes leading to 'model' (order from closet
|
|
|
|
to most distant ancestor). This has to handle the case were 'model' is
|
|
|
|
a granparent or even more distant relation.
|
|
|
|
"""
|
|
|
|
if not self.parents:
|
|
|
|
return
|
|
|
|
if model in self.parents:
|
|
|
|
return [model]
|
|
|
|
for parent in self.parents:
|
|
|
|
res = parent._meta.get_base_chain(model)
|
|
|
|
if res:
|
|
|
|
res.insert(0, parent)
|
|
|
|
return res
|
|
|
|
raise TypeError('%r is not an ancestor of this model'
|
|
|
|
% model._meta.module_name)
|
|
|
|
|
|
|
|
def get_parent_list(self):
|
|
|
|
"""
|
|
|
|
Returns a list of all the ancestor of this model as a list. Useful for
|
|
|
|
determining if something is an ancestor, regardless of lineage.
|
|
|
|
"""
|
|
|
|
result = set()
|
|
|
|
for parent in self.parents:
|
|
|
|
result.add(parent)
|
|
|
|
result.update(parent._meta.get_parent_list())
|
|
|
|
return result
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-03-04 15:20:08 +08:00
|
|
|
def get_ancestor_link(self, ancestor):
|
|
|
|
"""
|
|
|
|
Returns the field on the current model which points to the given
|
|
|
|
"ancestor". This is possible an indirect link (a pointer to a parent
|
|
|
|
model, which points, eventually, to the ancestor). Used when
|
|
|
|
constructing table joins for model inheritance.
|
|
|
|
|
|
|
|
Returns None if the model isn't an ancestor of this one.
|
|
|
|
"""
|
|
|
|
if ancestor in self.parents:
|
|
|
|
return self.parents[ancestor]
|
|
|
|
for parent in self.parents:
|
2009-05-11 18:10:03 +08:00
|
|
|
# Tries to get a link field from the immediate parent
|
|
|
|
parent_link = parent._meta.get_ancestor_link(ancestor)
|
|
|
|
if parent_link:
|
|
|
|
# In case of a proxied model, the first link
|
|
|
|
# of the chain to the ancestor is that parent
|
|
|
|
# links
|
|
|
|
return self.parents[parent] or parent_link
|
2009-03-04 15:20:08 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_ordered_objects(self):
|
|
|
|
"Returns a list of Options objects that are ordered with respect to this object."
|
|
|
|
if not hasattr(self, '_ordered_objects'):
|
|
|
|
objects = []
|
|
|
|
# TODO
|
|
|
|
#for klass in get_models(get_app(self.app_label)):
|
|
|
|
# opts = klass._meta
|
|
|
|
# if opts.order_with_respect_to and opts.order_with_respect_to.rel \
|
|
|
|
# and self == opts.order_with_respect_to.rel.to._meta:
|
|
|
|
# objects.append(opts)
|
|
|
|
self._ordered_objects = objects
|
|
|
|
return self._ordered_objects
|
2008-12-08 16:15:37 +08:00
|
|
|
|
2009-03-19 17:06:04 +08:00
|
|
|
def pk_index(self):
|
|
|
|
"""
|
|
|
|
Returns the index of the primary key field in the self.fields list.
|
|
|
|
"""
|
|
|
|
return self.fields.index(self.pk)
|