2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
import warnings
|
2015-03-03 16:43:56 +08:00
|
|
|
from functools import partial
|
2015-01-28 20:35:27 +08:00
|
|
|
from operator import attrgetter
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django import forms
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.apps import apps
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core import checks, exceptions
|
2015-01-02 23:14:23 +08:00
|
|
|
from django.core.exceptions import FieldDoesNotExist
|
2013-09-27 02:16:50 +08:00
|
|
|
from django.db import connection, connections, router, transaction
|
2013-09-17 00:52:05 +08:00
|
|
|
from django.db.backends import utils
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models import Q, signals
|
|
|
|
from django.db.models.deletion import CASCADE, SET_DEFAULT, SET_NULL
|
|
|
|
from django.db.models.fields import (
|
|
|
|
BLANK_CHOICE_DASH, AutoField, Field, IntegerField, PositiveIntegerField,
|
|
|
|
PositiveSmallIntegerField,
|
|
|
|
)
|
2015-02-02 19:48:30 +08:00
|
|
|
from django.db.models.fields.related_lookups import (
|
|
|
|
RelatedExact, RelatedGreaterThan, RelatedGreaterThanOrEqual, RelatedIn,
|
|
|
|
RelatedLessThan, RelatedLessThanOrEqual,
|
|
|
|
)
|
2008-07-21 23:41:21 +08:00
|
|
|
from django.db.models.query import QuerySet
|
2013-11-09 20:25:15 +08:00
|
|
|
from django.db.models.query_utils import PathInfo
|
2015-03-03 16:43:56 +08:00
|
|
|
from django.db.models.utils import make_model_tuple
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2015-02-26 22:19:17 +08:00
|
|
|
from django.utils.deprecation import (
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango20Warning, RemovedInDjango110Warning,
|
2015-02-26 22:19:17 +08:00
|
|
|
)
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.encoding import force_text, smart_text
|
|
|
|
from django.utils.functional import cached_property, curry
|
2013-04-15 21:54:37 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2015-07-22 22:43:21 +08:00
|
|
|
from django.utils.version import get_docs_version
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
RECURSIVE_RELATIONSHIP_CONSTANT = 'self'
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2015-03-03 16:43:56 +08:00
|
|
|
def resolve_relation(scope_model, relation):
|
2008-02-27 05:13:16 +08:00
|
|
|
"""
|
2015-03-03 16:43:56 +08:00
|
|
|
Transform relation into a model or fully-qualified model string of the form
|
|
|
|
"app_label.ModelName", relative to scope_model.
|
|
|
|
|
|
|
|
The relation argument can be:
|
|
|
|
* RECURSIVE_RELATIONSHIP_CONSTANT, i.e. the string "self", in which case
|
|
|
|
the model argument will be returned.
|
|
|
|
* A bare model name without an app_label, in which case scope_model's
|
|
|
|
app_label will be prepended.
|
|
|
|
* An "app_label.ModelName" string.
|
|
|
|
* A model class, which will be returned unchanged.
|
2008-02-27 05:13:16 +08:00
|
|
|
"""
|
|
|
|
# Check for recursive relations
|
|
|
|
if relation == RECURSIVE_RELATIONSHIP_CONSTANT:
|
2015-03-03 16:43:56 +08:00
|
|
|
relation = scope_model
|
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
|
|
|
|
2015-03-03 16:43:56 +08:00
|
|
|
# Look for an "app.Model" relation
|
|
|
|
if isinstance(relation, six.string_types):
|
|
|
|
if "." not in relation:
|
|
|
|
relation = "%s.%s" % (scope_model._meta.app_label, relation)
|
2012-11-05 05:43:54 +08:00
|
|
|
|
2015-03-03 16:43:56 +08:00
|
|
|
return relation
|
|
|
|
|
|
|
|
|
|
|
|
def lazy_related_operation(function, model, *related_models, **kwargs):
|
2008-02-27 05:13:16 +08:00
|
|
|
"""
|
2015-03-03 16:43:56 +08:00
|
|
|
Schedule `function` to be called once `model` and all `related_models`
|
|
|
|
have been imported and registered with the app registry. `function` will
|
|
|
|
be called with the newly-loaded model classes as its positional arguments,
|
|
|
|
plus any optional keyword arguments.
|
|
|
|
|
|
|
|
The `model` argument must be a model class. Each subsequent positional
|
|
|
|
argument is another model, or a reference to another model - see
|
|
|
|
`resolve_relation()` for the various forms these may take. Any relative
|
|
|
|
references will be resolved relative to `model`.
|
|
|
|
|
|
|
|
This is a convenience wrapper for `Apps.lazy_model_operation` - the app
|
|
|
|
registry model used is the one found in `model._meta.apps`.
|
2008-02-27 05:13:16 +08:00
|
|
|
"""
|
2015-03-03 16:43:56 +08:00
|
|
|
models = [model] + [resolve_relation(model, rel) for rel in related_models]
|
|
|
|
model_keys = (make_model_tuple(m) for m in models)
|
|
|
|
apps = model._meta.apps
|
|
|
|
return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-03-03 16:43:56 +08:00
|
|
|
|
|
|
|
def add_lazy_relation(cls, field, relation, operation):
|
|
|
|
warnings.warn(
|
|
|
|
"add_lazy_relation() has been superseded by lazy_related_operation() "
|
|
|
|
"and related methods on the Apps class.",
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango20Warning, stacklevel=2)
|
2015-03-03 16:43:56 +08:00
|
|
|
# Rearrange args for new Apps.lazy_model_operation
|
|
|
|
function = lambda local, related, field: operation(field, related, local)
|
|
|
|
lazy_related_operation(function, cls, relation, field=field)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
class RelatedField(Field):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Base class that all relational fields inherit from.
|
|
|
|
"""
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
one_to_many = False
|
|
|
|
one_to_one = False
|
|
|
|
many_to_many = False
|
|
|
|
many_to_one = False
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def related_model(self):
|
|
|
|
# Can't cache this property until all the models are loaded.
|
|
|
|
apps.check_models_ready()
|
2015-02-26 22:19:17 +08:00
|
|
|
return self.remote_field.model
|
2015-01-07 08:16:35 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def check(self, **kwargs):
|
|
|
|
errors = super(RelatedField, self).check(**kwargs)
|
2014-10-05 07:47:26 +08:00
|
|
|
errors.extend(self._check_related_name_is_valid())
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.extend(self._check_relation_model_exists())
|
|
|
|
errors.extend(self._check_referencing_to_swapped_model())
|
|
|
|
errors.extend(self._check_clashes())
|
|
|
|
return errors
|
|
|
|
|
2014-10-05 07:47:26 +08:00
|
|
|
def _check_related_name_is_valid(self):
|
|
|
|
import re
|
|
|
|
import keyword
|
2015-02-26 22:19:17 +08:00
|
|
|
related_name = self.remote_field.related_name
|
2015-06-23 14:08:12 +08:00
|
|
|
if not related_name:
|
|
|
|
return []
|
|
|
|
is_valid_id = True
|
|
|
|
if keyword.iskeyword(related_name):
|
|
|
|
is_valid_id = False
|
|
|
|
if six.PY3:
|
|
|
|
if not related_name.isidentifier():
|
|
|
|
is_valid_id = False
|
|
|
|
else:
|
|
|
|
if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*\Z', related_name):
|
|
|
|
is_valid_id = False
|
|
|
|
if not (is_valid_id or related_name.endswith('+')):
|
2014-10-05 07:47:26 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
|
|
|
"The name '%s' is invalid related_name for field %s.%s" %
|
2015-02-26 22:19:17 +08:00
|
|
|
(self.remote_field.related_name, self.model._meta.object_name,
|
2014-10-05 07:47:26 +08:00
|
|
|
self.name),
|
|
|
|
hint="Related name must be a valid Python identifier or end with a '+'",
|
|
|
|
obj=self,
|
|
|
|
id='fields.E306',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
return []
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def _check_relation_model_exists(self):
|
2015-02-26 22:19:17 +08:00
|
|
|
rel_is_missing = self.remote_field.model not in apps.get_models()
|
|
|
|
rel_is_string = isinstance(self.remote_field.model, six.string_types)
|
|
|
|
model_name = self.remote_field.model if rel_is_string else self.remote_field.model._meta.object_name
|
|
|
|
if rel_is_missing and (rel_is_string or not self.remote_field.model._meta.swapped):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("Field defines a relation with model '%s', which "
|
|
|
|
"is either not installed, or is abstract.") % model_name,
|
|
|
|
hint=None,
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E300',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
return []
|
|
|
|
|
|
|
|
def _check_referencing_to_swapped_model(self):
|
2015-02-26 22:19:17 +08:00
|
|
|
if (self.remote_field.model not in apps.get_models() and
|
|
|
|
not isinstance(self.remote_field.model, six.string_types) and
|
|
|
|
self.remote_field.model._meta.swapped):
|
2014-01-20 10:45:21 +08:00
|
|
|
model = "%s.%s" % (
|
2015-02-26 22:19:17 +08:00
|
|
|
self.remote_field.model._meta.app_label,
|
|
|
|
self.remote_field.model._meta.object_name
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("Field defines a relation with the model '%s', "
|
|
|
|
"which has been swapped out.") % model,
|
2015-02-26 22:19:17 +08:00
|
|
|
hint="Update the relation to point at 'settings.%s'." % self.remote_field.model._meta.swappable,
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E301',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
return []
|
|
|
|
|
|
|
|
def _check_clashes(self):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Check accessor and reverse query name clashes.
|
|
|
|
"""
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.db.models.base import ModelBase
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
opts = self.model._meta
|
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
# `f.remote_field.model` may be a string instead of a model. Skip if model name is
|
2014-01-20 10:45:21 +08:00
|
|
|
# not resolved.
|
2015-02-26 22:19:17 +08:00
|
|
|
if not isinstance(self.remote_field.model, ModelBase):
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
|
|
|
|
# If the field doesn't install backward relation on the target model (so
|
|
|
|
# `is_hidden` returns True), then there are no clashes to check and we
|
|
|
|
# can skip these fields.
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.is_hidden():
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
|
|
|
|
# Consider that we are checking field `Model.foreign` and the models
|
|
|
|
# are:
|
|
|
|
#
|
|
|
|
# class Target(models.Model):
|
|
|
|
# model = models.IntegerField()
|
|
|
|
# model_set = models.IntegerField()
|
|
|
|
#
|
|
|
|
# class Model(models.Model):
|
|
|
|
# foreign = models.ForeignKey(Target)
|
|
|
|
# m2m = models.ManyToManyField(Target)
|
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
rel_opts = self.remote_field.model._meta
|
2014-01-20 10:45:21 +08:00
|
|
|
# rel_opts.object_name == "Target"
|
2015-02-26 22:19:17 +08:00
|
|
|
rel_name = self.remote_field.get_accessor_name() # i. e. "model_set"
|
2014-01-20 10:45:21 +08:00
|
|
|
rel_query_name = self.related_query_name() # i. e. "model"
|
|
|
|
field_name = "%s.%s" % (opts.object_name,
|
|
|
|
self.name) # i. e. "Model.field"
|
|
|
|
|
|
|
|
# Check clashes between accessor or reverse query name of `field`
|
2013-11-09 20:25:15 +08:00
|
|
|
# and any other field name -- i.e. accessor for Model.foreign is
|
2014-01-20 10:45:21 +08:00
|
|
|
# model_set and it clashes with Target.model_set.
|
2014-02-15 22:36:07 +08:00
|
|
|
potential_clashes = rel_opts.fields + rel_opts.many_to_many
|
2014-01-20 10:45:21 +08:00
|
|
|
for clash_field in potential_clashes:
|
|
|
|
clash_name = "%s.%s" % (rel_opts.object_name,
|
|
|
|
clash_field.name) # i. e. "Target.model_set"
|
|
|
|
if clash_field.name == rel_name:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"Reverse accessor for '%s' clashes with field name '%s'." % (field_name, clash_name),
|
|
|
|
hint=("Rename field '%s', or add/change a related_name "
|
|
|
|
"argument to the definition for field '%s'.") % (clash_name, field_name),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E302',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if clash_field.name == rel_query_name:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"Reverse query name for '%s' clashes with field name '%s'." % (field_name, clash_name),
|
|
|
|
hint=("Rename field '%s', or add/change a related_name "
|
|
|
|
"argument to the definition for field '%s'.") % (clash_name, field_name),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E303',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check clashes between accessors/reverse query names of `field` and
|
|
|
|
# any other field accessor -- i. e. Model.foreign accessor clashes with
|
|
|
|
# Model.m2m accessor.
|
2015-01-07 08:16:35 +08:00
|
|
|
potential_clashes = (r for r in rel_opts.related_objects if r.field is not self)
|
2014-01-20 10:45:21 +08:00
|
|
|
for clash_field in potential_clashes:
|
|
|
|
clash_name = "%s.%s" % ( # i. e. "Model.m2m"
|
2015-01-07 08:16:35 +08:00
|
|
|
clash_field.related_model._meta.object_name,
|
2014-01-20 10:45:21 +08:00
|
|
|
clash_field.field.name)
|
|
|
|
if clash_field.get_accessor_name() == rel_name:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"Reverse accessor for '%s' clashes with reverse accessor for '%s'." % (field_name, clash_name),
|
|
|
|
hint=("Add or change a related_name argument "
|
|
|
|
"to the definition for '%s' or '%s'.") % (field_name, clash_name),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E304',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if clash_field.get_accessor_name() == rel_query_name:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-09-04 20:15:09 +08:00
|
|
|
"Reverse query name for '%s' clashes with reverse query name for '%s'."
|
|
|
|
% (field_name, clash_name),
|
2014-03-03 18:18:39 +08:00
|
|
|
hint=("Add or change a related_name argument "
|
|
|
|
"to the definition for '%s' or '%s'.") % (field_name, clash_name),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E305',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
def db_type(self, connection):
|
2015-02-16 17:06:42 +08:00
|
|
|
# By default related field will not have a column as it relates to
|
|
|
|
# columns from another table.
|
2013-03-25 00:40:40 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name, virtual_only=False):
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-03-03 16:43:56 +08:00
|
|
|
super(RelatedField, self).contribute_to_class(cls, name, virtual_only=virtual_only)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-03-03 16:43:56 +08:00
|
|
|
self.opts = cls._meta
|
2008-06-26 15:04:18 +08:00
|
|
|
|
2015-02-12 14:28:24 +08:00
|
|
|
if not cls._meta.abstract:
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.related_name:
|
|
|
|
related_name = force_text(self.remote_field.related_name) % {
|
2015-02-12 14:28:24 +08:00
|
|
|
'class': cls.__name__.lower(),
|
|
|
|
'app_label': cls._meta.app_label.lower()
|
|
|
|
}
|
2015-02-26 22:19:17 +08:00
|
|
|
self.remote_field.related_name = related_name
|
2015-03-03 16:43:56 +08:00
|
|
|
|
|
|
|
def resolve_related_class(model, related, field):
|
|
|
|
field.remote_field.model = related
|
|
|
|
field.do_related_class(related, model)
|
|
|
|
lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-01-15 22:20:47 +08:00
|
|
|
@property
|
|
|
|
def swappable_setting(self):
|
|
|
|
"""
|
2015-02-16 17:06:42 +08:00
|
|
|
Get the setting that this is powered from for swapping, or None
|
2014-01-15 22:20:47 +08:00
|
|
|
if it's not swapped in / marked with swappable=False.
|
|
|
|
"""
|
|
|
|
if self.swappable:
|
|
|
|
# Work out string form of "to"
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(self.remote_field.model, six.string_types):
|
|
|
|
to_string = self.remote_field.model
|
2014-01-15 22:20:47 +08:00
|
|
|
else:
|
|
|
|
to_string = "%s.%s" % (
|
2015-02-26 22:19:17 +08:00
|
|
|
self.remote_field.model._meta.app_label,
|
|
|
|
self.remote_field.model._meta.object_name,
|
2014-01-15 22:20:47 +08:00
|
|
|
)
|
|
|
|
# See if anything swapped/swappable matches
|
|
|
|
for model in apps.get_models(include_swapped=True):
|
|
|
|
if model._meta.swapped:
|
|
|
|
if model._meta.swapped == to_string:
|
|
|
|
return model._meta.swappable
|
|
|
|
if ("%s.%s" % (model._meta.app_label, model._meta.object_name)) == to_string and model._meta.swappable:
|
|
|
|
return model._meta.swappable
|
|
|
|
return None
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def set_attributes_from_rel(self):
|
2015-02-26 22:19:17 +08:00
|
|
|
self.name = self.name or (self.remote_field.model._meta.model_name + '_' + self.remote_field.model._meta.pk.name)
|
2008-07-29 13:17:47 +08:00
|
|
|
if self.verbose_name is None:
|
2015-02-26 22:19:17 +08:00
|
|
|
self.verbose_name = self.remote_field.model._meta.verbose_name
|
|
|
|
self.remote_field.set_field_name()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
@property
|
|
|
|
def related(self):
|
|
|
|
warnings.warn(
|
2015-02-26 22:19:17 +08:00
|
|
|
"Usage of field.related has been deprecated. Use field.remote_field instead.",
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango110Warning, 2)
|
2015-02-26 22:19:17 +08:00
|
|
|
return self.remote_field
|
2013-11-09 20:25:15 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def do_related_class(self, other, cls):
|
|
|
|
self.set_attributes_from_rel()
|
2015-03-03 16:43:56 +08:00
|
|
|
self.contribute_to_related_class(other, self.remote_field)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-02-02 03:23:31 +08:00
|
|
|
def get_limit_choices_to(self):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Return ``limit_choices_to`` for this model field.
|
2014-02-02 03:23:31 +08:00
|
|
|
|
|
|
|
If it is a callable, it will be invoked and the result will be
|
|
|
|
returned.
|
|
|
|
"""
|
2015-02-26 22:19:17 +08:00
|
|
|
if callable(self.remote_field.limit_choices_to):
|
|
|
|
return self.remote_field.limit_choices_to()
|
|
|
|
return self.remote_field.limit_choices_to
|
2014-02-02 03:23:31 +08:00
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Pass ``limit_choices_to`` to the field being constructed.
|
2014-02-02 03:23:31 +08:00
|
|
|
|
|
|
|
Only passes it if there is a type that supports related fields.
|
|
|
|
This is a similar strategy used to pass the ``queryset`` to the field
|
|
|
|
being constructed.
|
|
|
|
"""
|
|
|
|
defaults = {}
|
2015-02-26 22:19:17 +08:00
|
|
|
if hasattr(self.remote_field, 'get_related_field'):
|
2014-02-02 03:23:31 +08:00
|
|
|
# If this is a callable, do not invoke it here. Just pass
|
|
|
|
# it in the defaults for when the form class will later be
|
|
|
|
# instantiated.
|
2015-02-26 22:19:17 +08:00
|
|
|
limit_choices_to = self.remote_field.limit_choices_to
|
2014-02-02 03:23:31 +08:00
|
|
|
defaults.update({
|
|
|
|
'limit_choices_to': limit_choices_to,
|
|
|
|
})
|
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(RelatedField, self).formfield(**defaults)
|
|
|
|
|
2010-03-27 23:54:31 +08:00
|
|
|
def related_query_name(self):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Define the name that can be used to identify this related object in a
|
|
|
|
table-spanning query.
|
|
|
|
"""
|
2015-02-26 22:19:17 +08:00
|
|
|
return self.remote_field.related_query_name or self.remote_field.related_name or self.opts.model_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def target_field(self):
|
|
|
|
"""
|
|
|
|
When filtering against this relation, returns the field on the remote
|
|
|
|
model against which the filtering should happen.
|
|
|
|
"""
|
|
|
|
target_fields = self.get_path_info()[-1].target_fields
|
|
|
|
if len(target_fields) > 1:
|
|
|
|
raise exceptions.FieldError(
|
|
|
|
"The relation has multiple target fields, but only single target field was asked for")
|
|
|
|
return target_fields[0]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2014-03-21 19:45:38 +08:00
|
|
|
class SingleRelatedObjectDescriptor(object):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Accessor to the related object on the reverse side of a one-to-one
|
|
|
|
relation.
|
|
|
|
|
|
|
|
In the example::
|
|
|
|
|
|
|
|
class Restaurant(Model):
|
|
|
|
place = OneToOneField(Place, related_name='restaurant')
|
|
|
|
|
|
|
|
``place.restaurant`` is a ``SingleRelatedObjectDescriptor`` instance.
|
|
|
|
"""
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __init__(self, related):
|
|
|
|
self.related = related
|
2010-01-27 21:30:29 +08:00
|
|
|
self.cache_name = related.get_cache_name()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-12-09 03:12:01 +08:00
|
|
|
@cached_property
|
|
|
|
def RelatedObjectDoesNotExist(self):
|
|
|
|
# The exception isn't created at initialization time for the sake of
|
|
|
|
# consistency with `ReverseSingleRelatedObjectDescriptor`.
|
|
|
|
return type(
|
|
|
|
str('RelatedObjectDoesNotExist'),
|
2015-01-07 08:16:35 +08:00
|
|
|
(self.related.related_model.DoesNotExist, AttributeError),
|
2013-12-09 03:12:01 +08:00
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
def is_cached(self, instance):
|
|
|
|
return hasattr(instance, self.cache_name)
|
|
|
|
|
2013-09-30 13:05:43 +08:00
|
|
|
def get_queryset(self, **hints):
|
2015-01-07 08:16:35 +08:00
|
|
|
manager = self.related.related_model._default_manager
|
2014-09-24 01:41:32 +08:00
|
|
|
# If the related manager indicates that it should be used for
|
|
|
|
# related fields, respect that.
|
|
|
|
if not getattr(manager, 'use_for_related_fields', False):
|
2015-01-07 08:16:35 +08:00
|
|
|
manager = self.related.related_model._base_manager
|
2014-09-24 01:41:32 +08:00
|
|
|
return manager.db_manager(hints=hints).all()
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
2014-01-17 02:24:39 +08:00
|
|
|
if queryset is None:
|
2014-09-24 01:41:32 +08:00
|
|
|
queryset = self.get_queryset()
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset._add_hints(instance=instances[0])
|
2013-11-07 01:25:05 +08:00
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
rel_obj_attr = attrgetter(self.related.field.attname)
|
|
|
|
instance_attr = lambda obj: obj._get_pk_val()
|
2014-12-07 05:00:09 +08:00
|
|
|
instances_dict = {instance_attr(inst): inst for inst in instances}
|
2013-03-25 00:40:40 +08:00
|
|
|
query = {'%s__in' % self.related.field.name: instances}
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset = queryset.filter(**query)
|
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
# Since we're going to assign directly in the cache,
|
|
|
|
# we must manage the reverse relation cache manually.
|
|
|
|
rel_obj_cache_name = self.related.field.get_cache_name()
|
2014-01-17 02:24:39 +08:00
|
|
|
for rel_obj in queryset:
|
2012-05-24 19:25:01 +08:00
|
|
|
instance = instances_dict[rel_obj_attr(rel_obj)]
|
|
|
|
setattr(rel_obj, rel_obj_cache_name, instance)
|
2014-01-17 02:24:39 +08:00
|
|
|
return queryset, rel_obj_attr, instance_attr, True, self.cache_name
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
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:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = getattr(instance, self.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 AttributeError:
|
2012-10-10 03:36:35 +08:00
|
|
|
related_pk = instance._get_pk_val()
|
|
|
|
if related_pk is None:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = None
|
|
|
|
else:
|
2013-03-25 00:40:40 +08:00
|
|
|
params = {}
|
|
|
|
for lh_field, rh_field in self.related.field.related_fields:
|
|
|
|
params['%s__%s' % (self.related.field.name, rh_field.name)] = getattr(instance, rh_field.attname)
|
2012-10-10 03:36:35 +08:00
|
|
|
try:
|
2013-03-08 22:15:23 +08:00
|
|
|
rel_obj = self.get_queryset(instance=instance).get(**params)
|
2015-01-07 08:16:35 +08:00
|
|
|
except self.related.related_model.DoesNotExist:
|
2012-10-10 03:36:35 +08:00
|
|
|
rel_obj = None
|
|
|
|
else:
|
|
|
|
setattr(rel_obj, self.related.field.get_cache_name(), instance)
|
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
|
|
|
setattr(instance, self.cache_name, rel_obj)
|
2012-04-10 20:29:25 +08:00
|
|
|
if rel_obj is None:
|
2013-12-09 03:12:01 +08:00
|
|
|
raise self.RelatedObjectDoesNotExist(
|
|
|
|
"%s has no %s." % (
|
|
|
|
instance.__class__.__name__,
|
|
|
|
self.related.get_accessor_name()
|
|
|
|
)
|
|
|
|
)
|
2012-04-10 20:29:25 +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
|
|
|
return rel_obj
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2008-06-29 18:35:35 +08:00
|
|
|
# The similarity of the code below to the code in
|
2008-06-05 08:39:32 +08:00
|
|
|
# ReverseSingleRelatedObjectDescriptor is annoying, but there's a bunch
|
|
|
|
# of small differences that would make a common base class convoluted.
|
2008-06-29 18:35:35 +08:00
|
|
|
|
2008-06-05 08:39:32 +08:00
|
|
|
# If null=True, we can assign null here, but otherwise the value needs
|
|
|
|
# to be an instance of the related class.
|
2013-07-08 08:39:54 +08:00
|
|
|
if value is None and self.related.field.null is False:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign None: "%s.%s" does not allow null values.' % (
|
|
|
|
instance._meta.object_name,
|
|
|
|
self.related.get_accessor_name(),
|
|
|
|
)
|
|
|
|
)
|
2015-01-07 08:16:35 +08:00
|
|
|
elif value is not None and not isinstance(value, self.related.related_model):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign "%r": "%s.%s" must be a "%s" instance.' % (
|
|
|
|
value,
|
|
|
|
instance._meta.object_name,
|
|
|
|
self.related.get_accessor_name(),
|
2015-02-23 15:31:58 +08:00
|
|
|
self.related.related_model._meta.object_name,
|
2013-12-13 04:23:24 +08:00
|
|
|
)
|
|
|
|
)
|
2010-01-22 22:30:06 +08:00
|
|
|
elif value is not None:
|
|
|
|
if instance._state.db is None:
|
|
|
|
instance._state.db = router.db_for_write(instance.__class__, instance=value)
|
|
|
|
elif value._state.db is None:
|
|
|
|
value._state.db = router.db_for_write(value.__class__, instance=instance)
|
|
|
|
elif value._state.db is not None and instance._state.db is not None:
|
|
|
|
if not router.allow_relation(value, instance):
|
2013-05-01 04:28:09 +08:00
|
|
|
raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value)
|
2008-06-29 18:35:35 +08:00
|
|
|
|
2013-08-30 00:09:35 +08:00
|
|
|
related_pk = tuple(getattr(instance, field.attname) for field in self.related.field.foreign_related_fields)
|
2015-07-24 19:51:40 +08:00
|
|
|
if None in related_pk:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign "%r": "%s" instance isn\'t saved in the database.' %
|
|
|
|
(value, instance._meta.object_name)
|
|
|
|
)
|
2012-10-10 03:36:35 +08:00
|
|
|
|
2009-03-31 06:38:19 +08:00
|
|
|
# Set the value of the related field to the value of the related object's related field
|
2013-03-25 00:40:40 +08:00
|
|
|
for index, field in enumerate(self.related.field.local_related_fields):
|
|
|
|
setattr(value, field.attname, related_pk[index])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-06-05 08:39:32 +08:00
|
|
|
# Since we already know what the related object is, seed the related
|
2008-06-29 18:35:35 +08:00
|
|
|
# object caches now, too. This avoids another db hit if you get the
|
2008-06-05 08:39:32 +08:00
|
|
|
# object you just set.
|
|
|
|
setattr(instance, self.cache_name, value)
|
|
|
|
setattr(value, self.related.field.get_cache_name(), instance)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2014-03-21 19:45:38 +08:00
|
|
|
class ReverseSingleRelatedObjectDescriptor(object):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Accessor to the related object on the forward side of a many-to-one or
|
|
|
|
one-to-one relation.
|
|
|
|
|
|
|
|
In the example::
|
|
|
|
|
|
|
|
class Choice(Model):
|
|
|
|
poll = ForeignKey(Place, related_name='choices')
|
|
|
|
|
|
|
|
`choice.poll` is a ReverseSingleRelatedObjectDescriptor instance.
|
|
|
|
"""
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __init__(self, field_with_rel):
|
|
|
|
self.field = field_with_rel
|
2011-10-08 00:05:53 +08:00
|
|
|
self.cache_name = self.field.get_cache_name()
|
|
|
|
|
2013-12-09 03:12:01 +08:00
|
|
|
@cached_property
|
|
|
|
def RelatedObjectDoesNotExist(self):
|
|
|
|
# The exception can't be created at initialization time since the
|
2015-02-26 22:19:17 +08:00
|
|
|
# related model might not be resolved yet; `rel.model` might still be
|
2013-12-09 03:12:01 +08:00
|
|
|
# a string model reference.
|
|
|
|
return type(
|
|
|
|
str('RelatedObjectDoesNotExist'),
|
2015-02-26 22:19:17 +08:00
|
|
|
(self.field.remote_field.model.DoesNotExist, AttributeError),
|
2013-12-09 03:12:01 +08:00
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
def is_cached(self, instance):
|
|
|
|
return hasattr(instance, self.cache_name)
|
|
|
|
|
2013-09-30 13:05:43 +08:00
|
|
|
def get_queryset(self, **hints):
|
2015-02-26 22:19:17 +08:00
|
|
|
manager = self.field.remote_field.model._default_manager
|
2011-10-08 00:05:53 +08:00
|
|
|
# If the related manager indicates that it should be used for
|
|
|
|
# related fields, respect that.
|
2014-09-24 01:41:32 +08:00
|
|
|
if not getattr(manager, 'use_for_related_fields', False):
|
2015-02-26 22:19:17 +08:00
|
|
|
manager = self.field.remote_field.model._base_manager
|
2014-09-24 01:41:32 +08:00
|
|
|
return manager.db_manager(hints=hints).all()
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
2014-01-17 02:24:39 +08:00
|
|
|
if queryset is None:
|
2014-09-24 01:41:32 +08:00
|
|
|
queryset = self.get_queryset()
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset._add_hints(instance=instances[0])
|
2013-11-07 01:25:05 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
rel_obj_attr = self.field.get_foreign_related_value
|
|
|
|
instance_attr = self.field.get_local_related_value
|
2014-12-07 05:00:09 +08:00
|
|
|
instances_dict = {instance_attr(inst): inst for inst in instances}
|
2013-11-13 12:42:12 +08:00
|
|
|
related_field = self.field.foreign_related_fields[0]
|
|
|
|
|
|
|
|
# FIXME: This will need to be revisited when we introduce support for
|
|
|
|
# composite fields. In the meantime we take this practical approach to
|
|
|
|
# solve a regression on 1.6 when the reverse manager in hidden
|
|
|
|
# (related_name ends with a '+'). Refs #21410.
|
2014-04-12 09:27:34 +08:00
|
|
|
# The check for len(...) == 1 is a special case that allows the query
|
|
|
|
# to be join-less and smaller. Refs #21760.
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.field.remote_field.is_hidden() or len(self.field.foreign_related_fields) == 1:
|
2013-11-13 12:42:12 +08:00
|
|
|
query = {'%s__in' % related_field.name: set(instance_attr(inst)[0] for inst in instances)}
|
|
|
|
else:
|
|
|
|
query = {'%s__in' % self.field.related_query_name(): instances}
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset = queryset.filter(**query)
|
2013-11-13 12:42:12 +08:00
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
# Since we're going to assign directly in the cache,
|
|
|
|
# we must manage the reverse relation cache manually.
|
2015-02-26 22:19:17 +08:00
|
|
|
if not self.field.remote_field.multiple:
|
|
|
|
rel_obj_cache_name = self.field.remote_field.get_cache_name()
|
2014-01-17 02:24:39 +08:00
|
|
|
for rel_obj in queryset:
|
2012-05-24 19:25:01 +08:00
|
|
|
instance = instances_dict[rel_obj_attr(rel_obj)]
|
|
|
|
setattr(rel_obj, rel_obj_cache_name, instance)
|
2014-01-17 02:24:39 +08:00
|
|
|
return queryset, rel_obj_attr, instance_attr, True, self.cache_name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = getattr(instance, self.cache_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
except AttributeError:
|
2013-03-25 00:40:40 +08:00
|
|
|
val = self.field.get_local_related_value(instance)
|
|
|
|
if None in val:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = None
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2014-12-07 05:00:09 +08:00
|
|
|
params = {
|
|
|
|
rh_field.attname: getattr(instance, lh_field.attname)
|
|
|
|
for lh_field, rh_field in self.field.related_fields}
|
2013-04-02 05:30:43 +08:00
|
|
|
qs = self.get_queryset(instance=instance)
|
2013-07-23 20:06:02 +08:00
|
|
|
extra_filter = self.field.get_extra_descriptor_filter(instance)
|
|
|
|
if isinstance(extra_filter, dict):
|
|
|
|
params.update(extra_filter)
|
|
|
|
qs = qs.filter(**params)
|
|
|
|
else:
|
|
|
|
qs = qs.filter(extra_filter, **params)
|
2012-04-10 20:29:25 +08:00
|
|
|
# Assuming the database enforces foreign keys, this won't fail.
|
2013-07-23 20:06:02 +08:00
|
|
|
rel_obj = qs.get()
|
2015-02-26 22:19:17 +08:00
|
|
|
if not self.field.remote_field.multiple:
|
|
|
|
setattr(rel_obj, self.field.remote_field.get_cache_name(), instance)
|
2011-10-08 00:05:53 +08:00
|
|
|
setattr(instance, self.cache_name, rel_obj)
|
2012-04-10 20:29:25 +08:00
|
|
|
if rel_obj is None and not self.field.null:
|
2013-12-09 03:12:01 +08:00
|
|
|
raise self.RelatedObjectDoesNotExist(
|
|
|
|
"%s has no %s." % (self.field.model.__name__, self.field.name)
|
|
|
|
)
|
2012-04-10 20:29:25 +08:00
|
|
|
else:
|
2006-05-02 09:31:56 +08:00
|
|
|
return rel_obj
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2008-06-05 08:39:32 +08:00
|
|
|
# If null=True, we can assign null here, but otherwise the value needs
|
|
|
|
# to be an instance of the related class.
|
2013-07-08 08:39:54 +08:00
|
|
|
if value is None and self.field.null is False:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign None: "%s.%s" does not allow null values.' %
|
|
|
|
(instance._meta.object_name, self.field.name)
|
|
|
|
)
|
2015-02-26 22:19:17 +08:00
|
|
|
elif value is not None and not isinstance(value, self.field.remote_field.model):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign "%r": "%s.%s" must be a "%s" instance.' % (
|
|
|
|
value,
|
|
|
|
instance._meta.object_name,
|
|
|
|
self.field.name,
|
2015-02-26 22:19:17 +08:00
|
|
|
self.field.remote_field.model._meta.object_name,
|
2013-12-13 04:23:24 +08:00
|
|
|
)
|
|
|
|
)
|
2010-01-22 22:30:06 +08:00
|
|
|
elif value is not None:
|
2009-12-22 23:18:51 +08:00
|
|
|
if instance._state.db is None:
|
2010-01-22 22:30:06 +08:00
|
|
|
instance._state.db = router.db_for_write(instance.__class__, instance=value)
|
|
|
|
elif value._state.db is None:
|
|
|
|
value._state.db = router.db_for_write(value.__class__, instance=instance)
|
|
|
|
elif value._state.db is not None and instance._state.db is not None:
|
|
|
|
if not router.allow_relation(value, instance):
|
2013-05-01 04:28:09 +08:00
|
|
|
raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value)
|
2008-06-29 18:35:35 +08:00
|
|
|
|
2009-06-15 22:30:51 +08:00
|
|
|
# If we're setting the value of a OneToOneField to None, we need to clear
|
|
|
|
# out the cache on any old related object. Otherwise, deleting the
|
|
|
|
# previously-related object will also cause this object to be deleted,
|
|
|
|
# which is wrong.
|
|
|
|
if value is None:
|
|
|
|
# Look up the previously-related object, which may still be available
|
|
|
|
# since we've not yet cleared out the related field.
|
|
|
|
# Use the cache directly, instead of the accessor; if we haven't
|
|
|
|
# populated the cache, then we don't care - we're only accessing
|
|
|
|
# the object to invalidate the accessor cache, so there's no
|
|
|
|
# need to populate the cache just to expire it again.
|
2012-04-10 20:29:25 +08:00
|
|
|
related = getattr(instance, self.cache_name, None)
|
2009-06-15 22:30:51 +08:00
|
|
|
|
|
|
|
# If we've got an old related object, we need to clear out its
|
|
|
|
# cache. This cache also might not exist if the related object
|
|
|
|
# hasn't been accessed yet.
|
2012-04-10 20:29:25 +08:00
|
|
|
if related is not None:
|
2015-02-26 22:19:17 +08:00
|
|
|
setattr(related, self.field.remote_field.get_cache_name(), None)
|
2009-06-15 22:30:51 +08:00
|
|
|
|
2014-05-19 16:45:55 +08:00
|
|
|
for lh_field, rh_field in self.field.related_fields:
|
2013-03-25 00:40:40 +08:00
|
|
|
setattr(instance, lh_field.attname, None)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-05-19 16:45:55 +08:00
|
|
|
# Set the values of the related field.
|
|
|
|
else:
|
|
|
|
for lh_field, rh_field in self.field.related_fields:
|
2014-06-30 21:13:35 +08:00
|
|
|
setattr(instance, lh_field.attname, getattr(value, rh_field.attname))
|
2014-05-19 16:45:55 +08:00
|
|
|
|
2008-06-05 08:39:32 +08:00
|
|
|
# Since we already know what the related object is, seed the related
|
2012-04-10 20:29:25 +08:00
|
|
|
# object caches now, too. This avoids another db hit if you get the
|
2008-06-05 08:39:32 +08:00
|
|
|
# object you just set.
|
2012-04-10 20:29:25 +08:00
|
|
|
setattr(instance, self.cache_name, value)
|
2015-02-26 22:19:17 +08:00
|
|
|
if value is not None and not self.field.remote_field.multiple:
|
|
|
|
setattr(value, self.field.remote_field.get_cache_name(), instance)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
def create_foreign_related_manager(superclass, rel):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Factory function to create a manager that subclasses another manager
|
|
|
|
(generally the default manager of a given model) and adds behaviors
|
|
|
|
specific to many-to-one relations.
|
|
|
|
"""
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
class RelatedManager(superclass):
|
|
|
|
def __init__(self, instance):
|
|
|
|
super(RelatedManager, self).__init__()
|
2015-02-16 17:03:34 +08:00
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
self.instance = instance
|
2015-02-16 17:03:34 +08:00
|
|
|
self.model = rel.related_model
|
|
|
|
self.field = rel.field
|
|
|
|
|
|
|
|
self.core_filters = {self.field.name: instance}
|
2013-09-19 01:31:07 +08:00
|
|
|
|
|
|
|
def __call__(self, **kwargs):
|
|
|
|
# We use **kwargs rather than a kwarg argument to enforce the
|
|
|
|
# `manager='manager_name'` syntax.
|
|
|
|
manager = getattr(self.model, kwargs.pop('manager'))
|
2015-02-16 17:03:34 +08:00
|
|
|
manager_class = create_foreign_related_manager(manager.__class__, rel)
|
2013-09-19 01:31:07 +08:00
|
|
|
return manager_class(self.instance)
|
2013-09-30 11:21:21 +08:00
|
|
|
do_not_call_in_templates = True
|
2013-09-19 01:31:07 +08:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
try:
|
2015-02-16 17:03:34 +08:00
|
|
|
return self.instance._prefetched_objects_cache[self.field.related_query_name()]
|
2013-09-19 01:31:07 +08:00
|
|
|
except (AttributeError, KeyError):
|
|
|
|
db = self._db or router.db_for_read(self.model, instance=self.instance)
|
|
|
|
empty_strings_as_null = connections[db].features.interprets_empty_strings_as_nulls
|
2013-09-30 13:05:43 +08:00
|
|
|
qs = super(RelatedManager, self).get_queryset()
|
|
|
|
qs._add_hints(instance=self.instance)
|
|
|
|
if self._db:
|
|
|
|
qs = qs.using(self._db)
|
|
|
|
qs = qs.filter(**self.core_filters)
|
2015-02-16 17:03:34 +08:00
|
|
|
for field in self.field.foreign_related_fields:
|
2013-09-19 01:31:07 +08:00
|
|
|
val = getattr(self.instance, field.attname)
|
|
|
|
if val is None or (val == '' and empty_strings_as_null):
|
|
|
|
return qs.none()
|
2015-02-16 17:03:34 +08:00
|
|
|
qs._known_related_objects = {self.field: {self.instance.pk: self.instance}}
|
2013-09-19 01:31:07 +08:00
|
|
|
return qs
|
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = super(RelatedManager, self).get_queryset()
|
|
|
|
|
|
|
|
queryset._add_hints(instance=instances[0])
|
|
|
|
queryset = queryset.using(queryset._db or self._db)
|
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
rel_obj_attr = self.field.get_local_related_value
|
|
|
|
instance_attr = self.field.get_foreign_related_value
|
2014-12-07 05:00:09 +08:00
|
|
|
instances_dict = {instance_attr(inst): inst for inst in instances}
|
2015-02-16 17:03:34 +08:00
|
|
|
query = {'%s__in' % self.field.name: instances}
|
2013-11-07 01:25:05 +08:00
|
|
|
queryset = queryset.filter(**query)
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
# Since we just bypassed this class' get_queryset(), we must manage
|
|
|
|
# the reverse relation manually.
|
2013-11-07 01:25:05 +08:00
|
|
|
for rel_obj in queryset:
|
2013-09-19 01:31:07 +08:00
|
|
|
instance = instances_dict[rel_obj_attr(rel_obj)]
|
2015-02-16 17:03:34 +08:00
|
|
|
setattr(rel_obj, self.field.name, instance)
|
|
|
|
cache_name = self.field.related_query_name()
|
2013-11-07 01:25:05 +08:00
|
|
|
return queryset, rel_obj_attr, instance_attr, False, cache_name
|
2013-09-19 01:31:07 +08:00
|
|
|
|
2015-03-15 09:25:33 +08:00
|
|
|
def add(self, *objs, **kwargs):
|
|
|
|
bulk = kwargs.pop('bulk', True)
|
2013-09-27 02:16:50 +08:00
|
|
|
objs = list(objs)
|
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
2015-03-15 09:25:33 +08:00
|
|
|
|
|
|
|
def check_and_update_obj(obj):
|
|
|
|
if not isinstance(obj, self.model):
|
|
|
|
raise TypeError("'%s' instance expected, got %r" % (
|
|
|
|
self.model._meta.object_name, obj,
|
|
|
|
))
|
|
|
|
setattr(obj, self.field.name, self.instance)
|
|
|
|
|
|
|
|
if bulk:
|
|
|
|
pks = []
|
2013-09-27 02:16:50 +08:00
|
|
|
for obj in objs:
|
2015-03-15 09:25:33 +08:00
|
|
|
check_and_update_obj(obj)
|
|
|
|
if obj._state.adding or obj._state.db != db:
|
|
|
|
raise ValueError(
|
|
|
|
"%r instance isn't saved. Use bulk=False or save "
|
|
|
|
"the object first." % obj
|
|
|
|
)
|
|
|
|
pks.append(obj.pk)
|
|
|
|
self.model._base_manager.using(db).filter(pk__in=pks).update(**{
|
|
|
|
self.field.name: self.instance,
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
for obj in objs:
|
|
|
|
check_and_update_obj(obj)
|
|
|
|
obj.save()
|
2013-09-19 01:31:07 +08:00
|
|
|
add.alters_data = True
|
|
|
|
|
|
|
|
def create(self, **kwargs):
|
2015-02-16 17:03:34 +08:00
|
|
|
kwargs[self.field.name] = self.instance
|
2013-09-19 01:31:07 +08:00
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
return super(RelatedManager, self.db_manager(db)).create(**kwargs)
|
|
|
|
create.alters_data = True
|
|
|
|
|
|
|
|
def get_or_create(self, **kwargs):
|
2015-02-16 17:03:34 +08:00
|
|
|
kwargs[self.field.name] = self.instance
|
2013-09-19 01:31:07 +08:00
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
return super(RelatedManager, self.db_manager(db)).get_or_create(**kwargs)
|
|
|
|
get_or_create.alters_data = True
|
|
|
|
|
2014-10-08 04:27:31 +08:00
|
|
|
def update_or_create(self, **kwargs):
|
2015-02-16 17:03:34 +08:00
|
|
|
kwargs[self.field.name] = self.instance
|
2014-10-08 04:27:31 +08:00
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
return super(RelatedManager, self.db_manager(db)).update_or_create(**kwargs)
|
|
|
|
update_or_create.alters_data = True
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
# remove() and clear() are only provided if the ForeignKey can have a value of null.
|
2015-02-16 17:03:34 +08:00
|
|
|
if rel.field.null:
|
2013-11-22 01:14:16 +08:00
|
|
|
def remove(self, *objs, **kwargs):
|
2013-09-27 07:35:53 +08:00
|
|
|
if not objs:
|
|
|
|
return
|
2013-11-22 01:14:16 +08:00
|
|
|
bulk = kwargs.pop('bulk', True)
|
2015-02-16 17:03:34 +08:00
|
|
|
val = self.field.get_foreign_related_value(self.instance)
|
2013-09-27 07:35:53 +08:00
|
|
|
old_ids = set()
|
2013-09-19 01:31:07 +08:00
|
|
|
for obj in objs:
|
|
|
|
# Is obj actually part of this descriptor set?
|
2015-02-16 17:03:34 +08:00
|
|
|
if self.field.get_local_related_value(obj) == val:
|
2013-09-27 07:35:53 +08:00
|
|
|
old_ids.add(obj.pk)
|
2013-09-19 01:31:07 +08:00
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
raise self.field.remote_field.model.DoesNotExist("%r is not related to %r." % (obj, self.instance))
|
2013-11-22 01:14:16 +08:00
|
|
|
self._clear(self.filter(pk__in=old_ids), bulk)
|
2013-09-19 01:31:07 +08:00
|
|
|
remove.alters_data = True
|
|
|
|
|
2013-11-22 01:14:16 +08:00
|
|
|
def clear(self, **kwargs):
|
|
|
|
bulk = kwargs.pop('bulk', True)
|
|
|
|
self._clear(self, bulk)
|
2013-09-19 01:31:07 +08:00
|
|
|
clear.alters_data = True
|
|
|
|
|
2013-11-22 01:14:16 +08:00
|
|
|
def _clear(self, queryset, bulk):
|
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
queryset = queryset.using(db)
|
|
|
|
if bulk:
|
2014-03-30 02:57:28 +08:00
|
|
|
# `QuerySet.update()` is intrinsically atomic.
|
2015-02-16 17:03:34 +08:00
|
|
|
queryset.update(**{self.field.name: None})
|
2013-11-22 01:14:16 +08:00
|
|
|
else:
|
2014-03-21 21:21:43 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
2013-11-22 01:14:16 +08:00
|
|
|
for obj in queryset:
|
2015-02-16 17:03:34 +08:00
|
|
|
setattr(obj, self.field.name, None)
|
|
|
|
obj.save(update_fields=[self.field.name])
|
2013-11-22 01:14:16 +08:00
|
|
|
_clear.alters_data = True
|
|
|
|
|
2015-01-30 02:15:27 +08:00
|
|
|
def set(self, objs, **kwargs):
|
2015-02-05 17:52:26 +08:00
|
|
|
# Force evaluation of `objs` in case it's a queryset whose value
|
|
|
|
# could be affected by `manager.clear()`. Refs #19816.
|
|
|
|
objs = tuple(objs)
|
|
|
|
|
2015-03-15 09:25:33 +08:00
|
|
|
bulk = kwargs.pop('bulk', True)
|
2015-01-30 02:15:27 +08:00
|
|
|
clear = kwargs.pop('clear', False)
|
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
if self.field.null:
|
2015-01-30 02:15:27 +08:00
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
if clear:
|
|
|
|
self.clear()
|
2015-03-15 09:25:33 +08:00
|
|
|
self.add(*objs, bulk=bulk)
|
2015-01-30 02:15:27 +08:00
|
|
|
else:
|
|
|
|
old_objs = set(self.using(db).all())
|
|
|
|
new_objs = []
|
|
|
|
for obj in objs:
|
|
|
|
if obj in old_objs:
|
|
|
|
old_objs.remove(obj)
|
|
|
|
else:
|
|
|
|
new_objs.append(obj)
|
|
|
|
|
2015-03-15 09:25:33 +08:00
|
|
|
self.remove(*old_objs, bulk=bulk)
|
|
|
|
self.add(*new_objs, bulk=bulk)
|
2015-01-30 02:15:27 +08:00
|
|
|
else:
|
2015-03-15 09:25:33 +08:00
|
|
|
self.add(*objs, bulk=bulk)
|
2015-01-30 02:15:27 +08:00
|
|
|
set.alters_data = True
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
return RelatedManager
|
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ForeignRelatedObjectsDescriptor(object):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Accessor to the related objects manager on the reverse side of a
|
|
|
|
many-to-one relation.
|
|
|
|
|
|
|
|
In the example::
|
|
|
|
|
|
|
|
class Choice(Model):
|
|
|
|
poll = ForeignKey(Place, related_name='choices')
|
|
|
|
|
|
|
|
``poll.choices`` is a ``ForeignRelatedObjectsDescriptor`` instance.
|
|
|
|
"""
|
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
def __init__(self, rel):
|
|
|
|
self.rel = rel
|
|
|
|
self.field = rel.field
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def related_manager_cls(self):
|
|
|
|
return create_foreign_related_manager(
|
|
|
|
self.rel.related_model._default_manager.__class__,
|
|
|
|
self.rel,
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-10-06 19:08:11 +08:00
|
|
|
return self.related_manager_cls(instance)
|
2009-03-20 11:57:12 +08:00
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2014-03-27 19:32:48 +08:00
|
|
|
manager = self.__get__(instance)
|
2015-01-30 02:15:27 +08:00
|
|
|
manager.set(value)
|
2009-03-20 11:57:12 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
def create_many_related_manager(superclass, rel, reverse):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Factory function to create a manager that subclasses another manager
|
|
|
|
(generally the default manager of a given model) and adds behaviors
|
|
|
|
specific to many-to-many relations.
|
|
|
|
"""
|
2011-09-29 23:30:29 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ManyRelatedManager(superclass):
|
2015-02-16 17:03:34 +08:00
|
|
|
def __init__(self, instance=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
super(ManyRelatedManager, self).__init__()
|
2013-03-25 00:40:40 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
self.instance = instance
|
|
|
|
|
|
|
|
if not reverse:
|
2015-02-26 22:19:17 +08:00
|
|
|
self.model = rel.model
|
2015-02-16 17:03:34 +08:00
|
|
|
self.query_field_name = rel.field.related_query_name()
|
|
|
|
self.prefetch_cache_name = rel.field.name
|
|
|
|
self.source_field_name = rel.field.m2m_field_name()
|
|
|
|
self.target_field_name = rel.field.m2m_reverse_field_name()
|
|
|
|
self.symmetrical = rel.symmetrical
|
|
|
|
else:
|
|
|
|
self.model = rel.related_model
|
|
|
|
self.query_field_name = rel.field.name
|
|
|
|
self.prefetch_cache_name = rel.field.related_query_name()
|
|
|
|
self.source_field_name = rel.field.m2m_reverse_field_name()
|
|
|
|
self.target_field_name = rel.field.m2m_field_name()
|
|
|
|
self.symmetrical = False
|
|
|
|
|
|
|
|
self.through = rel.through
|
|
|
|
self.reverse = reverse
|
|
|
|
|
|
|
|
self.source_field = self.through._meta.get_field(self.source_field_name)
|
|
|
|
self.target_field = self.through._meta.get_field(self.target_field_name)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
self.core_filters = {}
|
2015-02-16 17:03:34 +08:00
|
|
|
for lh_field, rh_field in self.source_field.related_fields:
|
|
|
|
self.core_filters['%s__%s' % (self.query_field_name, rh_field.name)] = getattr(instance, rh_field.attname)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
self.related_val = self.source_field.get_foreign_related_value(instance)
|
2013-03-25 00:40:40 +08:00
|
|
|
if None in self.related_val:
|
2012-10-28 22:47:07 +08:00
|
|
|
raise ValueError('"%r" needs to have a value for field "%s" before '
|
|
|
|
'this many-to-many relationship can be used.' %
|
2015-02-16 17:03:34 +08:00
|
|
|
(instance, self.source_field_name))
|
2012-10-28 22:47:07 +08:00
|
|
|
# Even if this relation is not to pk, we require still pk value.
|
|
|
|
# The wish is that the instance has been already saved to DB,
|
|
|
|
# although having a pk value isn't a guarantee of that.
|
|
|
|
if instance.pk is None:
|
|
|
|
raise ValueError("%r instance needs to have a primary key value before "
|
|
|
|
"a many-to-many relationship can be used." %
|
|
|
|
instance.__class__.__name__)
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
def __call__(self, **kwargs):
|
|
|
|
# We use **kwargs rather than a kwarg argument to enforce the
|
|
|
|
# `manager='manager_name'` syntax.
|
|
|
|
manager = getattr(self.model, kwargs.pop('manager'))
|
2015-02-16 17:03:34 +08:00
|
|
|
manager_class = create_many_related_manager(manager.__class__, rel, reverse)
|
|
|
|
return manager_class(instance=self.instance)
|
2013-09-30 11:21:21 +08:00
|
|
|
do_not_call_in_templates = True
|
2013-09-19 01:31:07 +08:00
|
|
|
|
2013-10-09 20:29:14 +08:00
|
|
|
def _build_remove_filters(self, removed_vals):
|
|
|
|
filters = Q(**{self.source_field_name: self.related_val})
|
|
|
|
# No need to add a subquery condition if removed_vals is a QuerySet without
|
|
|
|
# filters.
|
|
|
|
removed_vals_filters = (not isinstance(removed_vals, QuerySet) or
|
|
|
|
removed_vals._has_filters())
|
|
|
|
if removed_vals_filters:
|
|
|
|
filters &= Q(**{'%s__in' % self.target_field_name: removed_vals})
|
2013-09-27 07:35:53 +08:00
|
|
|
if self.symmetrical:
|
2013-10-09 20:29:14 +08:00
|
|
|
symmetrical_filters = Q(**{self.target_field_name: self.related_val})
|
|
|
|
if removed_vals_filters:
|
|
|
|
symmetrical_filters &= Q(
|
|
|
|
**{'%s__in' % self.source_field_name: removed_vals})
|
|
|
|
filters |= symmetrical_filters
|
2013-09-27 07:35:53 +08:00
|
|
|
return filters
|
|
|
|
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
2011-10-06 07:14:52 +08:00
|
|
|
try:
|
|
|
|
return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
|
|
|
|
except (AttributeError, KeyError):
|
2013-09-30 13:05:43 +08:00
|
|
|
qs = super(ManyRelatedManager, self).get_queryset()
|
|
|
|
qs._add_hints(instance=self.instance)
|
|
|
|
if self._db:
|
|
|
|
qs = qs.using(self._db)
|
|
|
|
return qs._next_is_sticky().filter(**self.core_filters)
|
2011-10-06 07:14:52 +08:00
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = super(ManyRelatedManager, self).get_queryset()
|
|
|
|
|
|
|
|
queryset._add_hints(instance=instances[0])
|
|
|
|
queryset = queryset.using(queryset._db or self._db)
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
query = {'%s__in' % self.query_field_name: instances}
|
2013-11-07 01:25:05 +08:00
|
|
|
queryset = queryset._next_is_sticky().filter(**query)
|
2011-10-06 07:14:52 +08:00
|
|
|
|
|
|
|
# M2M: need to annotate the query in order to get the primary model
|
|
|
|
# that the secondary model was actually related to. We know that
|
|
|
|
# there will already be a join on the join table, so we can just add
|
|
|
|
# the select.
|
|
|
|
|
|
|
|
# For non-autocreated 'through' models, can't assume we are
|
|
|
|
# dealing with PK values.
|
|
|
|
fk = self.through._meta.get_field(self.source_field_name)
|
|
|
|
join_table = self.through._meta.db_table
|
2013-11-07 01:25:05 +08:00
|
|
|
connection = connections[queryset.db]
|
2011-10-06 07:14:52 +08:00
|
|
|
qn = connection.ops.quote_name
|
2014-12-07 05:00:09 +08:00
|
|
|
queryset = queryset.extra(select={
|
|
|
|
'_prefetch_related_val_%s' % f.attname:
|
|
|
|
'%s.%s' % (qn(join_table), qn(f.column)) for f in fk.local_related_fields})
|
2014-09-04 20:15:09 +08:00
|
|
|
return (
|
|
|
|
queryset,
|
|
|
|
lambda result: tuple(
|
|
|
|
getattr(result, '_prefetch_related_val_%s' % f.attname)
|
|
|
|
for f in fk.local_related_fields
|
|
|
|
),
|
2015-06-05 19:40:51 +08:00
|
|
|
lambda inst: tuple(
|
|
|
|
f.get_db_prep_value(getattr(inst, f.attname), connection)
|
|
|
|
for f in fk.foreign_related_fields
|
|
|
|
),
|
2014-09-04 20:15:09 +08:00
|
|
|
False,
|
|
|
|
self.prefetch_cache_name,
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-02-09 21:54:46 +08:00
|
|
|
def add(self, *objs):
|
|
|
|
if not rel.through._meta.auto_created:
|
|
|
|
opts = self.through._meta
|
|
|
|
raise AttributeError(
|
2014-09-04 20:15:09 +08:00
|
|
|
"Cannot use add() on a ManyToManyField which specifies an "
|
|
|
|
"intermediary model. Use %s.%s's Manager instead." %
|
2014-02-09 21:54:46 +08:00
|
|
|
(opts.app_label, opts.object_name)
|
|
|
|
)
|
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
self._add_items(self.source_field_name, self.target_field_name, *objs)
|
|
|
|
|
|
|
|
# If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table
|
|
|
|
if self.symmetrical:
|
|
|
|
self._add_items(self.target_field_name, self.source_field_name, *objs)
|
2014-02-09 21:54:46 +08:00
|
|
|
add.alters_data = True
|
|
|
|
|
|
|
|
def remove(self, *objs):
|
|
|
|
if not rel.through._meta.auto_created:
|
|
|
|
opts = self.through._meta
|
|
|
|
raise AttributeError(
|
2014-09-04 20:15:09 +08:00
|
|
|
"Cannot use remove() on a ManyToManyField which specifies "
|
|
|
|
"an intermediary model. Use %s.%s's Manager instead." %
|
2014-02-09 21:54:46 +08:00
|
|
|
(opts.app_label, opts.object_name)
|
|
|
|
)
|
|
|
|
self._remove_items(self.source_field_name, self.target_field_name, *objs)
|
|
|
|
remove.alters_data = True
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def clear(self):
|
2013-09-27 07:35:53 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
signals.m2m_changed.send(sender=self.through, action="pre_clear",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=None, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
filters = self._build_remove_filters(super(ManyRelatedManager, self).get_queryset().using(db))
|
|
|
|
self.through._default_manager.using(db).filter(filters).delete()
|
2013-09-27 07:35:53 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
signals.m2m_changed.send(sender=self.through, action="post_clear",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=None, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
clear.alters_data = True
|
|
|
|
|
2015-01-30 02:15:27 +08:00
|
|
|
def set(self, objs, **kwargs):
|
|
|
|
if not rel.through._meta.auto_created:
|
|
|
|
opts = self.through._meta
|
|
|
|
raise AttributeError(
|
|
|
|
"Cannot set values on a ManyToManyField which specifies an "
|
|
|
|
"intermediary model. Use %s.%s's Manager instead." %
|
|
|
|
(opts.app_label, opts.object_name)
|
|
|
|
)
|
|
|
|
|
2015-02-05 17:52:26 +08:00
|
|
|
# Force evaluation of `objs` in case it's a queryset whose value
|
|
|
|
# could be affected by `manager.clear()`. Refs #19816.
|
|
|
|
objs = tuple(objs)
|
|
|
|
|
2015-01-30 02:15:27 +08:00
|
|
|
clear = kwargs.pop('clear', False)
|
|
|
|
|
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
if clear:
|
|
|
|
self.clear()
|
|
|
|
self.add(*objs)
|
|
|
|
else:
|
2015-02-26 22:02:29 +08:00
|
|
|
old_ids = set(self.using(db).values_list(self.target_field.target_field.attname, flat=True))
|
2015-01-30 02:15:27 +08:00
|
|
|
|
|
|
|
new_objs = []
|
|
|
|
for obj in objs:
|
|
|
|
fk_val = (self.target_field.get_foreign_related_value(obj)[0]
|
|
|
|
if isinstance(obj, self.model) else obj)
|
|
|
|
|
|
|
|
if fk_val in old_ids:
|
|
|
|
old_ids.remove(fk_val)
|
|
|
|
else:
|
|
|
|
new_objs.append(obj)
|
|
|
|
|
|
|
|
self.remove(*old_ids)
|
|
|
|
self.add(*new_objs)
|
|
|
|
set.alters_data = True
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def create(self, **kwargs):
|
2008-07-29 20:41:08 +08:00
|
|
|
# This check needs to be done here, since we can't later remove this
|
|
|
|
# from the method lookup table, as we do with add and remove.
|
2011-09-29 23:30:29 +08:00
|
|
|
if not self.through._meta.auto_created:
|
|
|
|
opts = self.through._meta
|
2013-12-13 04:23:24 +08:00
|
|
|
raise AttributeError(
|
2014-09-04 20:15:09 +08:00
|
|
|
"Cannot use create() on a ManyToManyField which specifies "
|
|
|
|
"an intermediary model. Use %s.%s's Manager instead." %
|
2013-12-13 04:23:24 +08:00
|
|
|
(opts.app_label, opts.object_name)
|
|
|
|
)
|
2010-01-22 22:30:06 +08:00
|
|
|
db = router.db_for_write(self.instance.__class__, instance=self.instance)
|
2010-10-28 20:57:04 +08:00
|
|
|
new_obj = super(ManyRelatedManager, self.db_manager(db)).create(**kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.add(new_obj)
|
|
|
|
return new_obj
|
|
|
|
create.alters_data = True
|
|
|
|
|
2008-08-17 04:59:06 +08:00
|
|
|
def get_or_create(self, **kwargs):
|
2010-01-22 22:30:06 +08:00
|
|
|
db = router.db_for_write(self.instance.__class__, instance=self.instance)
|
2014-10-08 04:27:31 +08:00
|
|
|
obj, created = super(ManyRelatedManager, self.db_manager(db)).get_or_create(**kwargs)
|
2008-08-17 04:59:06 +08:00
|
|
|
# We only need to add() if created because if we got an object back
|
|
|
|
# from get() then the relationship already exists.
|
|
|
|
if created:
|
|
|
|
self.add(obj)
|
|
|
|
return obj, created
|
|
|
|
get_or_create.alters_data = True
|
2014-10-08 04:27:31 +08:00
|
|
|
|
|
|
|
def update_or_create(self, **kwargs):
|
|
|
|
db = router.db_for_write(self.instance.__class__, instance=self.instance)
|
|
|
|
obj, created = super(ManyRelatedManager, self.db_manager(db)).update_or_create(**kwargs)
|
|
|
|
# We only need to add() if created because if we got an object back
|
|
|
|
# from get() then the relationship already exists.
|
|
|
|
if created:
|
|
|
|
self.add(obj)
|
|
|
|
return obj, created
|
|
|
|
update_or_create.alters_data = True
|
2008-08-17 04:59:06 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def _add_items(self, source_field_name, target_field_name, *objs):
|
2011-09-27 01:20:20 +08:00
|
|
|
# source_field_name: the PK fieldname in join table for the source object
|
|
|
|
# target_field_name: the PK fieldname in join table for the target object
|
2007-01-30 00:09:25 +08:00
|
|
|
# *objs - objects to add. Either object instances, or primary keys of object instances.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-12-20 21:57:17 +08:00
|
|
|
# If there aren't any objects, there is nothing to do.
|
2009-11-03 22:02:49 +08:00
|
|
|
from django.db.models import Model
|
2006-12-20 21:57:17 +08:00
|
|
|
if objs:
|
2007-01-30 00:09:25 +08:00
|
|
|
new_ids = set()
|
2006-12-20 22:06:27 +08:00
|
|
|
for obj in objs:
|
2007-01-30 00:09:25 +08:00
|
|
|
if isinstance(obj, self.model):
|
2010-01-22 22:30:06 +08:00
|
|
|
if not router.allow_relation(obj, self.instance):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot add "%r": instance is on database "%s", value is on database "%s"' %
|
|
|
|
(obj, self.instance._state.db, obj._state.db)
|
|
|
|
)
|
2013-08-20 22:13:41 +08:00
|
|
|
fk_val = self.through._meta.get_field(
|
|
|
|
target_field_name).get_foreign_related_value(obj)[0]
|
2012-10-28 22:47:07 +08:00
|
|
|
if fk_val is None:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot add "%r": the value for field "%s" is None' %
|
|
|
|
(obj, target_field_name)
|
|
|
|
)
|
2013-08-20 22:13:41 +08:00
|
|
|
new_ids.add(fk_val)
|
2009-03-31 06:41:49 +08:00
|
|
|
elif isinstance(obj, Model):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise TypeError(
|
|
|
|
"'%s' instance expected, got %r" %
|
|
|
|
(self.model._meta.object_name, obj)
|
|
|
|
)
|
2007-01-30 00:09:25 +08:00
|
|
|
else:
|
|
|
|
new_ids.add(obj)
|
2014-03-30 02:57:28 +08:00
|
|
|
|
2011-01-13 12:11:41 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
vals = (self.through._default_manager.using(db)
|
|
|
|
.values_list(target_field_name, flat=True)
|
|
|
|
.filter(**{
|
|
|
|
source_field_name: self.related_val[0],
|
|
|
|
'%s__in' % target_field_name: new_ids,
|
|
|
|
}))
|
2010-01-13 19:07:16 +08:00
|
|
|
new_ids = new_ids - set(vals)
|
2010-03-30 19:54:56 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
if self.reverse or source_field_name == self.source_field_name:
|
|
|
|
# Don't send the signal when we are inserting the
|
|
|
|
# duplicate data row for symmetrical reverse entries.
|
|
|
|
signals.m2m_changed.send(sender=self.through, action='pre_add',
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=new_ids, using=db)
|
|
|
|
|
|
|
|
# Add the ones that aren't there already
|
|
|
|
self.through._default_manager.using(db).bulk_create([
|
|
|
|
self.through(**{
|
|
|
|
'%s_id' % source_field_name: self.related_val[0],
|
|
|
|
'%s_id' % target_field_name: obj_id,
|
|
|
|
})
|
|
|
|
for obj_id in new_ids
|
|
|
|
])
|
|
|
|
|
|
|
|
if self.reverse or source_field_name == self.source_field_name:
|
|
|
|
# Don't send the signal when we are inserting the
|
|
|
|
# duplicate data row for symmetrical reverse entries.
|
|
|
|
signals.m2m_changed.send(sender=self.through, action='post_add',
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=new_ids, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def _remove_items(self, source_field_name, target_field_name, *objs):
|
2011-09-27 01:20:20 +08:00
|
|
|
# source_field_name: the PK colname in join table for the source object
|
|
|
|
# target_field_name: the PK colname in join table for the target object
|
2006-05-02 09:31:56 +08:00
|
|
|
# *objs - objects to remove
|
2013-09-27 07:35:53 +08:00
|
|
|
if not objs:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Check that all the objects are of the right type
|
|
|
|
old_ids = set()
|
|
|
|
for obj in objs:
|
|
|
|
if isinstance(obj, self.model):
|
|
|
|
fk_val = self.target_field.get_foreign_related_value(obj)[0]
|
|
|
|
old_ids.add(fk_val)
|
|
|
|
else:
|
|
|
|
old_ids.add(obj)
|
2009-11-03 22:02:49 +08:00
|
|
|
|
2011-01-13 12:11:41 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
# Send a signal to the other end if need be.
|
|
|
|
signals.m2m_changed.send(sender=self.through, action="pre_remove",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=old_ids, using=db)
|
|
|
|
target_model_qs = super(ManyRelatedManager, self).get_queryset()
|
|
|
|
if target_model_qs._has_filters():
|
|
|
|
old_vals = target_model_qs.using(db).filter(**{
|
2015-02-26 22:02:29 +08:00
|
|
|
'%s__in' % self.target_field.target_field.attname: old_ids})
|
2014-03-30 02:57:28 +08:00
|
|
|
else:
|
|
|
|
old_vals = old_ids
|
|
|
|
filters = self._build_remove_filters(old_vals)
|
|
|
|
self.through._default_manager.using(db).filter(filters).delete()
|
2013-09-27 07:35:53 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
signals.m2m_changed.send(sender=self.through, action="post_remove",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=old_ids, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
return ManyRelatedManager
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
class ManyRelatedObjectsDescriptor(ForeignRelatedObjectsDescriptor):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Accessor to the related objects manager on the forward and reverse sides of
|
|
|
|
a many-to-many relation.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:06:42 +08:00
|
|
|
In the example::
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:06:42 +08:00
|
|
|
class Pizza(Model):
|
|
|
|
toppings = ManyToManyField(Topping, related_name='pizzas')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:06:42 +08:00
|
|
|
``pizza.toppings`` and ``topping.pizzas`` are ManyRelatedObjectsDescriptor
|
|
|
|
instances.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
def __init__(self, rel, reverse=False):
|
|
|
|
super(ManyRelatedObjectsDescriptor, self).__init__(rel)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
self.reverse = reverse
|
2009-11-13 20:34:41 +08:00
|
|
|
|
2011-09-10 03:22:28 +08:00
|
|
|
@property
|
|
|
|
def through(self):
|
2009-11-03 23:02:16 +08:00
|
|
|
# through is provided so that you have easy access to the through
|
2009-11-13 20:34:41 +08:00
|
|
|
# model (Book.authors.through) for inlines, etc. This is done as
|
|
|
|
# a property to ensure that the fully resolved value is returned.
|
2015-02-16 17:03:34 +08:00
|
|
|
return self.rel.through
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-09-30 18:41:25 +08:00
|
|
|
@cached_property
|
|
|
|
def related_manager_cls(self):
|
2015-02-26 22:19:17 +08:00
|
|
|
model = self.rel.related_model if self.reverse else self.rel.model
|
2011-09-30 18:41:25 +08:00
|
|
|
return create_many_related_manager(
|
2015-02-16 17:03:34 +08:00
|
|
|
model._default_manager.__class__,
|
|
|
|
self.rel,
|
|
|
|
reverse=self.reverse,
|
2006-05-02 09:31:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
class ForeignObjectRel(object):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Used by ForeignObject to store information about the relation.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:06:42 +08:00
|
|
|
``_meta.get_fields()`` returns this class to provide access to the field
|
|
|
|
flags for the reverse relation.
|
|
|
|
"""
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
auto_created = True
|
|
|
|
concrete = False
|
|
|
|
editable = False
|
|
|
|
is_relation = True
|
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
# Reverse relations are always nullable (Django can't enforce that a
|
|
|
|
# foreign key on the related model points to this model).
|
|
|
|
null = True
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, on_delete=None):
|
2013-03-25 00:40:40 +08:00
|
|
|
self.field = field
|
2015-02-26 22:19:17 +08:00
|
|
|
self.model = to
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
self.related_name = related_name
|
2013-06-27 21:44:21 +08:00
|
|
|
self.related_query_name = related_query_name
|
2013-03-25 00:40:40 +08:00
|
|
|
self.limit_choices_to = {} if limit_choices_to is None else limit_choices_to
|
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.parent_link = parent_link
|
2010-11-10 00:46:42 +08:00
|
|
|
self.on_delete = on_delete
|
2015-01-27 22:40:01 +08:00
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
self.symmetrical = False
|
2015-01-27 22:40:01 +08:00
|
|
|
self.multiple = True
|
2013-11-09 20:25:15 +08:00
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Some of the following cached_properties can't be initialized in
|
2013-11-09 20:25:15 +08:00
|
|
|
# __init__ as the field doesn't have its model yet. Calling these methods
|
|
|
|
# before field.contribute_to_class() has been called will result in
|
|
|
|
# AttributeError
|
2015-02-26 22:19:17 +08:00
|
|
|
@property
|
|
|
|
def to(self):
|
|
|
|
warnings.warn(
|
|
|
|
"Usage of ForeignObjectRel.to attribute has been deprecated. "
|
|
|
|
"Use the model attribute instead.",
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango20Warning, 2)
|
2015-02-26 22:19:17 +08:00
|
|
|
return self.model
|
2013-11-09 20:25:15 +08:00
|
|
|
|
|
|
|
@cached_property
|
2015-01-07 08:16:35 +08:00
|
|
|
def hidden(self):
|
|
|
|
return self.is_hidden()
|
2013-11-09 20:25:15 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def name(self):
|
2015-01-07 08:16:35 +08:00
|
|
|
return self.field.related_query_name()
|
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
@property
|
|
|
|
def remote_field(self):
|
|
|
|
return self.field
|
|
|
|
|
|
|
|
@property
|
|
|
|
def target_field(self):
|
|
|
|
"""
|
|
|
|
When filtering against this relation, returns the field on the remote
|
|
|
|
model against which the filtering should happen.
|
|
|
|
"""
|
|
|
|
target_fields = self.get_path_info()[-1].target_fields
|
|
|
|
if len(target_fields) > 1:
|
|
|
|
raise exceptions.FieldError("Can't use target_field for multicolumn relations.")
|
|
|
|
return target_fields[0]
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
@cached_property
|
|
|
|
def related_model(self):
|
|
|
|
if not self.field.model:
|
|
|
|
raise AttributeError(
|
|
|
|
"This property can't be accessed before self.field.contribute_to_class has been called.")
|
|
|
|
return self.field.model
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def many_to_many(self):
|
|
|
|
return self.field.many_to_many
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def many_to_one(self):
|
|
|
|
return self.field.one_to_many
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def one_to_many(self):
|
|
|
|
return self.field.many_to_one
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def one_to_one(self):
|
|
|
|
return self.field.one_to_one
|
|
|
|
|
2015-02-02 19:48:30 +08:00
|
|
|
def get_prep_lookup(self, lookup_name, value):
|
|
|
|
return self.field.get_prep_lookup(lookup_name, value)
|
|
|
|
|
2015-02-26 21:44:41 +08:00
|
|
|
def get_lookup(self, lookup_name):
|
|
|
|
return self.field.get_lookup(lookup_name)
|
|
|
|
|
2015-02-02 19:48:30 +08:00
|
|
|
def get_internal_type(self):
|
|
|
|
return self.field.get_internal_type()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def db_type(self):
|
|
|
|
return self.field.db_type
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
def __repr__(self):
|
2015-02-23 15:31:58 +08:00
|
|
|
return '<%s: %s.%s>' % (
|
|
|
|
type(self).__name__,
|
|
|
|
self.related_model._meta.app_label,
|
|
|
|
self.related_model._meta.model_name,
|
|
|
|
)
|
2013-11-09 20:25:15 +08:00
|
|
|
|
|
|
|
def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH,
|
|
|
|
limit_to_currently_related=False):
|
|
|
|
"""
|
2015-02-16 17:06:42 +08:00
|
|
|
Return choices with a default blank choices included, for use as
|
2013-11-09 20:25:15 +08:00
|
|
|
SelectField choices for this field.
|
|
|
|
|
|
|
|
Analog of django.db.models.fields.Field.get_choices(), provided
|
|
|
|
initially for utilization by RelatedFieldListFilter.
|
|
|
|
"""
|
|
|
|
first_choice = blank_choice if include_blank else []
|
2015-01-07 08:16:35 +08:00
|
|
|
queryset = self.related_model._default_manager.all()
|
2013-11-09 20:25:15 +08:00
|
|
|
if limit_to_currently_related:
|
|
|
|
queryset = queryset.complex_filter(
|
2015-01-07 08:16:35 +08:00
|
|
|
{'%s__isnull' % self.related_model._meta.model_name: False}
|
2013-11-09 20:25:15 +08:00
|
|
|
)
|
|
|
|
lst = [(x._get_pk_val(), smart_text(x)) for x in queryset]
|
|
|
|
return first_choice + lst
|
|
|
|
|
|
|
|
def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
|
|
|
|
# Defer to the actual field definition for db prep
|
|
|
|
return self.field.get_db_prep_lookup(lookup_type, value, connection=connection, prepared=prepared)
|
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
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def is_hidden(self):
|
|
|
|
"Should the related object be hidden?"
|
2015-01-07 08:16:35 +08:00
|
|
|
return self.related_name is not None and self.related_name[-1] == '+'
|
2009-11-03 22:02:49 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
def get_joining_columns(self):
|
|
|
|
return self.field.get_reverse_joining_columns()
|
|
|
|
|
|
|
|
def get_extra_restriction(self, where_class, alias, related_alias):
|
|
|
|
return self.field.get_extra_restriction(where_class, related_alias, alias)
|
|
|
|
|
2013-03-25 02:57:02 +08:00
|
|
|
def set_field_name(self):
|
|
|
|
"""
|
|
|
|
Sets the related field's name, this is not available until later stages
|
|
|
|
of app loading, so set_field_name is called from
|
|
|
|
set_attributes_from_rel()
|
|
|
|
"""
|
|
|
|
# By default foreign object doesn't relate to any remote field (for
|
|
|
|
# example custom multicolumn joins currently have no remote field).
|
|
|
|
self.field_name = None
|
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
def get_accessor_name(self, model=None):
|
|
|
|
# This method encapsulates the logic that decides what name to give an
|
|
|
|
# accessor descriptor that retrieves related many-to-one or
|
|
|
|
# many-to-many objects. It uses the lower-cased object_name + "_set",
|
|
|
|
# but this can be overridden with the "related_name" option.
|
|
|
|
# Due to backwards compatibility ModelForms need to be able to provide
|
|
|
|
# an alternate model. See BaseInlineFormSet.get_default_prefix().
|
2015-02-23 15:31:58 +08:00
|
|
|
opts = model._meta if model else self.related_model._meta
|
2015-01-07 08:16:35 +08:00
|
|
|
model = model or self.related_model
|
2013-11-09 20:25:15 +08:00
|
|
|
if self.multiple:
|
|
|
|
# If this is a symmetrical m2m relation on self, there is no reverse accessor.
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.symmetrical and model == self.model:
|
2013-11-09 20:25:15 +08:00
|
|
|
return None
|
|
|
|
if self.related_name:
|
|
|
|
return self.related_name
|
|
|
|
if opts.default_related_name:
|
2015-02-23 15:31:58 +08:00
|
|
|
return opts.default_related_name % {
|
|
|
|
'model_name': opts.model_name.lower(),
|
|
|
|
'app_label': opts.app_label.lower(),
|
2013-11-09 20:25:15 +08:00
|
|
|
}
|
|
|
|
return opts.model_name + ('_set' if self.multiple else '')
|
|
|
|
|
|
|
|
def get_cache_name(self):
|
|
|
|
return "_%s_cache" % self.get_accessor_name()
|
|
|
|
|
|
|
|
def get_path_info(self):
|
|
|
|
return self.field.get_reverse_path_info()
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
class ManyToOneRel(ForeignObjectRel):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Used by the ForeignKey field to store information about the relation.
|
|
|
|
|
|
|
|
``_meta.get_fields()`` returns this class to provide access to the field
|
|
|
|
flags for the reverse relation.
|
|
|
|
|
|
|
|
Note: Because we somewhat abuse the Rel objects by using them as reverse
|
|
|
|
fields we get the funny situation where
|
|
|
|
``ManyToOneRel.many_to_one == False`` and
|
|
|
|
``ManyToOneRel.one_to_many == True``. This is unfortunate but the actual
|
|
|
|
ManyToOneRel class is a private API and there is work underway to turn
|
|
|
|
reverse relations into actual fields.
|
|
|
|
"""
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, field_name, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, on_delete=None):
|
2013-03-25 00:40:40 +08:00
|
|
|
super(ManyToOneRel, self).__init__(
|
2015-01-27 22:40:01 +08:00
|
|
|
field, to,
|
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
parent_link=parent_link,
|
|
|
|
on_delete=on_delete,
|
|
|
|
)
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
self.field_name = field_name
|
|
|
|
|
2015-05-29 19:45:36 +08:00
|
|
|
def __getstate__(self):
|
|
|
|
state = self.__dict__.copy()
|
|
|
|
state.pop('related_model', None)
|
|
|
|
return state
|
|
|
|
|
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_related_field(self):
|
|
|
|
"""
|
2015-02-16 17:06:42 +08:00
|
|
|
Return the Field in the 'to' object to which this relationship is tied.
|
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
|
|
|
"""
|
2015-02-26 22:19:17 +08:00
|
|
|
field = self.model._meta.get_field(self.field_name)
|
2015-01-07 08:16:35 +08:00
|
|
|
if not field.concrete:
|
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
|
|
|
raise FieldDoesNotExist("No related field named '%s'" %
|
|
|
|
self.field_name)
|
2015-01-07 08:16:35 +08:00
|
|
|
return 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
|
|
|
|
2013-03-25 02:57:02 +08:00
|
|
|
def set_field_name(self):
|
2015-02-26 22:19:17 +08:00
|
|
|
self.field_name = self.field_name or self.model._meta.pk.name
|
2013-03-25 02:57:02 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +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
|
|
|
class OneToOneRel(ManyToOneRel):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Used by OneToOneField to store information about the relation.
|
|
|
|
|
|
|
|
``_meta.get_fields()`` returns this class to provide access to the field
|
|
|
|
flags for the reverse relation.
|
|
|
|
"""
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, field_name, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, on_delete=None):
|
|
|
|
super(OneToOneRel, self).__init__(
|
|
|
|
field, to, field_name,
|
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
parent_link=parent_link,
|
|
|
|
on_delete=on_delete,
|
|
|
|
)
|
|
|
|
|
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.multiple = False
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
class ManyToManyRel(ForeignObjectRel):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Used by ManyToManyField to store information about the relation.
|
|
|
|
|
|
|
|
``_meta.get_fields()`` returns this class to provide access to the field
|
|
|
|
flags for the reverse relation.
|
|
|
|
"""
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, symmetrical=True, through=None, through_fields=None,
|
|
|
|
db_constraint=True):
|
|
|
|
super(ManyToManyRel, self).__init__(
|
|
|
|
field, to,
|
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
)
|
|
|
|
|
2013-03-08 03:24:51 +08:00
|
|
|
if through and not db_constraint:
|
|
|
|
raise ValueError("Can't supply a through model and db_constraint=False")
|
2015-01-27 22:40:01 +08:00
|
|
|
self.through = through
|
|
|
|
|
2014-02-20 02:01:55 +08:00
|
|
|
if through_fields and not through:
|
|
|
|
raise ValueError("Cannot specify through_fields without a through model")
|
|
|
|
self.through_fields = through_fields
|
2015-01-27 22:40:01 +08:00
|
|
|
|
|
|
|
self.symmetrical = symmetrical
|
2013-03-08 03:24:51 +08:00
|
|
|
self.db_constraint = db_constraint
|
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
|
|
|
|
2009-03-25 12:41:40 +08:00
|
|
|
def get_related_field(self):
|
|
|
|
"""
|
2015-02-16 17:06:42 +08:00
|
|
|
Return the field in the 'to' object to which this relationship is tied.
|
2014-11-17 00:21:33 +08:00
|
|
|
Provided for symmetry with ManyToOneRel.
|
2009-03-25 12:41:40 +08:00
|
|
|
"""
|
2014-11-17 00:21:33 +08:00
|
|
|
opts = self.through._meta
|
|
|
|
if self.through_fields:
|
|
|
|
field = opts.get_field(self.through_fields[0])
|
|
|
|
else:
|
|
|
|
for field in opts.fields:
|
2015-02-26 22:19:17 +08:00
|
|
|
rel = getattr(field, 'remote_field', None)
|
|
|
|
if rel and rel.model == self.model:
|
2014-11-17 00:21:33 +08:00
|
|
|
break
|
|
|
|
return field.foreign_related_fields[0]
|
2009-03-25 12:41:40 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
class ForeignObject(RelatedField):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Abstraction of the ForeignKey relation, supports multi-column relations.
|
|
|
|
"""
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
many_to_many = False
|
2015-02-14 02:55:36 +08:00
|
|
|
many_to_one = True
|
|
|
|
one_to_many = False
|
2015-01-07 08:16:35 +08:00
|
|
|
one_to_one = False
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
requires_unique_target = True
|
2013-08-20 13:52:32 +08:00
|
|
|
related_accessor_class = ForeignRelatedObjectsDescriptor
|
2015-01-27 22:40:01 +08:00
|
|
|
rel_class = ForeignObjectRel
|
2013-03-25 00:40:40 +08:00
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
def __init__(self, to, on_delete, from_fields, to_fields, rel=None, related_name=None,
|
2015-01-27 22:40:01 +08:00
|
|
|
related_query_name=None, limit_choices_to=None, parent_link=False,
|
2015-07-22 22:43:21 +08:00
|
|
|
swappable=True, **kwargs):
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
if rel is None:
|
|
|
|
rel = self.rel_class(
|
2013-03-25 00:40:40 +08:00
|
|
|
self, to,
|
2015-01-27 22:40:01 +08:00
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
parent_link=parent_link,
|
|
|
|
on_delete=on_delete,
|
2013-03-25 00:40:40 +08:00
|
|
|
)
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
super(ForeignObject, self).__init__(rel=rel, **kwargs)
|
|
|
|
|
|
|
|
self.from_fields = from_fields
|
|
|
|
self.to_fields = to_fields
|
|
|
|
self.swappable = swappable
|
2013-03-25 00:40:40 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def check(self, **kwargs):
|
|
|
|
errors = super(ForeignObject, self).check(**kwargs)
|
|
|
|
errors.extend(self._check_unique_target())
|
|
|
|
return errors
|
|
|
|
|
|
|
|
def _check_unique_target(self):
|
2015-02-26 22:19:17 +08:00
|
|
|
rel_is_string = isinstance(self.remote_field.model, six.string_types)
|
2014-01-20 10:45:21 +08:00
|
|
|
if rel_is_string or not self.requires_unique_target:
|
|
|
|
return []
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.foreign_related_fields
|
|
|
|
except FieldDoesNotExist:
|
|
|
|
return []
|
|
|
|
|
|
|
|
has_unique_field = any(rel_field.unique
|
|
|
|
for rel_field in self.foreign_related_fields)
|
|
|
|
if not has_unique_field and len(self.foreign_related_fields) > 1:
|
2014-03-03 18:18:39 +08:00
|
|
|
field_combination = ', '.join("'%s'" % rel_field.name
|
2014-01-20 10:45:21 +08:00
|
|
|
for rel_field in self.foreign_related_fields)
|
2015-02-26 22:19:17 +08:00
|
|
|
model_name = self.remote_field.model.__name__
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-09-04 20:15:09 +08:00
|
|
|
"None of the fields %s on model '%s' have a unique=True constraint."
|
|
|
|
% (field_combination, model_name),
|
2014-03-03 18:18:39 +08:00
|
|
|
hint=None,
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E310',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
elif not has_unique_field:
|
|
|
|
field_name = self.foreign_related_fields[0].name
|
2015-02-26 22:19:17 +08:00
|
|
|
model_name = self.remote_field.model.__name__
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("'%s.%s' must set unique=True "
|
|
|
|
"because it is referenced by a foreign key.") % (model_name, field_name),
|
2014-01-20 10:45:21 +08:00
|
|
|
hint=None,
|
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E311',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2013-12-06 22:21:13 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
name, path, args, kwargs = super(ForeignObject, self).deconstruct()
|
2015-07-22 22:43:21 +08:00
|
|
|
kwargs['on_delete'] = self.remote_field.on_delete
|
2013-12-06 22:21:13 +08:00
|
|
|
kwargs['from_fields'] = self.from_fields
|
|
|
|
kwargs['to_fields'] = self.to_fields
|
2015-07-22 22:43:21 +08:00
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.related_name is not None:
|
|
|
|
kwargs['related_name'] = self.remote_field.related_name
|
|
|
|
if self.remote_field.related_query_name is not None:
|
|
|
|
kwargs['related_query_name'] = self.remote_field.related_query_name
|
|
|
|
if self.remote_field.parent_link:
|
|
|
|
kwargs['parent_link'] = self.remote_field.parent_link
|
2014-01-15 22:20:47 +08:00
|
|
|
# Work out string form of "to"
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(self.remote_field.model, six.string_types):
|
|
|
|
kwargs['to'] = self.remote_field.model
|
2013-12-06 22:21:13 +08:00
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
kwargs['to'] = "%s.%s" % (self.remote_field.model._meta.app_label, self.remote_field.model._meta.object_name)
|
2014-01-15 22:20:47 +08:00
|
|
|
# If swappable is True, then see if we're actually pointing to the target
|
|
|
|
# of a swap.
|
|
|
|
swappable_setting = self.swappable_setting
|
|
|
|
if swappable_setting is not None:
|
|
|
|
# If it's already a settings reference, error
|
|
|
|
if hasattr(kwargs['to'], "setting_name"):
|
|
|
|
if kwargs['to'].setting_name != swappable_setting:
|
2014-09-04 20:15:09 +08:00
|
|
|
raise ValueError(
|
|
|
|
"Cannot deconstruct a ForeignKey pointing to a model "
|
|
|
|
"that is swapped in place of more than one model (%s and %s)"
|
|
|
|
% (kwargs['to'].setting_name, swappable_setting)
|
|
|
|
)
|
2014-01-15 22:20:47 +08:00
|
|
|
# Set it
|
|
|
|
from django.db.migrations.writer import SettingsReference
|
|
|
|
kwargs['to'] = SettingsReference(
|
|
|
|
kwargs['to'],
|
|
|
|
swappable_setting,
|
|
|
|
)
|
2013-12-06 22:21:13 +08:00
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
def resolve_related_fields(self):
|
|
|
|
if len(self.from_fields) < 1 or len(self.from_fields) != len(self.to_fields):
|
|
|
|
raise ValueError('Foreign Object from and to fields must be the same non-zero length')
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(self.remote_field.model, six.string_types):
|
|
|
|
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
|
2013-03-25 00:40:40 +08:00
|
|
|
related_fields = []
|
|
|
|
for index in range(len(self.from_fields)):
|
|
|
|
from_field_name = self.from_fields[index]
|
|
|
|
to_field_name = self.to_fields[index]
|
|
|
|
from_field = (self if from_field_name == 'self'
|
2015-01-07 08:16:35 +08:00
|
|
|
else self.opts.get_field(from_field_name))
|
2015-02-26 22:19:17 +08:00
|
|
|
to_field = (self.remote_field.model._meta.pk if to_field_name is None
|
|
|
|
else self.remote_field.model._meta.get_field(to_field_name))
|
2013-03-25 00:40:40 +08:00
|
|
|
related_fields.append((from_field, to_field))
|
|
|
|
return related_fields
|
|
|
|
|
|
|
|
@property
|
|
|
|
def related_fields(self):
|
|
|
|
if not hasattr(self, '_related_fields'):
|
|
|
|
self._related_fields = self.resolve_related_fields()
|
|
|
|
return self._related_fields
|
|
|
|
|
|
|
|
@property
|
|
|
|
def reverse_related_fields(self):
|
|
|
|
return [(rhs_field, lhs_field) for lhs_field, rhs_field in self.related_fields]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def local_related_fields(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(lhs_field for lhs_field, rhs_field in self.related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def foreign_related_fields(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_local_related_value(self, instance):
|
|
|
|
return self.get_instance_value_for_fields(instance, self.local_related_fields)
|
|
|
|
|
|
|
|
def get_foreign_related_value(self, instance):
|
|
|
|
return self.get_instance_value_for_fields(instance, self.foreign_related_fields)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_instance_value_for_fields(instance, fields):
|
2013-08-20 21:23:25 +08:00
|
|
|
ret = []
|
2014-05-13 00:46:47 +08:00
|
|
|
opts = instance._meta
|
2013-08-20 21:23:25 +08:00
|
|
|
for field in fields:
|
|
|
|
# Gotcha: in some cases (like fixture loading) a model can have
|
|
|
|
# different values in parent_ptr_id and parent's id. So, use
|
|
|
|
# instance.pk (that is, parent_ptr_id) when asked for instance.id.
|
|
|
|
if field.primary_key:
|
2013-08-20 22:13:41 +08:00
|
|
|
possible_parent_link = opts.get_ancestor_link(field.model)
|
2014-05-13 00:46:47 +08:00
|
|
|
if (not possible_parent_link or
|
|
|
|
possible_parent_link.primary_key or
|
|
|
|
possible_parent_link.model._meta.abstract):
|
2013-08-20 22:13:41 +08:00
|
|
|
ret.append(instance.pk)
|
|
|
|
continue
|
|
|
|
ret.append(getattr(instance, field.attname))
|
2013-08-20 21:23:25 +08:00
|
|
|
return tuple(ret)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_attname_column(self):
|
|
|
|
attname, column = super(ForeignObject, self).get_attname_column()
|
|
|
|
return attname, None
|
|
|
|
|
|
|
|
def get_joining_columns(self, reverse_join=False):
|
|
|
|
source = self.reverse_related_fields if reverse_join else self.related_fields
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple((lhs_field.column, rhs_field.column) for lhs_field, rhs_field in source)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_reverse_joining_columns(self):
|
|
|
|
return self.get_joining_columns(reverse_join=True)
|
|
|
|
|
|
|
|
def get_extra_descriptor_filter(self, instance):
|
|
|
|
"""
|
2015-02-16 17:06:42 +08:00
|
|
|
Return an extra filter condition for related object fetching when
|
2013-03-25 00:40:40 +08:00
|
|
|
user does 'instance.fieldname', that is the extra filter is used in
|
|
|
|
the descriptor of the field.
|
|
|
|
|
2013-07-23 20:06:02 +08:00
|
|
|
The filter should be either a dict usable in .filter(**kwargs) call or
|
|
|
|
a Q-object. The condition will be ANDed together with the relation's
|
|
|
|
joining columns.
|
2013-03-25 00:40:40 +08:00
|
|
|
|
2013-07-23 20:06:02 +08:00
|
|
|
A parallel method is get_extra_restriction() which is used in
|
2013-03-25 00:40:40 +08:00
|
|
|
JOIN and subquery conditions.
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def get_extra_restriction(self, where_class, alias, related_alias):
|
|
|
|
"""
|
2015-02-16 17:06:42 +08:00
|
|
|
Return a pair condition used for joining and subquery pushdown. The
|
2014-11-16 09:56:42 +08:00
|
|
|
condition is something that responds to as_sql(compiler, connection)
|
|
|
|
method.
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
Note that currently referring both the 'alias' and 'related_alias'
|
|
|
|
will not work in some conditions, like subquery pushdown.
|
|
|
|
|
|
|
|
A parallel method is get_extra_descriptor_filter() which is used in
|
|
|
|
instance.fieldname related object fetching.
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_path_info(self):
|
|
|
|
"""
|
|
|
|
Get path from this field to the related model.
|
|
|
|
"""
|
2015-02-26 22:19:17 +08:00
|
|
|
opts = self.remote_field.model._meta
|
2013-03-25 00:40:40 +08:00
|
|
|
from_opts = self.model._meta
|
|
|
|
return [PathInfo(from_opts, opts, self.foreign_related_fields, self, False, True)]
|
|
|
|
|
|
|
|
def get_reverse_path_info(self):
|
|
|
|
"""
|
|
|
|
Get path from the related model to this field's model.
|
|
|
|
"""
|
|
|
|
opts = self.model._meta
|
2015-02-26 22:19:17 +08:00
|
|
|
from_opts = self.remote_field.model._meta
|
|
|
|
pathinfos = [PathInfo(from_opts, opts, (opts.pk,), self.remote_field, not self.unique, False)]
|
2013-03-25 00:40:40 +08:00
|
|
|
return pathinfos
|
|
|
|
|
2015-02-02 19:48:30 +08:00
|
|
|
def get_lookup(self, lookup_name):
|
|
|
|
if lookup_name == 'in':
|
|
|
|
return RelatedIn
|
|
|
|
elif lookup_name == 'exact':
|
|
|
|
return RelatedExact
|
|
|
|
elif lookup_name == 'gt':
|
|
|
|
return RelatedGreaterThan
|
|
|
|
elif lookup_name == 'gte':
|
|
|
|
return RelatedGreaterThanOrEqual
|
|
|
|
elif lookup_name == 'lt':
|
|
|
|
return RelatedLessThan
|
|
|
|
elif lookup_name == 'lte':
|
|
|
|
return RelatedLessThanOrEqual
|
|
|
|
elif lookup_name != 'isnull':
|
|
|
|
raise TypeError('Related Field got invalid lookup: %s' % lookup_name)
|
|
|
|
return super(ForeignObject, self).get_lookup(lookup_name)
|
|
|
|
|
|
|
|
def get_transform(self, *args, **kwargs):
|
|
|
|
raise NotImplementedError('Relational fields do not support transforms.')
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def attnames(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(field.attname for field in self.local_related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_defaults(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(field.get_default() for field in self.local_related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name, virtual_only=False):
|
|
|
|
super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
|
|
|
|
setattr(cls, self.name, ReverseSingleRelatedObjectDescriptor(self))
|
|
|
|
|
|
|
|
def contribute_to_related_class(self, cls, related):
|
|
|
|
# Internal FK's - i.e., those with a related name ending with '+' -
|
|
|
|
# and swapped models don't get a related descriptor.
|
2015-02-26 22:19:17 +08:00
|
|
|
if not self.remote_field.is_hidden() and not related.related_model._meta.swapped:
|
2013-08-20 13:52:32 +08:00
|
|
|
setattr(cls, related.get_accessor_name(), self.related_accessor_class(related))
|
2014-02-02 03:23:31 +08:00
|
|
|
# While 'limit_choices_to' might be a callable, simply pass
|
|
|
|
# it along for later - this is too early because it's still
|
|
|
|
# model load time.
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.limit_choices_to:
|
|
|
|
cls._meta.related_fkey_lookups.append(self.remote_field.limit_choices_to)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ForeignKey(ForeignObject):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Provide a many-to-one relation by adding a column to the local model
|
|
|
|
to hold the remote value.
|
|
|
|
|
|
|
|
By default ForeignKey will target the pk of the remote model but this
|
|
|
|
behavior can be changed by using the ``to_field`` argument.
|
|
|
|
"""
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
many_to_many = False
|
2015-02-14 02:55:36 +08:00
|
|
|
many_to_one = True
|
|
|
|
one_to_many = False
|
2015-01-07 08:16:35 +08:00
|
|
|
one_to_one = False
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
rel_class = ManyToOneRel
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
empty_strings_allowed = False
|
2010-01-05 11:56:19 +08:00
|
|
|
default_error_messages = {
|
2014-07-02 12:43:07 +08:00
|
|
|
'invalid': _('%(model)s instance with %(field)s %(value)r does not exist.')
|
2010-01-05 11:56:19 +08:00
|
|
|
}
|
|
|
|
description = _("Foreign Key (type determined by related field)")
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
def __init__(self, to, on_delete=None, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, to_field=None,
|
2015-01-27 22:40:01 +08:00
|
|
|
db_constraint=True, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2013-12-30 01:42:12 +08:00
|
|
|
to._meta.model_name
|
2015-01-27 22:40:01 +08:00
|
|
|
except AttributeError:
|
2014-09-04 20:15:09 +08:00
|
|
|
assert isinstance(to, six.string_types), (
|
|
|
|
"%s(%r) is invalid. First parameter to ForeignKey must be "
|
|
|
|
"either a model, a model name, or the string %r" % (
|
|
|
|
self.__class__.__name__, to,
|
|
|
|
RECURSIVE_RELATIONSHIP_CONSTANT,
|
|
|
|
)
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2009-11-10 23:21:12 +08:00
|
|
|
# For backwards compatibility purposes, we need to *try* and set
|
|
|
|
# the to_field during FK construction. It won't be guaranteed to
|
|
|
|
# be correct until contribute_to_class is called. Refs #12190.
|
|
|
|
to_field = to_field or (to._meta.pk and to._meta.pk.name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
if on_delete is None:
|
|
|
|
warnings.warn(
|
|
|
|
"on_delete will be a required arg for %s in Django 2.0. "
|
|
|
|
"Set it to models.CASCADE if you want to maintain the current default behavior. "
|
|
|
|
"See https://docs.djangoproject.com/en/%s/ref/models/fields/"
|
|
|
|
"#django.db.models.ForeignKey.on_delete" % (
|
|
|
|
self.__class__.__name__,
|
|
|
|
get_docs_version(),
|
|
|
|
),
|
|
|
|
RemovedInDjango20Warning, 2)
|
|
|
|
on_delete = CASCADE
|
|
|
|
|
|
|
|
elif not callable(on_delete):
|
|
|
|
warnings.warn(
|
|
|
|
"The signature for {0} will change in Django 2.0. "
|
|
|
|
"Pass to_field='{1}' as a kwarg instead of as an arg.".format(
|
|
|
|
self.__class__.__name__,
|
|
|
|
on_delete,
|
|
|
|
),
|
|
|
|
RemovedInDjango20Warning, 2)
|
|
|
|
on_delete, to_field = to_field, on_delete
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
kwargs['rel'] = self.rel_class(
|
2013-03-25 00:40:40 +08:00
|
|
|
self, to, to_field,
|
2015-01-27 22:40:01 +08:00
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
parent_link=parent_link,
|
|
|
|
on_delete=on_delete,
|
2010-11-10 00:46:42 +08:00
|
|
|
)
|
2015-01-27 22:40:01 +08:00
|
|
|
|
|
|
|
kwargs['db_index'] = kwargs.get('db_index', True)
|
|
|
|
|
|
|
|
super(ForeignKey, self).__init__(
|
2015-07-22 22:43:21 +08:00
|
|
|
to, on_delete, from_fields=['self'], to_fields=[to_field], **kwargs)
|
2015-01-27 22:40:01 +08:00
|
|
|
|
|
|
|
self.db_constraint = db_constraint
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def check(self, **kwargs):
|
|
|
|
errors = super(ForeignKey, self).check(**kwargs)
|
|
|
|
errors.extend(self._check_on_delete())
|
2014-09-09 01:38:07 +08:00
|
|
|
errors.extend(self._check_unique())
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
|
|
|
def _check_on_delete(self):
|
2015-02-26 22:19:17 +08:00
|
|
|
on_delete = getattr(self.remote_field, 'on_delete', None)
|
2014-01-20 10:45:21 +08:00
|
|
|
if on_delete == SET_NULL and not self.null:
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
'Field specifies on_delete=SET_NULL, but cannot be null.',
|
|
|
|
hint='Set null=True argument on the field, or change the on_delete rule.',
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E320',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
elif on_delete == SET_DEFAULT and not self.has_default():
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
'Field specifies on_delete=SET_DEFAULT, but has no default value.',
|
|
|
|
hint='Set a default value, or change the on_delete rule.',
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E321',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2014-09-09 01:38:07 +08:00
|
|
|
def _check_unique(self, **kwargs):
|
|
|
|
return [
|
|
|
|
checks.Warning(
|
|
|
|
'Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.',
|
|
|
|
hint='ForeignKey(unique=True) is usually better served by a OneToOneField.',
|
|
|
|
obj=self,
|
|
|
|
id='fields.W342',
|
|
|
|
)
|
|
|
|
] if self.unique else []
|
|
|
|
|
2013-05-18 19:48:57 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
name, path, args, kwargs = super(ForeignKey, self).deconstruct()
|
2013-12-06 22:21:13 +08:00
|
|
|
del kwargs['to_fields']
|
|
|
|
del kwargs['from_fields']
|
2013-05-18 19:48:57 +08:00
|
|
|
# Handle the simpler arguments
|
|
|
|
if self.db_index:
|
|
|
|
del kwargs['db_index']
|
|
|
|
else:
|
|
|
|
kwargs['db_index'] = False
|
|
|
|
if self.db_constraint is not True:
|
|
|
|
kwargs['db_constraint'] = self.db_constraint
|
|
|
|
# Rel needs more work.
|
2015-02-26 22:19:17 +08:00
|
|
|
to_meta = getattr(self.remote_field.model, "_meta", None)
|
|
|
|
if self.remote_field.field_name and (not to_meta or (to_meta.pk and self.remote_field.field_name != to_meta.pk.name)):
|
|
|
|
kwargs['to_field'] = self.remote_field.field_name
|
2013-05-18 19:48:57 +08:00
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
@property
|
2015-02-26 22:02:29 +08:00
|
|
|
def target_field(self):
|
2013-03-25 00:40:40 +08:00
|
|
|
return self.foreign_related_fields[0]
|
2012-12-17 23:09:07 +08:00
|
|
|
|
|
|
|
def get_reverse_path_info(self):
|
|
|
|
"""
|
|
|
|
Get path from the related model to this field's model.
|
|
|
|
"""
|
|
|
|
opts = self.model._meta
|
2015-02-26 22:19:17 +08:00
|
|
|
from_opts = self.remote_field.model._meta
|
|
|
|
pathinfos = [PathInfo(from_opts, opts, (opts.pk,), self.remote_field, not self.unique, False)]
|
2013-03-25 00:40:40 +08:00
|
|
|
return pathinfos
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
def validate(self, value, model_instance):
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.parent_link:
|
2010-01-05 11:56:19 +08:00
|
|
|
return
|
|
|
|
super(ForeignKey, self).validate(value, model_instance)
|
2010-04-27 23:05:38 +08:00
|
|
|
if value is None:
|
2010-01-05 11:56:19 +08:00
|
|
|
return
|
2010-01-21 11:26:14 +08:00
|
|
|
|
2010-11-17 06:09:13 +08:00
|
|
|
using = router.db_for_read(model_instance.__class__, instance=model_instance)
|
2015-02-26 22:19:17 +08:00
|
|
|
qs = self.remote_field.model._default_manager.using(using).filter(
|
|
|
|
**{self.remote_field.field_name: value}
|
2013-10-18 06:27:45 +08:00
|
|
|
)
|
2014-02-02 03:23:31 +08:00
|
|
|
qs = qs.complex_filter(self.get_limit_choices_to())
|
2010-01-21 11:26:14 +08:00
|
|
|
if not qs.exists():
|
2013-06-06 02:55:05 +08:00
|
|
|
raise exceptions.ValidationError(
|
|
|
|
self.error_messages['invalid'],
|
|
|
|
code='invalid',
|
2014-07-02 12:43:07 +08:00
|
|
|
params={
|
2015-02-26 22:19:17 +08:00
|
|
|
'model': self.remote_field.model._meta.verbose_name, 'pk': value,
|
|
|
|
'field': self.remote_field.field_name, 'value': value,
|
2014-11-04 06:48:03 +08:00
|
|
|
}, # 'pk' is included for backwards compatibility
|
2013-06-06 02:55:05 +08:00
|
|
|
)
|
2010-01-05 11:56:19 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_attname(self):
|
|
|
|
return '%s_id' % self.name
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
def get_attname_column(self):
|
|
|
|
attname = self.get_attname()
|
|
|
|
column = self.db_column or attname
|
|
|
|
return attname, column
|
|
|
|
|
2008-03-20 14:56:23 +08:00
|
|
|
def get_default(self):
|
|
|
|
"Here we check if the default value is an object and return the to_field if so."
|
|
|
|
field_default = super(ForeignKey, self).get_default()
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(field_default, self.remote_field.model):
|
2015-02-26 22:02:29 +08:00
|
|
|
return getattr(field_default, self.target_field.attname)
|
2008-03-20 14:56:23 +08:00
|
|
|
return field_default
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_db_prep_save(self, value, connection):
|
2013-09-30 03:56:04 +08:00
|
|
|
if value is None or (value == '' and
|
2015-02-26 22:02:29 +08:00
|
|
|
(not self.target_field.empty_strings_allowed or
|
2013-09-30 03:56:04 +08:00
|
|
|
connection.features.interprets_empty_strings_as_nulls)):
|
2006-05-02 09:31:56 +08:00
|
|
|
return None
|
|
|
|
else:
|
2015-02-26 22:02:29 +08:00
|
|
|
return self.target_field.get_db_prep_save(value, connection=connection)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-02 19:48:30 +08:00
|
|
|
def get_db_prep_value(self, value, connection, prepared=False):
|
2015-02-26 22:02:29 +08:00
|
|
|
return self.target_field.get_db_prep_value(value, connection, prepared)
|
2015-02-02 19:48:30 +08:00
|
|
|
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
def value_to_string(self, obj):
|
2006-05-02 09:31:56 +08:00
|
|
|
if not obj:
|
|
|
|
# In required many-to-one fields with only one available choice,
|
|
|
|
# select that one available choice. Note: For SelectFields
|
2008-07-19 07:54:34 +08:00
|
|
|
# we have to check that the length of choices is *2*, not 1,
|
|
|
|
# because SelectFields always have an initial "blank" value.
|
|
|
|
if not self.blank and self.choices:
|
2006-05-02 09:31:56 +08:00
|
|
|
choice_list = self.get_choices_default()
|
2008-07-19 07:54:34 +08:00
|
|
|
if len(choice_list) == 2:
|
2012-07-21 16:00:10 +08:00
|
|
|
return smart_text(choice_list[1][0])
|
2013-03-25 00:40:40 +08:00
|
|
|
return super(ForeignKey, self).value_to_string(obj)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def contribute_to_related_class(self, cls, related):
|
2013-03-25 00:40:40 +08:00
|
|
|
super(ForeignKey, self).contribute_to_related_class(cls, related)
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.field_name is None:
|
|
|
|
self.remote_field.field_name = cls._meta.pk.name
|
2008-09-01 08:49:03 +08:00
|
|
|
|
2007-01-22 14:32:14 +08:00
|
|
|
def formfield(self, **kwargs):
|
2009-12-22 23:18:51 +08:00
|
|
|
db = kwargs.pop('using', None)
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(self.remote_field.model, six.string_types):
|
2011-08-12 22:15:08 +08:00
|
|
|
raise ValueError("Cannot create form field for %r yet, because "
|
|
|
|
"its related model %r has not been loaded yet" %
|
2015-02-26 22:19:17 +08:00
|
|
|
(self.name, self.remote_field.model))
|
2008-09-02 06:43:38 +08:00
|
|
|
defaults = {
|
|
|
|
'form_class': forms.ModelChoiceField,
|
2015-02-26 22:19:17 +08:00
|
|
|
'queryset': self.remote_field.model._default_manager.using(db),
|
|
|
|
'to_field_name': self.remote_field.field_name,
|
2008-09-02 06:43:38 +08:00
|
|
|
}
|
2007-01-22 14:32:14 +08:00
|
|
|
defaults.update(kwargs)
|
2007-04-28 21:55:24 +08:00
|
|
|
return super(ForeignKey, self).formfield(**defaults)
|
2006-12-27 13:23:21 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def db_type(self, connection):
|
2007-07-20 14:28:56 +08:00
|
|
|
# The database column type of a ForeignKey is the column type
|
|
|
|
# of the field to which it points. An exception is if the ForeignKey
|
|
|
|
# points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField,
|
|
|
|
# in which case the column type is simply that of an IntegerField.
|
2008-09-01 08:49:03 +08:00
|
|
|
# If the database needs similar types for key fields however, the only
|
|
|
|
# thing we can do is making AutoField an IntegerField.
|
2015-02-26 22:02:29 +08:00
|
|
|
rel_field = self.target_field
|
2008-09-01 08:49:03 +08:00
|
|
|
if (isinstance(rel_field, AutoField) or
|
|
|
|
(not connection.features.related_fields_match_type and
|
|
|
|
isinstance(rel_field, (PositiveIntegerField,
|
|
|
|
PositiveSmallIntegerField)))):
|
2009-12-22 23:18:51 +08:00
|
|
|
return IntegerField().db_type(connection=connection)
|
|
|
|
return rel_field.db_type(connection=connection)
|
2007-07-20 14:28:56 +08:00
|
|
|
|
2012-09-08 03:40:59 +08:00
|
|
|
def db_parameters(self, connection):
|
|
|
|
return {"type": self.db_type(connection), "check": []}
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def convert_empty_strings(self, value, expression, connection, context):
|
2015-02-15 03:37:12 +08:00
|
|
|
if (not value) and isinstance(value, six.string_types):
|
|
|
|
return None
|
|
|
|
return value
|
|
|
|
|
|
|
|
def get_db_converters(self, connection):
|
|
|
|
converters = super(ForeignKey, self).get_db_converters(connection)
|
|
|
|
if connection.features.interprets_empty_strings_as_nulls:
|
|
|
|
converters += [self.convert_empty_strings]
|
|
|
|
return converters
|
|
|
|
|
|
|
|
def get_col(self, alias, output_field=None):
|
2015-02-26 22:02:29 +08:00
|
|
|
return super(ForeignKey, self).get_col(alias, output_field or self.target_field)
|
2015-02-15 03:37:12 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +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
|
|
|
class OneToOneField(ForeignKey):
|
2009-12-17 02:13:34 +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
|
|
|
A OneToOneField is essentially the same as a ForeignKey, with the exception
|
2015-02-16 17:06:42 +08:00
|
|
|
that it always carries a "unique" constraint with it and the reverse
|
|
|
|
relation always returns the object pointed to (since there will only ever
|
|
|
|
be one), rather than returning a list.
|
2009-12-17 02:13:34 +08:00
|
|
|
"""
|
2015-02-16 17:06:42 +08:00
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
many_to_many = False
|
|
|
|
many_to_one = False
|
|
|
|
one_to_many = False
|
|
|
|
one_to_one = True
|
|
|
|
|
2013-08-20 13:52:32 +08:00
|
|
|
related_accessor_class = SingleRelatedObjectDescriptor
|
2015-01-27 22:40:01 +08:00
|
|
|
rel_class = OneToOneRel
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
description = _("One-to-one relationship")
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
def __init__(self, to, on_delete=None, to_field=None, **kwargs):
|
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
|
|
|
kwargs['unique'] = True
|
2015-07-22 22:43:21 +08:00
|
|
|
|
|
|
|
if on_delete is None:
|
|
|
|
warnings.warn(
|
|
|
|
"on_delete will be a required arg for %s in Django 2.0. "
|
|
|
|
"Set it to models.CASCADE if you want to maintain the current default behavior. "
|
|
|
|
"See https://docs.djangoproject.com/en/%s/ref/models/fields/"
|
|
|
|
"#django.db.models.ForeignKey.on_delete" % (
|
|
|
|
self.__class__.__name__,
|
|
|
|
get_docs_version(),
|
|
|
|
),
|
|
|
|
RemovedInDjango20Warning, 2)
|
|
|
|
on_delete = CASCADE
|
|
|
|
|
|
|
|
elif not callable(on_delete):
|
|
|
|
warnings.warn(
|
|
|
|
"The signature for {0} will change in Django 2.0. "
|
|
|
|
"Pass to_field='{1}' as a kwarg instead of as an arg.".format(
|
|
|
|
self.__class__.__name__,
|
|
|
|
on_delete,
|
|
|
|
),
|
|
|
|
RemovedInDjango20Warning, 2)
|
|
|
|
to_field = on_delete
|
|
|
|
on_delete = CASCADE # Avoid warning in superclass
|
|
|
|
|
|
|
|
super(OneToOneField, self).__init__(to, on_delete, to_field=to_field, **kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-05-18 19:48:57 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
name, path, args, kwargs = super(OneToOneField, self).deconstruct()
|
|
|
|
if "unique" in kwargs:
|
|
|
|
del kwargs['unique']
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2008-08-23 03:27:26 +08:00
|
|
|
def formfield(self, **kwargs):
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.parent_link:
|
2008-08-23 03:27:26 +08:00
|
|
|
return None
|
|
|
|
return super(OneToOneField, self).formfield(**kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
def save_form_data(self, instance, data):
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(data, self.remote_field.model):
|
2010-01-05 11:56:19 +08:00
|
|
|
setattr(instance, self.name, data)
|
|
|
|
else:
|
|
|
|
setattr(instance, self.attname, data)
|
|
|
|
|
2014-09-09 01:38:07 +08:00
|
|
|
def _check_unique(self, **kwargs):
|
2015-02-16 17:06:42 +08:00
|
|
|
# Override ForeignKey since check isn't applicable here.
|
2014-09-09 01:38:07 +08:00
|
|
|
return []
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def create_many_to_many_intermediary_model(field, klass):
|
|
|
|
from django.db import models
|
2015-03-03 16:43:56 +08:00
|
|
|
|
|
|
|
def set_managed(model, related, through):
|
|
|
|
through._meta.managed = model._meta.managed or related._meta.managed
|
|
|
|
|
|
|
|
to_model = resolve_relation(klass, field.remote_field.model)
|
2009-11-03 22:02:49 +08:00
|
|
|
name = '%s_%s' % (klass._meta.object_name, field.name)
|
2015-03-03 16:43:56 +08:00
|
|
|
lazy_related_operation(set_managed, klass, to_model, name)
|
|
|
|
|
|
|
|
to = make_model_tuple(to_model)[1]
|
|
|
|
from_ = klass._meta.model_name
|
|
|
|
if to == from_:
|
|
|
|
to = 'to_%s' % to
|
|
|
|
from_ = 'from_%s' % from_
|
|
|
|
|
2014-05-06 01:50:51 +08:00
|
|
|
meta = type(str('Meta'), (object,), {
|
2009-11-03 22:02:49 +08:00
|
|
|
'db_table': field._get_m2m_db_table(klass._meta),
|
|
|
|
'auto_created': klass,
|
2009-11-20 08:59:38 +08:00
|
|
|
'app_label': klass._meta.app_label,
|
2011-10-15 05:49:43 +08:00
|
|
|
'db_tablespace': klass._meta.db_tablespace,
|
2010-04-27 20:35:49 +08:00
|
|
|
'unique_together': (from_, to),
|
2010-04-27 22:42:55 +08:00
|
|
|
'verbose_name': '%(from)s-%(to)s relationship' % {'from': from_, 'to': to},
|
|
|
|
'verbose_name_plural': '%(from)s-%(to)s relationships' % {'from': from_, 'to': to},
|
2013-12-24 19:25:17 +08:00
|
|
|
'apps': field.model._meta.apps,
|
2009-11-03 22:02:49 +08:00
|
|
|
})
|
2009-11-05 19:57:28 +08:00
|
|
|
# Construct and return the new class.
|
2013-04-06 01:59:15 +08:00
|
|
|
return type(str(name), (models.Model,), {
|
2009-11-03 22:02:49 +08:00
|
|
|
'Meta': meta,
|
2009-11-20 08:59:38 +08:00
|
|
|
'__module__': klass.__module__,
|
2014-09-04 20:15:09 +08:00
|
|
|
from_: models.ForeignKey(
|
|
|
|
klass,
|
|
|
|
related_name='%s+' % name,
|
|
|
|
db_tablespace=field.db_tablespace,
|
2015-02-26 22:19:17 +08:00
|
|
|
db_constraint=field.remote_field.db_constraint,
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=CASCADE,
|
2014-09-04 20:15:09 +08:00
|
|
|
),
|
|
|
|
to: models.ForeignKey(
|
|
|
|
to_model,
|
|
|
|
related_name='%s+' % name,
|
|
|
|
db_tablespace=field.db_tablespace,
|
2015-02-26 22:19:17 +08:00
|
|
|
db_constraint=field.remote_field.db_constraint,
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=CASCADE,
|
2014-09-04 20:15:09 +08:00
|
|
|
)
|
2009-11-03 22:02:49 +08:00
|
|
|
})
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
class ManyToManyField(RelatedField):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Provide a many-to-many relation by using an intermediary model that
|
|
|
|
holds two ForeignKey fields pointed at the two sides of the relation.
|
|
|
|
|
|
|
|
Unless a ``through`` model was provided, ManyToManyField will use the
|
|
|
|
create_many_to_many_intermediary_model factory to automatically generate
|
|
|
|
the intermediary model.
|
|
|
|
"""
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
many_to_many = True
|
|
|
|
many_to_one = False
|
|
|
|
one_to_many = False
|
|
|
|
one_to_one = False
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
rel_class = ManyToManyRel
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
description = _("Many-to-many relationship")
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, to, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, symmetrical=None, through=None,
|
|
|
|
through_fields=None, db_constraint=True, db_table=None,
|
|
|
|
swappable=True, **kwargs):
|
2008-08-19 22:17:24 +08:00
|
|
|
try:
|
2014-01-20 10:45:21 +08:00
|
|
|
to._meta
|
2015-01-27 22:40:01 +08:00
|
|
|
except AttributeError:
|
2014-09-04 20:15:09 +08:00
|
|
|
assert isinstance(to, six.string_types), (
|
|
|
|
"%s(%r) is invalid. First parameter to ManyToManyField must be "
|
|
|
|
"either a model, a model name, or the string %r" %
|
|
|
|
(self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
|
|
|
|
)
|
|
|
|
# Class names must be ASCII in Python 2.x, so we forcibly coerce it
|
|
|
|
# here to break early if there's a problem.
|
2011-08-25 14:07:28 +08:00
|
|
|
to = str(to)
|
2015-01-27 22:40:01 +08:00
|
|
|
|
|
|
|
if symmetrical is None:
|
|
|
|
symmetrical = (to == RECURSIVE_RELATIONSHIP_CONSTANT)
|
|
|
|
|
|
|
|
if through is not None:
|
|
|
|
assert db_table is None, (
|
|
|
|
"Cannot specify a db_table if an intermediary model is used."
|
|
|
|
)
|
|
|
|
|
|
|
|
kwargs['rel'] = self.rel_class(
|
2013-11-09 20:25:15 +08:00
|
|
|
self, to,
|
2015-01-27 22:40:01 +08:00
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
symmetrical=symmetrical,
|
|
|
|
through=through,
|
|
|
|
through_fields=through_fields,
|
2013-03-08 03:24:51 +08:00
|
|
|
db_constraint=db_constraint,
|
|
|
|
)
|
2015-02-26 22:19:17 +08:00
|
|
|
self.has_null_arg = 'null' in kwargs
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2013-03-08 03:24:51 +08:00
|
|
|
super(ManyToManyField, self).__init__(**kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
self.db_table = db_table
|
|
|
|
self.swappable = swappable
|
2015-02-26 22:19:17 +08:00
|
|
|
# Many-to-many fields are always nullable.
|
|
|
|
self.null = True
|
2015-01-27 22:40:01 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def check(self, **kwargs):
|
|
|
|
errors = super(ManyToManyField, self).check(**kwargs)
|
|
|
|
errors.extend(self._check_unique(**kwargs))
|
|
|
|
errors.extend(self._check_relationship_model(**kwargs))
|
2014-07-09 04:42:40 +08:00
|
|
|
errors.extend(self._check_ignored_options(**kwargs))
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
|
|
|
def _check_unique(self, **kwargs):
|
|
|
|
if self.unique:
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
'ManyToManyFields cannot be unique.',
|
2014-01-20 10:45:21 +08:00
|
|
|
hint=None,
|
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E330',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
return []
|
|
|
|
|
2014-07-09 04:42:40 +08:00
|
|
|
def _check_ignored_options(self, **kwargs):
|
|
|
|
warnings = []
|
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.has_null_arg:
|
2014-07-09 04:42:40 +08:00
|
|
|
warnings.append(
|
|
|
|
checks.Warning(
|
|
|
|
'null has no effect on ManyToManyField.',
|
|
|
|
hint=None,
|
|
|
|
obj=self,
|
|
|
|
id='fields.W340',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(self._validators) > 0:
|
|
|
|
warnings.append(
|
|
|
|
checks.Warning(
|
|
|
|
'ManyToManyField does not support validators.',
|
|
|
|
hint=None,
|
|
|
|
obj=self,
|
|
|
|
id='fields.W341',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return warnings
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def _check_relationship_model(self, from_model=None, **kwargs):
|
2015-02-26 22:19:17 +08:00
|
|
|
if hasattr(self.remote_field.through, '_meta'):
|
2014-02-20 02:01:55 +08:00
|
|
|
qualified_model_name = "%s.%s" % (
|
2015-02-26 22:19:17 +08:00
|
|
|
self.remote_field.through._meta.app_label, self.remote_field.through.__name__)
|
2014-02-20 02:01:55 +08:00
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
qualified_model_name = self.remote_field.through
|
2014-02-20 02:01:55 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = []
|
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.through not in apps.get_models(include_auto_created=True):
|
2014-01-20 10:45:21 +08:00
|
|
|
# The relationship model is not installed.
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("Field specifies a many-to-many relation through model "
|
2014-02-20 02:01:55 +08:00
|
|
|
"'%s', which has not been installed.") %
|
|
|
|
qualified_model_name,
|
2014-03-03 18:18:39 +08:00
|
|
|
hint=None,
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E331',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-03-07 19:56:28 +08:00
|
|
|
else:
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
assert from_model is not None, (
|
|
|
|
"ManyToManyField with intermediate "
|
|
|
|
"tables cannot be checked if you don't pass the model "
|
2014-01-20 10:45:21 +08:00
|
|
|
"where the field is attached to."
|
2015-01-27 22:40:01 +08:00
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
# Set some useful local variables
|
2015-03-03 16:43:56 +08:00
|
|
|
to_model = resolve_relation(from_model, self.remote_field.model)
|
2014-01-20 10:45:21 +08:00
|
|
|
from_model_name = from_model._meta.object_name
|
|
|
|
if isinstance(to_model, six.string_types):
|
|
|
|
to_model_name = to_model
|
|
|
|
else:
|
|
|
|
to_model_name = to_model._meta.object_name
|
2015-02-26 22:19:17 +08:00
|
|
|
relationship_model_name = self.remote_field.through._meta.object_name
|
2014-01-20 10:45:21 +08:00
|
|
|
self_referential = from_model == to_model
|
|
|
|
|
|
|
|
# Check symmetrical attribute.
|
2015-02-26 22:19:17 +08:00
|
|
|
if (self_referential and self.remote_field.symmetrical and
|
|
|
|
not self.remote_field.through._meta.auto_created):
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
'Many-to-many fields with intermediate tables must not be symmetrical.',
|
|
|
|
hint=None,
|
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E332',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Count foreign keys in intermediate model
|
|
|
|
if self_referential:
|
2015-02-26 22:19:17 +08:00
|
|
|
seen_self = sum(from_model == getattr(field.remote_field, 'model', None)
|
|
|
|
for field in self.remote_field.through._meta.fields)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
if seen_self > 2 and not self.remote_field.through_fields:
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("The model is used as an intermediate model by "
|
|
|
|
"'%s', but it has more than two foreign keys "
|
2014-02-20 02:01:55 +08:00
|
|
|
"to '%s', which is ambiguous. You must specify "
|
|
|
|
"which two foreign keys Django should use via the "
|
|
|
|
"through_fields keyword argument.") % (self, from_model_name),
|
|
|
|
hint=("Use through_fields to specify which two "
|
|
|
|
"foreign keys Django should use."),
|
2015-02-26 22:19:17 +08:00
|
|
|
obj=self.remote_field.through,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E333',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Count foreign keys in relationship model
|
2015-02-26 22:19:17 +08:00
|
|
|
seen_from = sum(from_model == getattr(field.remote_field, 'model', None)
|
|
|
|
for field in self.remote_field.through._meta.fields)
|
|
|
|
seen_to = sum(to_model == getattr(field.remote_field, 'model', None)
|
|
|
|
for field in self.remote_field.through._meta.fields)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
if seen_from > 1 and not self.remote_field.through_fields:
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("The model is used as an intermediate model by "
|
|
|
|
"'%s', but it has more than one foreign key "
|
2014-02-20 02:01:55 +08:00
|
|
|
"from '%s', which is ambiguous. You must specify "
|
|
|
|
"which foreign key Django should use via the "
|
|
|
|
"through_fields keyword argument.") % (self, from_model_name),
|
2014-01-20 10:45:21 +08:00
|
|
|
hint=('If you want to create a recursive relationship, '
|
|
|
|
'use ForeignKey("self", symmetrical=False, '
|
|
|
|
'through="%s").') % relationship_model_name,
|
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E334',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
if seen_to > 1 and not self.remote_field.through_fields:
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("The model is used as an intermediate model by "
|
|
|
|
"'%s', but it has more than one foreign key "
|
2014-02-20 02:01:55 +08:00
|
|
|
"to '%s', which is ambiguous. You must specify "
|
|
|
|
"which foreign key Django should use via the "
|
|
|
|
"through_fields keyword argument.") % (self, to_model_name),
|
2014-01-20 10:45:21 +08:00
|
|
|
hint=('If you want to create a recursive '
|
|
|
|
'relationship, use ForeignKey("self", '
|
|
|
|
'symmetrical=False, through="%s").') % relationship_model_name,
|
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E335',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if seen_from == 0 or seen_to == 0:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
("The model is used as an intermediate model by "
|
|
|
|
"'%s', but it does not have a foreign key to '%s' or '%s'.") % (
|
2014-01-20 10:45:21 +08:00
|
|
|
self, from_model_name, to_model_name
|
|
|
|
),
|
|
|
|
hint=None,
|
2015-02-26 22:19:17 +08:00
|
|
|
obj=self.remote_field.through,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E336',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
2014-03-07 19:56:28 +08:00
|
|
|
|
2015-02-16 17:06:42 +08:00
|
|
|
# Validate `through_fields`.
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.through_fields is not None:
|
2014-03-07 19:56:28 +08:00
|
|
|
# Validate that we're given an iterable of at least two items
|
2015-02-16 17:06:42 +08:00
|
|
|
# and that none of them is "falsy".
|
2015-02-26 22:19:17 +08:00
|
|
|
if not (len(self.remote_field.through_fields) >= 2 and
|
|
|
|
self.remote_field.through_fields[0] and self.remote_field.through_fields[1]):
|
2014-03-07 19:56:28 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
("Field specifies 'through_fields' but does not "
|
|
|
|
"provide the names of the two link fields that should be "
|
|
|
|
"used for the relation through model "
|
|
|
|
"'%s'.") % qualified_model_name,
|
|
|
|
hint=("Make sure you specify 'through_fields' as "
|
|
|
|
"through_fields=('field1', 'field2')"),
|
|
|
|
obj=self,
|
|
|
|
id='fields.E337',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Validate the given through fields -- they should be actual
|
|
|
|
# fields on the through model, and also be foreign keys to the
|
2015-02-16 17:06:42 +08:00
|
|
|
# expected models.
|
2014-03-07 19:56:28 +08:00
|
|
|
else:
|
2015-01-27 22:40:01 +08:00
|
|
|
assert from_model is not None, (
|
|
|
|
"ManyToManyField with intermediate "
|
|
|
|
"tables cannot be checked if you don't pass the model "
|
2014-03-07 19:56:28 +08:00
|
|
|
"where the field is attached to."
|
2015-01-27 22:40:01 +08:00
|
|
|
)
|
2014-03-07 19:56:28 +08:00
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
source, through, target = from_model, self.remote_field.through, self.remote_field.model
|
|
|
|
source_field_name, target_field_name = self.remote_field.through_fields[:2]
|
2014-03-07 19:56:28 +08:00
|
|
|
|
|
|
|
for field_name, related_model in ((source_field_name, source),
|
|
|
|
(target_field_name, target)):
|
|
|
|
|
|
|
|
possible_field_names = []
|
|
|
|
for f in through._meta.fields:
|
2015-02-26 22:19:17 +08:00
|
|
|
if hasattr(f, 'remote_field') and getattr(f.remote_field, 'model', None) == related_model:
|
2014-03-07 19:56:28 +08:00
|
|
|
possible_field_names.append(f.name)
|
|
|
|
if possible_field_names:
|
|
|
|
hint = ("Did you mean one of the following foreign "
|
|
|
|
"keys to '%s': %s?") % (related_model._meta.object_name,
|
|
|
|
', '.join(possible_field_names))
|
|
|
|
else:
|
|
|
|
hint = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
field = through._meta.get_field(field_name)
|
|
|
|
except FieldDoesNotExist:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
("The intermediary model '%s' has no field '%s'.") % (
|
|
|
|
qualified_model_name, field_name),
|
|
|
|
hint=hint,
|
|
|
|
obj=self,
|
|
|
|
id='fields.E338',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
if not (hasattr(field, 'remote_field') and
|
|
|
|
getattr(field.remote_field, 'model', None) == related_model):
|
2014-03-07 19:56:28 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"'%s.%s' is not a foreign key to '%s'." % (
|
|
|
|
through._meta.object_name, field_name,
|
|
|
|
related_model._meta.object_name),
|
|
|
|
hint=hint,
|
|
|
|
obj=self,
|
|
|
|
id='fields.E339',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
2013-05-18 19:48:57 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
name, path, args, kwargs = super(ManyToManyField, self).deconstruct()
|
2015-02-16 17:06:42 +08:00
|
|
|
# Handle the simpler arguments.
|
2015-02-26 22:19:17 +08:00
|
|
|
del kwargs["null"]
|
2014-07-27 00:48:36 +08:00
|
|
|
if self.db_table is not None:
|
|
|
|
kwargs['db_table'] = self.db_table
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.db_constraint is not True:
|
|
|
|
kwargs['db_constraint'] = self.remote_field.db_constraint
|
|
|
|
if self.remote_field.related_name is not None:
|
|
|
|
kwargs['related_name'] = self.remote_field.related_name
|
|
|
|
if self.remote_field.related_query_name is not None:
|
|
|
|
kwargs['related_query_name'] = self.remote_field.related_query_name
|
2013-05-18 19:48:57 +08:00
|
|
|
# Rel needs more work.
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(self.remote_field.model, six.string_types):
|
|
|
|
kwargs['to'] = self.remote_field.model
|
2013-05-18 19:48:57 +08:00
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
kwargs['to'] = "%s.%s" % (
|
|
|
|
self.remote_field.model._meta.app_label,
|
|
|
|
self.remote_field.model._meta.object_name,
|
|
|
|
)
|
|
|
|
if getattr(self.remote_field, 'through', None) is not None:
|
|
|
|
if isinstance(self.remote_field.through, six.string_types):
|
|
|
|
kwargs['through'] = self.remote_field.through
|
|
|
|
elif not self.remote_field.through._meta.auto_created:
|
|
|
|
kwargs['through'] = "%s.%s" % (
|
|
|
|
self.remote_field.through._meta.app_label,
|
|
|
|
self.remote_field.through._meta.object_name,
|
|
|
|
)
|
2014-01-15 22:20:47 +08:00
|
|
|
# If swappable is True, then see if we're actually pointing to the target
|
|
|
|
# of a swap.
|
|
|
|
swappable_setting = self.swappable_setting
|
|
|
|
if swappable_setting is not None:
|
2015-02-16 17:06:42 +08:00
|
|
|
# If it's already a settings reference, error.
|
2014-01-15 22:20:47 +08:00
|
|
|
if hasattr(kwargs['to'], "setting_name"):
|
|
|
|
if kwargs['to'].setting_name != swappable_setting:
|
2014-09-04 20:15:09 +08:00
|
|
|
raise ValueError(
|
|
|
|
"Cannot deconstruct a ManyToManyField pointing to a "
|
|
|
|
"model that is swapped in place of more than one model "
|
|
|
|
"(%s and %s)" % (kwargs['to'].setting_name, swappable_setting)
|
|
|
|
)
|
2015-02-16 17:06:42 +08:00
|
|
|
|
2014-01-15 22:20:47 +08:00
|
|
|
from django.db.migrations.writer import SettingsReference
|
|
|
|
kwargs['to'] = SettingsReference(
|
|
|
|
kwargs['to'],
|
|
|
|
swappable_setting,
|
|
|
|
)
|
2013-05-18 19:48:57 +08:00
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2012-12-17 23:09:07 +08:00
|
|
|
def _get_path_info(self, direct=False):
|
|
|
|
"""
|
2014-07-06 02:28:30 +08:00
|
|
|
Called by both direct and indirect m2m traversal.
|
2012-12-17 23:09:07 +08:00
|
|
|
"""
|
|
|
|
pathinfos = []
|
2015-02-26 22:19:17 +08:00
|
|
|
int_model = self.remote_field.through
|
2015-01-07 08:16:35 +08:00
|
|
|
linkfield1 = int_model._meta.get_field(self.m2m_field_name())
|
|
|
|
linkfield2 = int_model._meta.get_field(self.m2m_reverse_field_name())
|
2012-12-17 23:09:07 +08:00
|
|
|
if direct:
|
2013-03-25 00:40:40 +08:00
|
|
|
join1infos = linkfield1.get_reverse_path_info()
|
|
|
|
join2infos = linkfield2.get_path_info()
|
2012-12-17 23:09:07 +08:00
|
|
|
else:
|
2013-03-25 00:40:40 +08:00
|
|
|
join1infos = linkfield2.get_reverse_path_info()
|
|
|
|
join2infos = linkfield1.get_path_info()
|
2012-12-17 23:09:07 +08:00
|
|
|
pathinfos.extend(join1infos)
|
|
|
|
pathinfos.extend(join2infos)
|
2013-03-25 00:40:40 +08:00
|
|
|
return pathinfos
|
2012-12-17 23:09:07 +08:00
|
|
|
|
|
|
|
def get_path_info(self):
|
|
|
|
return self._get_path_info(direct=True)
|
|
|
|
|
|
|
|
def get_reverse_path_info(self):
|
|
|
|
return self._get_path_info(direct=False)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_choices_default(self):
|
|
|
|
return Field.get_choices(self, include_blank=False)
|
|
|
|
|
|
|
|
def _get_m2m_db_table(self, opts):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Function that can be curried to provide the m2m table name for this
|
|
|
|
relation.
|
|
|
|
"""
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.through is not None:
|
|
|
|
return self.remote_field.through._meta.db_table
|
2008-07-29 20:41:08 +08:00
|
|
|
elif self.db_table:
|
2007-01-25 21:47:55 +08:00
|
|
|
return self.db_table
|
|
|
|
else:
|
2013-09-17 00:52:05 +08:00
|
|
|
return utils.truncate_name('%s_%s' % (opts.db_table, self.name),
|
2008-12-03 00:58:06 +08:00
|
|
|
connection.ops.max_name_length())
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def _get_m2m_attr(self, related, attr):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Function that can be curried to provide the source accessor or DB
|
|
|
|
column name for the m2m table.
|
|
|
|
"""
|
2009-11-03 22:02:49 +08:00
|
|
|
cache_attr = '_m2m_%s_cache' % attr
|
|
|
|
if hasattr(self, cache_attr):
|
|
|
|
return getattr(self, cache_attr)
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.through_fields is not None:
|
|
|
|
link_field_name = self.remote_field.through_fields[0]
|
2014-02-20 02:01:55 +08:00
|
|
|
else:
|
|
|
|
link_field_name = None
|
2015-02-26 22:19:17 +08:00
|
|
|
for f in self.remote_field.through._meta.fields:
|
|
|
|
if (f.is_relation and f.remote_field.model == related.related_model and
|
2015-01-07 08:16:35 +08:00
|
|
|
(link_field_name is None or link_field_name == f.name)):
|
2009-11-03 22:02:49 +08:00
|
|
|
setattr(self, cache_attr, getattr(f, attr))
|
|
|
|
return getattr(self, cache_attr)
|
|
|
|
|
|
|
|
def _get_m2m_reverse_attr(self, related, attr):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Function that can be curried to provide the related accessor or DB
|
|
|
|
column name for the m2m table.
|
|
|
|
"""
|
2009-11-03 22:02:49 +08:00
|
|
|
cache_attr = '_m2m_reverse_%s_cache' % attr
|
|
|
|
if hasattr(self, cache_attr):
|
|
|
|
return getattr(self, cache_attr)
|
|
|
|
found = False
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.through_fields is not None:
|
|
|
|
link_field_name = self.remote_field.through_fields[1]
|
2014-02-20 02:01:55 +08:00
|
|
|
else:
|
|
|
|
link_field_name = None
|
2015-02-26 22:19:17 +08:00
|
|
|
for f in self.remote_field.through._meta.fields:
|
|
|
|
if f.is_relation and f.remote_field.model == related.model:
|
2015-01-07 08:16:35 +08:00
|
|
|
if link_field_name is None and related.related_model == related.model:
|
2009-11-03 22:02:49 +08:00
|
|
|
# If this is an m2m-intermediate to self,
|
|
|
|
# the first foreign key you find will be
|
|
|
|
# the source column. Keep searching for
|
|
|
|
# the second foreign key.
|
|
|
|
if found:
|
|
|
|
setattr(self, cache_attr, getattr(f, attr))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
found = True
|
2014-02-20 02:01:55 +08:00
|
|
|
elif link_field_name is None or link_field_name == f.name:
|
2009-11-03 22:02:49 +08:00
|
|
|
setattr(self, cache_attr, getattr(f, attr))
|
|
|
|
break
|
2014-03-07 19:56:28 +08:00
|
|
|
return getattr(self, cache_attr)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
def value_to_string(self, obj):
|
|
|
|
data = ''
|
2006-05-02 09:31:56 +08:00
|
|
|
if obj:
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
qs = getattr(obj, self.name).all()
|
|
|
|
data = [instance._get_pk_val() for instance in qs]
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
# In required many-to-many fields with only one available choice,
|
|
|
|
# select that one available choice.
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
if not self.blank:
|
2006-05-02 09:31:56 +08:00
|
|
|
choices_list = self.get_choices_default()
|
|
|
|
if len(choices_list) == 1:
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
data = [choices_list[0][0]]
|
2012-07-21 16:00:10 +08:00
|
|
|
return smart_text(data)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-07-23 17:41:06 +08:00
|
|
|
def contribute_to_class(self, cls, name, **kwargs):
|
2008-08-30 05:24:00 +08:00
|
|
|
# To support multiple relations to self, it's useful to have a non-None
|
|
|
|
# related name on symmetrical relations for internal reasons. The
|
|
|
|
# concept doesn't make a lot of sense externally ("you want me to
|
|
|
|
# specify *what* on my non-reversible relation?!"), so we set it up
|
|
|
|
# automatically. The funky name reduces the chance of an accidental
|
|
|
|
# clash.
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.remote_field.symmetrical and (
|
|
|
|
self.remote_field.model == "self" or self.remote_field.model == cls._meta.object_name):
|
|
|
|
self.remote_field.related_name = "%s_rel_+" % name
|
2015-03-27 02:47:07 +08:00
|
|
|
elif self.remote_field.is_hidden():
|
|
|
|
# If the backwards relation is disabled, replace the original
|
|
|
|
# related_name with one generated from the m2m field name. Django
|
|
|
|
# still uses backwards relations internally and we need to avoid
|
|
|
|
# clashes between multiple m2m fields with related_name == '+'.
|
2015-05-09 18:57:13 +08:00
|
|
|
self.remote_field.related_name = "_%s_%s_+" % (cls.__name__.lower(), name)
|
2008-08-30 05:24:00 +08:00
|
|
|
|
2014-07-23 17:41:06 +08:00
|
|
|
super(ManyToManyField, self).contribute_to_class(cls, name, **kwargs)
|
2009-11-03 22:02:49 +08:00
|
|
|
|
|
|
|
# The intermediate m2m model is not auto created if:
|
|
|
|
# 1) There is a manually specified intermediate, or
|
|
|
|
# 2) The class owning the m2m field is abstract.
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
# 3) The class owning the m2m field has been swapped out.
|
2015-03-03 16:43:56 +08:00
|
|
|
if not cls._meta.abstract:
|
|
|
|
if self.remote_field.through:
|
|
|
|
def resolve_through_model(_, model, field):
|
|
|
|
field.remote_field.through = model
|
|
|
|
lazy_related_operation(resolve_through_model, cls, self.remote_field.through, field=self)
|
|
|
|
elif not cls._meta.swapped:
|
|
|
|
self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
|
2009-11-03 22:02:49 +08:00
|
|
|
|
2015-02-16 17:03:34 +08:00
|
|
|
# Add the descriptor for the m2m relation.
|
2015-02-26 22:19:17 +08:00
|
|
|
setattr(cls, self.name, ManyRelatedObjectsDescriptor(self.remote_field, reverse=False))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:06:42 +08:00
|
|
|
# Set up the accessor for the m2m table name for the relation.
|
2006-05-02 09:31:56 +08:00
|
|
|
self.m2m_db_table = curry(self._get_m2m_db_table, cls._meta)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def contribute_to_related_class(self, cls, related):
|
2009-11-03 22:02:49 +08:00
|
|
|
# Internal M2Ms (i.e., those with a related name ending with '+')
|
2012-10-02 22:52:45 +08:00
|
|
|
# and swapped models don't get a related descriptor.
|
2015-02-26 22:19:17 +08:00
|
|
|
if not self.remote_field.is_hidden() and not related.related_model._meta.swapped:
|
|
|
|
setattr(cls, related.get_accessor_name(), ManyRelatedObjectsDescriptor(self.remote_field, reverse=True))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-16 17:06:42 +08:00
|
|
|
# Set up the accessors for the column names on the m2m table.
|
2009-11-03 22:02:49 +08:00
|
|
|
self.m2m_column_name = curry(self._get_m2m_attr, related, 'column')
|
|
|
|
self.m2m_reverse_name = curry(self._get_m2m_reverse_attr, related, 'column')
|
|
|
|
|
|
|
|
self.m2m_field_name = curry(self._get_m2m_attr, related, 'name')
|
|
|
|
self.m2m_reverse_field_name = curry(self._get_m2m_reverse_attr, related, 'name')
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-02-26 22:19:17 +08:00
|
|
|
get_m2m_rel = curry(self._get_m2m_attr, related, 'remote_field')
|
2011-01-27 03:10:08 +08:00
|
|
|
self.m2m_target_field_name = lambda: get_m2m_rel().field_name
|
2015-02-26 22:19:17 +08:00
|
|
|
get_m2m_reverse_rel = curry(self._get_m2m_reverse_attr, related, 'remote_field')
|
2011-01-27 03:10:08 +08:00
|
|
|
self.m2m_reverse_target_field_name = lambda: get_m2m_reverse_rel().field_name
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def set_attributes_from_rel(self):
|
|
|
|
pass
|
|
|
|
|
2006-12-28 10:27:14 +08:00
|
|
|
def value_from_object(self, obj):
|
2015-02-16 17:06:42 +08:00
|
|
|
"""
|
|
|
|
Return the value of this field in the given model instance.
|
|
|
|
"""
|
2006-12-28 10:27:14 +08:00
|
|
|
return getattr(obj, self.attname).all()
|
|
|
|
|
2007-08-06 21:58:56 +08:00
|
|
|
def save_form_data(self, instance, data):
|
|
|
|
setattr(instance, self.attname, data)
|
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
|
|
|
|
2007-01-22 14:32:14 +08:00
|
|
|
def formfield(self, **kwargs):
|
2009-12-22 23:18:51 +08:00
|
|
|
db = kwargs.pop('using', None)
|
|
|
|
defaults = {
|
|
|
|
'form_class': forms.ModelMultipleChoiceField,
|
2015-02-26 22:19:17 +08:00
|
|
|
'queryset': self.remote_field.model._default_manager.using(db),
|
2009-12-22 23:18:51 +08:00
|
|
|
}
|
2007-04-28 21:55:24 +08:00
|
|
|
defaults.update(kwargs)
|
2006-12-30 08:12:02 +08:00
|
|
|
# If initial is passed in, it's a list of related objects, but the
|
|
|
|
# MultipleChoiceField takes a list of IDs.
|
2007-04-28 21:55:24 +08:00
|
|
|
if defaults.get('initial') is not None:
|
2009-05-02 15:03:33 +08:00
|
|
|
initial = defaults['initial']
|
|
|
|
if callable(initial):
|
|
|
|
initial = initial()
|
|
|
|
defaults['initial'] = [i._get_pk_val() for i in initial]
|
2007-04-28 21:55:24 +08:00
|
|
|
return super(ManyToManyField, self).formfield(**defaults)
|
2006-12-27 13:15:22 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def db_type(self, connection):
|
2007-07-20 14:28:56 +08:00
|
|
|
# A ManyToManyField is not represented by a single column,
|
|
|
|
# so return None.
|
|
|
|
return None
|
2012-09-08 03:40:59 +08:00
|
|
|
|
|
|
|
def db_parameters(self, connection):
|
|
|
|
return {"type": None, "check": None}
|