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
|
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-09-19 20:13:56 +08:00
|
|
|
from django.db import connection, router
|
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.deletion import CASCADE, SET_DEFAULT, SET_NULL
|
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-09-01 23:02:08 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
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
|
|
|
|
2015-11-13 15:56:10 +08:00
|
|
|
from . import Field
|
2015-09-19 20:13:56 +08:00
|
|
|
from .related_descriptors import (
|
2015-09-20 23:51:25 +08:00
|
|
|
ForwardManyToOneDescriptor, ManyToManyDescriptor,
|
|
|
|
ReverseManyToOneDescriptor, ReverseOneToOneDescriptor,
|
2015-09-19 20:13:56 +08:00
|
|
|
)
|
|
|
|
from .related_lookups import (
|
|
|
|
RelatedExact, RelatedGreaterThan, RelatedGreaterThanOrEqual, RelatedIn,
|
2015-12-23 07:22:54 +08:00
|
|
|
RelatedIsNull, RelatedLessThan, RelatedLessThanOrEqual,
|
2015-09-19 20:13:56 +08:00
|
|
|
)
|
|
|
|
from .reverse_related import (
|
|
|
|
ForeignObjectRel, ManyToManyRel, ManyToOneRel, OneToOneRel,
|
|
|
|
)
|
|
|
|
|
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-10-17 00:21:30 +08:00
|
|
|
if related_name is None:
|
2015-06-23 14:08:12 +08:00
|
|
|
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-11-10 05:50:40 +08:00
|
|
|
rel_is_missing = self.remote_field.model not in self.opts.apps.get_models()
|
2015-02-26 22:19:17 +08:00
|
|
|
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-11-10 05:50:40 +08:00
|
|
|
if (self.remote_field.model not in self.opts.apps.get_models() and
|
2015-02-26 22:19:17 +08:00
|
|
|
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
|
|
|
|
2015-03-27 00:52:11 +08:00
|
|
|
def get_forward_related_filter(self, obj):
|
|
|
|
"""
|
|
|
|
Return the keyword arguments that when supplied to
|
|
|
|
self.model.object.filter(), would select all instances related through
|
|
|
|
this field to the remote obj. This is used to build the querysets
|
|
|
|
returned by related descriptors. obj is an instance of
|
|
|
|
self.related_field.model.
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'%s__%s' % (self.name, rh_field.name): getattr(obj, rh_field.attname)
|
|
|
|
for _, rh_field in self.related_fields
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_reverse_related_filter(self, obj):
|
|
|
|
"""
|
|
|
|
Complement to get_forward_related_filter(). Return the keyword
|
|
|
|
arguments that when passed to self.related_field.model.object.filter()
|
|
|
|
select all instances of self.related_field.model related through
|
|
|
|
this field to obj. obj is an instance of self.model.
|
|
|
|
"""
|
|
|
|
base_filter = {
|
|
|
|
rh_field.attname: getattr(obj, lh_field.attname)
|
|
|
|
for lh_field, rh_field in self.related_fields
|
|
|
|
}
|
|
|
|
base_filter.update(self.get_extra_descriptor_filter(obj) or {})
|
|
|
|
return base_filter
|
|
|
|
|
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:
|
2015-08-22 21:40:34 +08:00
|
|
|
to_string = self.remote_field.model._meta.label
|
|
|
|
return apps.get_swappable_settings_name(to_string)
|
2014-01-15 22:20:47 +08:00
|
|
|
return None
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def set_attributes_from_rel(self):
|
2015-09-12 07:33:12 +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
|
|
|
|
|
|
|
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
|
|
|
|
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
|
2015-09-20 23:51:25 +08:00
|
|
|
related_accessor_class = ReverseManyToOneDescriptor
|
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
|
2015-09-19 20:13:56 +08:00
|
|
|
except exceptions.FieldDoesNotExist:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
|
2015-10-06 04:29:23 +08:00
|
|
|
if not self.foreign_related_fields:
|
|
|
|
return []
|
|
|
|
|
2015-10-10 00:55:19 +08:00
|
|
|
unique_foreign_fields = {
|
|
|
|
frozenset([f.name])
|
|
|
|
for f in self.remote_field.model._meta.get_fields()
|
|
|
|
if getattr(f, 'unique', False)
|
|
|
|
}
|
|
|
|
unique_foreign_fields.update({
|
|
|
|
frozenset(ut)
|
|
|
|
for ut in self.remote_field.model._meta.unique_together
|
|
|
|
})
|
|
|
|
foreign_fields = {f.name for f in self.foreign_related_fields}
|
|
|
|
has_unique_constraint = any(u <= foreign_fields for u in unique_foreign_fields)
|
|
|
|
|
|
|
|
if not has_unique_constraint 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(
|
2015-10-13 21:08:27 +08:00
|
|
|
"No subset of the fields %s on model '%s' is unique."
|
2014-09-04 20:15:09 +08:00
|
|
|
% (field_combination, model_name),
|
2015-10-13 21:08:27 +08:00
|
|
|
hint=(
|
|
|
|
"Add unique=True on any of those fields or add at "
|
|
|
|
"least a subset of them to a unique_together constraint."
|
|
|
|
),
|
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
|
|
|
)
|
|
|
|
]
|
2015-10-10 00:55:19 +08:00
|
|
|
elif not has_unique_constraint:
|
2014-01-20 10:45:21 +08:00
|
|
|
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-09-12 07:33:12 +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):
|
2015-10-06 04:29:23 +08:00
|
|
|
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
|
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
|
2015-12-23 07:22:54 +08:00
|
|
|
elif lookup_name == 'isnull':
|
|
|
|
return RelatedIsNull
|
|
|
|
else:
|
2015-02-02 19:48:30 +08:00
|
|
|
raise TypeError('Related Field got invalid lookup: %s' % 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)
|
2015-09-20 23:51:25 +08:00
|
|
|
setattr(cls, self.name, ForwardManyToOneDescriptor(self))
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
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:
|
2015-10-02 02:57:58 +08:00
|
|
|
setattr(cls._meta.concrete_model, 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(
|
2015-12-24 09:52:24 +08:00
|
|
|
"on_delete will be a required arg for %s in Django 2.0. Set "
|
|
|
|
"it to models.CASCADE on models and in existing migrations "
|
|
|
|
"if you want to maintain the current default behavior. "
|
2015-07-22 22:43:21 +08:00
|
|
|
"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)
|
2015-09-12 07:33:12 +08:00
|
|
|
if self.remote_field.field_name and (
|
|
|
|
not to_meta or (to_meta.pk and self.remote_field.field_name != to_meta.pk.name)):
|
2015-02-26 22:19:17 +08:00
|
|
|
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):
|
2015-11-13 15:56:10 +08:00
|
|
|
return self.target_field.rel_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
|
|
|
|
|
2015-09-20 23:51:25 +08:00
|
|
|
related_accessor_class = ReverseOneToOneDescriptor
|
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(
|
2015-12-24 09:52:24 +08:00
|
|
|
"on_delete will be a required arg for %s in Django 2.0. Set "
|
|
|
|
"it to models.CASCADE on models and in existing migrations "
|
|
|
|
"if you want to maintain the current default behavior. "
|
2015-07-22 22:43:21 +08:00
|
|
|
"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),
|
2015-11-21 18:01:07 +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
|
|
|
|
|
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-11-10 05:50:40 +08:00
|
|
|
if self.remote_field.through not in self.opts.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)
|
2015-09-19 20:13:56 +08:00
|
|
|
except exceptions.FieldDoesNotExist:
|
2014-03-07 19:56:28 +08:00
|
|
|
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.
|
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-09-20 23:51:25 +08:00
|
|
|
setattr(cls, self.name, ManyToManyDescriptor(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:
|
2015-09-20 23:51:25 +08:00
|
|
|
setattr(cls, related.get_accessor_name(), ManyToManyDescriptor(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):
|
2015-10-09 05:17:10 +08:00
|
|
|
getattr(instance, self.attname).set(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}
|