2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
from operator import attrgetter
|
2013-11-09 20:25:15 +08:00
|
|
|
import warnings
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.apps import apps
|
|
|
|
from django.core import checks
|
2015-01-02 23:14:23 +08:00
|
|
|
from django.core.exceptions import FieldDoesNotExist
|
2013-09-27 02:16:50 +08:00
|
|
|
from django.db import connection, connections, router, transaction
|
2013-09-17 00:52:05 +08:00
|
|
|
from django.db.backends import utils
|
2013-09-27 07:35:53 +08:00
|
|
|
from django.db.models import signals, Q
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.db.models.deletion import SET_NULL, SET_DEFAULT, CASCADE
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db.models.fields import (AutoField, Field, IntegerField,
|
2015-01-02 23:14:23 +08:00
|
|
|
PositiveIntegerField, PositiveSmallIntegerField, BLANK_CHOICE_DASH)
|
2014-01-18 17:09:43 +08:00
|
|
|
from django.db.models.lookups import IsNull
|
2008-07-21 23:41:21 +08:00
|
|
|
from django.db.models.query import QuerySet
|
2013-11-09 20:25:15 +08:00
|
|
|
from django.db.models.query_utils import PathInfo
|
2014-12-12 08:28:03 +08:00
|
|
|
from django.utils.encoding import force_text, smart_text
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2013-11-09 20:25:15 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2013-04-15 21:54:37 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2011-10-06 19:08:11 +08:00
|
|
|
from django.utils.functional import curry, cached_property
|
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
|
|
|
from django.core import exceptions
|
2008-07-19 09:22:26 +08:00
|
|
|
from django import forms
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
RECURSIVE_RELATIONSHIP_CONSTANT = 'self'
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
def add_lazy_relation(cls, field, relation, operation):
|
2008-02-27 05:13:16 +08:00
|
|
|
"""
|
|
|
|
Adds a lookup on ``cls`` when a related field is defined using a string,
|
|
|
|
i.e.::
|
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
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
class MyModel(Model):
|
|
|
|
fk = ForeignKey("AnotherModel")
|
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
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
This string can be:
|
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
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
* RECURSIVE_RELATIONSHIP_CONSTANT (i.e. "self") to indicate a recursive
|
|
|
|
relation.
|
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
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
* The name of a model (i.e "AnotherModel") to indicate another model in
|
|
|
|
the same app.
|
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
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
* An app-label and model name (i.e. "someapp.AnotherModel") to indicate
|
|
|
|
another model in a different app.
|
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
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
If the other model hasn't yet been loaded -- almost a given if you're using
|
|
|
|
lazy relationships -- then the relation won't be set up until the
|
|
|
|
class_prepared signal fires at the end of model initialization.
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
operation is the work that must be performed once the relation can be resolved.
|
2008-02-27 05:13:16 +08:00
|
|
|
"""
|
|
|
|
# Check for recursive relations
|
|
|
|
if relation == RECURSIVE_RELATIONSHIP_CONSTANT:
|
|
|
|
app_label = cls._meta.app_label
|
|
|
|
model_name = cls.__name__
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
else:
|
|
|
|
# Look for an "app.Model" relation
|
2012-11-05 05:43:54 +08:00
|
|
|
|
2012-11-05 06:16:32 +08:00
|
|
|
if isinstance(relation, six.string_types):
|
2012-11-05 05:43:54 +08:00
|
|
|
try:
|
|
|
|
app_label, model_name = relation.split(".")
|
|
|
|
except ValueError:
|
|
|
|
# If we can't split, assume a model in current app
|
|
|
|
app_label = cls._meta.app_label
|
|
|
|
model_name = relation
|
|
|
|
else:
|
|
|
|
# it's actually a model class
|
2009-11-03 22:02:49 +08:00
|
|
|
app_label = relation._meta.app_label
|
|
|
|
model_name = relation._meta.object_name
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2008-02-27 05:13:16 +08:00
|
|
|
# Try to look up the related model, and if it's already loaded resolve the
|
2014-11-26 22:45:10 +08:00
|
|
|
# string right away. If get_registered_model raises a LookupError, it means
|
|
|
|
# that the related model isn't loaded yet, so we need to pend the relation
|
|
|
|
# until the class is prepared.
|
2013-12-28 21:55:54 +08:00
|
|
|
try:
|
|
|
|
model = cls._meta.apps.get_registered_model(app_label, model_name)
|
|
|
|
except LookupError:
|
2008-02-27 05:13:16 +08:00
|
|
|
key = (app_label, model_name)
|
2008-07-29 20:41:08 +08:00
|
|
|
value = (cls, field, operation)
|
2013-12-24 19:25:17 +08:00
|
|
|
cls._meta.apps._pending_lookups.setdefault(key, []).append(value)
|
2013-12-28 21:55:54 +08:00
|
|
|
else:
|
|
|
|
operation(field, model, cls)
|
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
|
|
|
|
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
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def do_pending_lookups(sender, **kwargs):
|
2008-02-27 05:13:16 +08:00
|
|
|
"""
|
|
|
|
Handle any pending relations to the sending model. Sent from class_prepared.
|
|
|
|
"""
|
|
|
|
key = (sender._meta.app_label, sender.__name__)
|
2013-12-24 19:25:17 +08:00
|
|
|
for cls, field, operation in sender._meta.apps._pending_lookups.pop(key, []):
|
2008-07-29 20:41:08 +08:00
|
|
|
operation(field, sender, cls)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.class_prepared.connect(do_pending_lookups)
|
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-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()
|
|
|
|
return self.rel.to
|
|
|
|
|
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
|
|
|
|
related_name = self.rel.related_name
|
|
|
|
|
|
|
|
is_valid_id = (related_name and re.match('^[a-zA-Z_][a-zA-Z0-9_]*$', related_name)
|
|
|
|
and not keyword.iskeyword(related_name))
|
|
|
|
if related_name and not (is_valid_id or related_name.endswith('+')):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
|
|
|
"The name '%s' is invalid related_name for field %s.%s" %
|
|
|
|
(self.rel.related_name, self.model._meta.object_name,
|
|
|
|
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):
|
|
|
|
rel_is_missing = self.rel.to not in apps.get_models()
|
|
|
|
rel_is_string = isinstance(self.rel.to, six.string_types)
|
|
|
|
model_name = self.rel.to if rel_is_string else self.rel.to._meta.object_name
|
|
|
|
if rel_is_missing and (rel_is_string or not self.rel.to._meta.swapped):
|
|
|
|
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):
|
|
|
|
if (self.rel.to not in apps.get_models() and
|
|
|
|
not isinstance(self.rel.to, six.string_types) and
|
|
|
|
self.rel.to._meta.swapped):
|
|
|
|
model = "%s.%s" % (
|
|
|
|
self.rel.to._meta.app_label,
|
|
|
|
self.rel.to._meta.object_name
|
|
|
|
)
|
|
|
|
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,
|
|
|
|
hint="Update the relation to point at 'settings.%s'." % self.rel.to._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):
|
|
|
|
""" Check accessor and reverse query name clashes. """
|
|
|
|
|
|
|
|
from django.db.models.base import ModelBase
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
opts = self.model._meta
|
|
|
|
|
|
|
|
# `f.rel.to` may be a string instead of a model. Skip if model name is
|
|
|
|
# not resolved.
|
|
|
|
if not isinstance(self.rel.to, ModelBase):
|
|
|
|
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.
|
|
|
|
if self.rel.is_hidden():
|
|
|
|
return []
|
|
|
|
|
|
|
|
try:
|
2013-11-09 20:25:15 +08:00
|
|
|
self.rel
|
2014-01-20 10:45:21 +08:00
|
|
|
except AttributeError:
|
|
|
|
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)
|
|
|
|
|
|
|
|
rel_opts = self.rel.to._meta
|
|
|
|
# rel_opts.object_name == "Target"
|
2013-11-09 20:25:15 +08:00
|
|
|
rel_name = self.rel.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):
|
|
|
|
'''By default related field will not have a column
|
|
|
|
as it relates columns to another table'''
|
|
|
|
return None
|
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name, virtual_only=False):
|
2006-05-02 09:31:56 +08:00
|
|
|
sup = super(RelatedField, self)
|
|
|
|
|
2010-03-27 23:54:31 +08:00
|
|
|
# Store the opts for related_query_name()
|
|
|
|
self.opts = cls._meta
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
if hasattr(sup, 'contribute_to_class'):
|
2013-03-25 00:40:40 +08:00
|
|
|
sup.contribute_to_class(cls, name, virtual_only=virtual_only)
|
2008-06-26 15:04:18 +08:00
|
|
|
|
|
|
|
if not cls._meta.abstract and self.rel.related_name:
|
2014-12-12 08:28:03 +08:00
|
|
|
related_name = force_text(self.rel.related_name) % {
|
2013-03-25 00:40:40 +08:00
|
|
|
'class': cls.__name__.lower(),
|
|
|
|
'app_label': cls._meta.app_label.lower()
|
|
|
|
}
|
|
|
|
self.rel.related_name = related_name
|
2006-05-02 09:31:56 +08:00
|
|
|
other = self.rel.to
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(other, six.string_types) or other._meta.pk is None:
|
2008-07-29 20:41:08 +08:00
|
|
|
def resolve_related_class(field, model, cls):
|
|
|
|
field.rel.to = model
|
|
|
|
field.do_related_class(model, cls)
|
|
|
|
add_lazy_relation(cls, self, other, resolve_related_class)
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
self.do_related_class(other, cls)
|
|
|
|
|
2014-01-15 22:20:47 +08:00
|
|
|
@property
|
|
|
|
def swappable_setting(self):
|
|
|
|
"""
|
|
|
|
Gets the setting that this is powered from for swapping, or None
|
|
|
|
if it's not swapped in / marked with swappable=False.
|
|
|
|
"""
|
|
|
|
if self.swappable:
|
|
|
|
# Work out string form of "to"
|
|
|
|
if isinstance(self.rel.to, six.string_types):
|
|
|
|
to_string = self.rel.to
|
|
|
|
else:
|
|
|
|
to_string = "%s.%s" % (
|
|
|
|
self.rel.to._meta.app_label,
|
|
|
|
self.rel.to._meta.object_name,
|
|
|
|
)
|
|
|
|
# See if anything swapped/swappable matches
|
|
|
|
for model in apps.get_models(include_swapped=True):
|
|
|
|
if model._meta.swapped:
|
|
|
|
if model._meta.swapped == to_string:
|
|
|
|
return model._meta.swappable
|
|
|
|
if ("%s.%s" % (model._meta.app_label, model._meta.object_name)) == to_string and model._meta.swappable:
|
|
|
|
return model._meta.swappable
|
|
|
|
return None
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def set_attributes_from_rel(self):
|
2013-02-05 17:16:07 +08:00
|
|
|
self.name = self.name or (self.rel.to._meta.model_name + '_' + self.rel.to._meta.pk.name)
|
2008-07-29 13:17:47 +08:00
|
|
|
if self.verbose_name is None:
|
|
|
|
self.verbose_name = self.rel.to._meta.verbose_name
|
2013-03-25 02:57:02 +08:00
|
|
|
self.rel.set_field_name()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
@property
|
|
|
|
def related(self):
|
|
|
|
warnings.warn(
|
|
|
|
"Usage of field.related has been deprecated. Use field.rel instead.",
|
|
|
|
RemovedInDjango20Warning, 2)
|
|
|
|
return self.rel
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def do_related_class(self, other, cls):
|
|
|
|
self.set_attributes_from_rel()
|
2008-06-26 15:04:18 +08:00
|
|
|
if not cls._meta.abstract:
|
2013-11-09 20:25:15 +08:00
|
|
|
self.contribute_to_related_class(other, self.rel)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-02-02 03:23:31 +08:00
|
|
|
def get_limit_choices_to(self):
|
|
|
|
"""Returns 'limit_choices_to' for this model field.
|
|
|
|
|
|
|
|
If it is a callable, it will be invoked and the result will be
|
|
|
|
returned.
|
|
|
|
"""
|
|
|
|
if callable(self.rel.limit_choices_to):
|
|
|
|
return self.rel.limit_choices_to()
|
|
|
|
return self.rel.limit_choices_to
|
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
|
|
|
"""Passes ``limit_choices_to`` to field being constructed.
|
|
|
|
|
|
|
|
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 = {}
|
|
|
|
if hasattr(self.rel, 'get_related_field'):
|
|
|
|
# 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.
|
|
|
|
limit_choices_to = self.rel.limit_choices_to
|
|
|
|
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):
|
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
|
|
|
# This method defines the name that can be used to identify this
|
|
|
|
# related object in a table-spanning query. It uses the lower-cased
|
|
|
|
# object_name by default, but this can be overridden with the
|
|
|
|
# "related_name" option.
|
2013-06-27 21:44:21 +08:00
|
|
|
return self.rel.related_query_name or self.rel.related_name or self.opts.model_name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2014-03-21 19:45:38 +08:00
|
|
|
class SingleRelatedObjectDescriptor(object):
|
2006-05-02 09:31:56 +08:00
|
|
|
# This class provides the functionality that makes the related-object
|
|
|
|
# managers available as attributes on a model class, for fields that have
|
|
|
|
# a single "remote" value, on the class pointed to by a related field.
|
|
|
|
# In the example "place.restaurant", the restaurant attribute is a
|
|
|
|
# SingleRelatedObjectDescriptor instance.
|
|
|
|
def __init__(self, related):
|
|
|
|
self.related = related
|
2010-01-27 21:30:29 +08:00
|
|
|
self.cache_name = related.get_cache_name()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-12-09 03:12:01 +08:00
|
|
|
@cached_property
|
|
|
|
def RelatedObjectDoesNotExist(self):
|
|
|
|
# The exception isn't created at initialization time for the sake of
|
|
|
|
# consistency with `ReverseSingleRelatedObjectDescriptor`.
|
|
|
|
return type(
|
|
|
|
str('RelatedObjectDoesNotExist'),
|
2015-01-07 08:16:35 +08:00
|
|
|
(self.related.related_model.DoesNotExist, AttributeError),
|
2013-12-09 03:12:01 +08:00
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
def is_cached(self, instance):
|
|
|
|
return hasattr(instance, self.cache_name)
|
|
|
|
|
2013-09-30 13:05:43 +08:00
|
|
|
def get_queryset(self, **hints):
|
2015-01-07 08:16:35 +08:00
|
|
|
manager = self.related.related_model._default_manager
|
2014-09-24 01:41:32 +08:00
|
|
|
# If the related manager indicates that it should be used for
|
|
|
|
# related fields, respect that.
|
|
|
|
if not getattr(manager, 'use_for_related_fields', False):
|
2015-01-07 08:16:35 +08:00
|
|
|
manager = self.related.related_model._base_manager
|
2014-09-24 01:41:32 +08:00
|
|
|
return manager.db_manager(hints=hints).all()
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
2014-01-17 02:24:39 +08:00
|
|
|
if queryset is None:
|
2014-09-24 01:41:32 +08:00
|
|
|
queryset = self.get_queryset()
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset._add_hints(instance=instances[0])
|
2013-11-07 01:25:05 +08:00
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
rel_obj_attr = attrgetter(self.related.field.attname)
|
|
|
|
instance_attr = lambda obj: obj._get_pk_val()
|
2014-12-07 05:00:09 +08:00
|
|
|
instances_dict = {instance_attr(inst): inst for inst in instances}
|
2013-03-25 00:40:40 +08:00
|
|
|
query = {'%s__in' % self.related.field.name: instances}
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset = queryset.filter(**query)
|
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
# Since we're going to assign directly in the cache,
|
|
|
|
# we must manage the reverse relation cache manually.
|
|
|
|
rel_obj_cache_name = self.related.field.get_cache_name()
|
2014-01-17 02:24:39 +08:00
|
|
|
for rel_obj in queryset:
|
2012-05-24 19:25:01 +08:00
|
|
|
instance = instances_dict[rel_obj_attr(rel_obj)]
|
|
|
|
setattr(rel_obj, rel_obj_cache_name, instance)
|
2014-01-17 02:24:39 +08:00
|
|
|
return queryset, rel_obj_attr, instance_attr, True, self.cache_name
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
try:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = getattr(instance, self.cache_name)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
except AttributeError:
|
2012-10-10 03:36:35 +08:00
|
|
|
related_pk = instance._get_pk_val()
|
|
|
|
if related_pk is None:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = None
|
|
|
|
else:
|
2013-03-25 00:40:40 +08:00
|
|
|
params = {}
|
|
|
|
for lh_field, rh_field in self.related.field.related_fields:
|
|
|
|
params['%s__%s' % (self.related.field.name, rh_field.name)] = getattr(instance, rh_field.attname)
|
2012-10-10 03:36:35 +08:00
|
|
|
try:
|
2013-03-08 22:15:23 +08:00
|
|
|
rel_obj = self.get_queryset(instance=instance).get(**params)
|
2015-01-07 08:16:35 +08:00
|
|
|
except self.related.related_model.DoesNotExist:
|
2012-10-10 03:36:35 +08:00
|
|
|
rel_obj = None
|
|
|
|
else:
|
|
|
|
setattr(rel_obj, self.related.field.get_cache_name(), instance)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
setattr(instance, self.cache_name, rel_obj)
|
2012-04-10 20:29:25 +08:00
|
|
|
if rel_obj is None:
|
2013-12-09 03:12:01 +08:00
|
|
|
raise self.RelatedObjectDoesNotExist(
|
|
|
|
"%s has no %s." % (
|
|
|
|
instance.__class__.__name__,
|
|
|
|
self.related.get_accessor_name()
|
|
|
|
)
|
|
|
|
)
|
2012-04-10 20:29:25 +08:00
|
|
|
else:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
return rel_obj
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2008-06-29 18:35:35 +08:00
|
|
|
# The similarity of the code below to the code in
|
2008-06-05 08:39:32 +08:00
|
|
|
# ReverseSingleRelatedObjectDescriptor is annoying, but there's a bunch
|
|
|
|
# of small differences that would make a common base class convoluted.
|
2008-06-29 18:35:35 +08:00
|
|
|
|
2008-06-05 08:39:32 +08:00
|
|
|
# If null=True, we can assign null here, but otherwise the value needs
|
|
|
|
# to be an instance of the related class.
|
2013-07-08 08:39:54 +08:00
|
|
|
if value is None and self.related.field.null is False:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign None: "%s.%s" does not allow null values.' % (
|
|
|
|
instance._meta.object_name,
|
|
|
|
self.related.get_accessor_name(),
|
|
|
|
)
|
|
|
|
)
|
2015-01-07 08:16:35 +08:00
|
|
|
elif value is not None and not isinstance(value, self.related.related_model):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign "%r": "%s.%s" must be a "%s" instance.' % (
|
|
|
|
value,
|
|
|
|
instance._meta.object_name,
|
|
|
|
self.related.get_accessor_name(),
|
|
|
|
self.related.opts.object_name,
|
|
|
|
)
|
|
|
|
)
|
2010-01-22 22:30:06 +08:00
|
|
|
elif value is not None:
|
|
|
|
if instance._state.db is None:
|
|
|
|
instance._state.db = router.db_for_write(instance.__class__, instance=value)
|
|
|
|
elif value._state.db is None:
|
|
|
|
value._state.db = router.db_for_write(value.__class__, instance=instance)
|
|
|
|
elif value._state.db is not None and instance._state.db is not None:
|
|
|
|
if not router.allow_relation(value, instance):
|
2013-05-01 04:28:09 +08:00
|
|
|
raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value)
|
2008-06-29 18:35:35 +08:00
|
|
|
|
2013-08-30 00:09:35 +08:00
|
|
|
related_pk = tuple(getattr(instance, field.attname) for field in self.related.field.foreign_related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
if None in related_pk:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign "%r": "%s" instance isn\'t saved in the database.' %
|
|
|
|
(value, instance._meta.object_name)
|
|
|
|
)
|
2012-10-10 03:36:35 +08:00
|
|
|
|
2009-03-31 06:38:19 +08:00
|
|
|
# Set the value of the related field to the value of the related object's related field
|
2013-03-25 00:40:40 +08:00
|
|
|
for index, field in enumerate(self.related.field.local_related_fields):
|
|
|
|
setattr(value, field.attname, related_pk[index])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-06-05 08:39:32 +08:00
|
|
|
# Since we already know what the related object is, seed the related
|
2008-06-29 18:35:35 +08:00
|
|
|
# object caches now, too. This avoids another db hit if you get the
|
2008-06-05 08:39:32 +08:00
|
|
|
# object you just set.
|
|
|
|
setattr(instance, self.cache_name, value)
|
|
|
|
setattr(value, self.related.field.get_cache_name(), instance)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2014-03-21 19:45:38 +08:00
|
|
|
class ReverseSingleRelatedObjectDescriptor(object):
|
2006-05-02 09:31:56 +08:00
|
|
|
# This class provides the functionality that makes the related-object
|
|
|
|
# managers available as attributes on a model class, for fields that have
|
|
|
|
# a single "remote" value, on the class that defines the related field.
|
|
|
|
# In the example "choice.poll", the poll attribute is a
|
|
|
|
# ReverseSingleRelatedObjectDescriptor instance.
|
|
|
|
def __init__(self, field_with_rel):
|
|
|
|
self.field = field_with_rel
|
2011-10-08 00:05:53 +08:00
|
|
|
self.cache_name = self.field.get_cache_name()
|
|
|
|
|
2013-12-09 03:12:01 +08:00
|
|
|
@cached_property
|
|
|
|
def RelatedObjectDoesNotExist(self):
|
|
|
|
# The exception can't be created at initialization time since the
|
|
|
|
# related model might not be resolved yet; `rel.to` might still be
|
|
|
|
# a string model reference.
|
|
|
|
return type(
|
|
|
|
str('RelatedObjectDoesNotExist'),
|
|
|
|
(self.field.rel.to.DoesNotExist, AttributeError),
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
def is_cached(self, instance):
|
|
|
|
return hasattr(instance, self.cache_name)
|
|
|
|
|
2013-09-30 13:05:43 +08:00
|
|
|
def get_queryset(self, **hints):
|
2014-09-24 01:41:32 +08:00
|
|
|
manager = self.field.rel.to._default_manager
|
2011-10-08 00:05:53 +08:00
|
|
|
# If the related manager indicates that it should be used for
|
|
|
|
# related fields, respect that.
|
2014-09-24 01:41:32 +08:00
|
|
|
if not getattr(manager, 'use_for_related_fields', False):
|
|
|
|
manager = self.field.rel.to._base_manager
|
|
|
|
return manager.db_manager(hints=hints).all()
|
2011-10-08 00:05:53 +08:00
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
2014-01-17 02:24:39 +08:00
|
|
|
if queryset is None:
|
2014-09-24 01:41:32 +08:00
|
|
|
queryset = self.get_queryset()
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset._add_hints(instance=instances[0])
|
2013-11-07 01:25:05 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
rel_obj_attr = self.field.get_foreign_related_value
|
|
|
|
instance_attr = self.field.get_local_related_value
|
2014-12-07 05:00:09 +08:00
|
|
|
instances_dict = {instance_attr(inst): inst for inst in instances}
|
2013-11-13 12:42:12 +08:00
|
|
|
related_field = self.field.foreign_related_fields[0]
|
|
|
|
|
|
|
|
# FIXME: This will need to be revisited when we introduce support for
|
|
|
|
# composite fields. In the meantime we take this practical approach to
|
|
|
|
# solve a regression on 1.6 when the reverse manager in hidden
|
|
|
|
# (related_name ends with a '+'). Refs #21410.
|
2014-04-12 09:27:34 +08:00
|
|
|
# The check for len(...) == 1 is a special case that allows the query
|
|
|
|
# to be join-less and smaller. Refs #21760.
|
|
|
|
if self.field.rel.is_hidden() or len(self.field.foreign_related_fields) == 1:
|
2013-11-13 12:42:12 +08:00
|
|
|
query = {'%s__in' % related_field.name: set(instance_attr(inst)[0] for inst in instances)}
|
|
|
|
else:
|
|
|
|
query = {'%s__in' % self.field.related_query_name(): instances}
|
2014-01-17 02:24:39 +08:00
|
|
|
queryset = queryset.filter(**query)
|
2013-11-13 12:42:12 +08:00
|
|
|
|
2012-05-24 19:25:01 +08:00
|
|
|
# Since we're going to assign directly in the cache,
|
|
|
|
# we must manage the reverse relation cache manually.
|
|
|
|
if not self.field.rel.multiple:
|
2013-11-09 20:25:15 +08:00
|
|
|
rel_obj_cache_name = self.field.rel.get_cache_name()
|
2014-01-17 02:24:39 +08:00
|
|
|
for rel_obj in queryset:
|
2012-05-24 19:25:01 +08:00
|
|
|
instance = instances_dict[rel_obj_attr(rel_obj)]
|
|
|
|
setattr(rel_obj, rel_obj_cache_name, instance)
|
2014-01-17 02:24:39 +08:00
|
|
|
return queryset, rel_obj_attr, instance_attr, True, self.cache_name
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = getattr(instance, self.cache_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
except AttributeError:
|
2013-03-25 00:40:40 +08:00
|
|
|
val = self.field.get_local_related_value(instance)
|
|
|
|
if None in val:
|
2012-04-10 20:29:25 +08:00
|
|
|
rel_obj = None
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2014-12-07 05:00:09 +08:00
|
|
|
params = {
|
|
|
|
rh_field.attname: getattr(instance, lh_field.attname)
|
|
|
|
for lh_field, rh_field in self.field.related_fields}
|
2013-04-02 05:30:43 +08:00
|
|
|
qs = self.get_queryset(instance=instance)
|
2013-07-23 20:06:02 +08:00
|
|
|
extra_filter = self.field.get_extra_descriptor_filter(instance)
|
|
|
|
if isinstance(extra_filter, dict):
|
|
|
|
params.update(extra_filter)
|
|
|
|
qs = qs.filter(**params)
|
|
|
|
else:
|
|
|
|
qs = qs.filter(extra_filter, **params)
|
2012-04-10 20:29:25 +08:00
|
|
|
# Assuming the database enforces foreign keys, this won't fail.
|
2013-07-23 20:06:02 +08:00
|
|
|
rel_obj = qs.get()
|
2012-04-10 20:29:25 +08:00
|
|
|
if not self.field.rel.multiple:
|
2013-11-09 20:25:15 +08:00
|
|
|
setattr(rel_obj, self.field.rel.get_cache_name(), instance)
|
2011-10-08 00:05:53 +08:00
|
|
|
setattr(instance, self.cache_name, rel_obj)
|
2012-04-10 20:29:25 +08:00
|
|
|
if rel_obj is None and not self.field.null:
|
2013-12-09 03:12:01 +08:00
|
|
|
raise self.RelatedObjectDoesNotExist(
|
|
|
|
"%s has no %s." % (self.field.model.__name__, self.field.name)
|
|
|
|
)
|
2012-04-10 20:29:25 +08:00
|
|
|
else:
|
2006-05-02 09:31:56 +08:00
|
|
|
return rel_obj
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2008-06-05 08:39:32 +08:00
|
|
|
# If null=True, we can assign null here, but otherwise the value needs
|
|
|
|
# to be an instance of the related class.
|
2013-07-08 08:39:54 +08:00
|
|
|
if value is None and self.field.null is False:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign None: "%s.%s" does not allow null values.' %
|
|
|
|
(instance._meta.object_name, self.field.name)
|
|
|
|
)
|
2008-06-05 08:39:32 +08:00
|
|
|
elif value is not None and not isinstance(value, self.field.rel.to):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign "%r": "%s.%s" must be a "%s" instance.' % (
|
|
|
|
value,
|
|
|
|
instance._meta.object_name,
|
|
|
|
self.field.name,
|
|
|
|
self.field.rel.to._meta.object_name,
|
|
|
|
)
|
|
|
|
)
|
2010-01-22 22:30:06 +08:00
|
|
|
elif value is not None:
|
2009-12-22 23:18:51 +08:00
|
|
|
if instance._state.db is None:
|
2010-01-22 22:30:06 +08:00
|
|
|
instance._state.db = router.db_for_write(instance.__class__, instance=value)
|
|
|
|
elif value._state.db is None:
|
|
|
|
value._state.db = router.db_for_write(value.__class__, instance=instance)
|
|
|
|
elif value._state.db is not None and instance._state.db is not None:
|
|
|
|
if not router.allow_relation(value, instance):
|
2013-05-01 04:28:09 +08:00
|
|
|
raise ValueError('Cannot assign "%r": the current database router prevents this relation.' % value)
|
2008-06-29 18:35:35 +08:00
|
|
|
|
2009-06-15 22:30:51 +08:00
|
|
|
# If we're setting the value of a OneToOneField to None, we need to clear
|
|
|
|
# out the cache on any old related object. Otherwise, deleting the
|
|
|
|
# previously-related object will also cause this object to be deleted,
|
|
|
|
# which is wrong.
|
|
|
|
if value is None:
|
|
|
|
# Look up the previously-related object, which may still be available
|
|
|
|
# since we've not yet cleared out the related field.
|
|
|
|
# Use the cache directly, instead of the accessor; if we haven't
|
|
|
|
# populated the cache, then we don't care - we're only accessing
|
|
|
|
# the object to invalidate the accessor cache, so there's no
|
|
|
|
# need to populate the cache just to expire it again.
|
2012-04-10 20:29:25 +08:00
|
|
|
related = getattr(instance, self.cache_name, None)
|
2009-06-15 22:30:51 +08:00
|
|
|
|
|
|
|
# If we've got an old related object, we need to clear out its
|
|
|
|
# cache. This cache also might not exist if the related object
|
|
|
|
# hasn't been accessed yet.
|
2012-04-10 20:29:25 +08:00
|
|
|
if related is not None:
|
2013-11-09 20:25:15 +08:00
|
|
|
setattr(related, self.field.rel.get_cache_name(), None)
|
2009-06-15 22:30:51 +08:00
|
|
|
|
2014-05-19 16:45:55 +08:00
|
|
|
for lh_field, rh_field in self.field.related_fields:
|
2013-03-25 00:40:40 +08:00
|
|
|
setattr(instance, lh_field.attname, None)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-05-19 16:45:55 +08:00
|
|
|
# Set the values of the related field.
|
|
|
|
else:
|
|
|
|
for lh_field, rh_field in self.field.related_fields:
|
2014-06-30 21:13:35 +08:00
|
|
|
pk = value._get_pk_val()
|
|
|
|
if pk is None:
|
2014-05-19 16:45:55 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot assign "%r": "%s" instance isn\'t saved in the database.' %
|
|
|
|
(value, self.field.rel.to._meta.object_name)
|
|
|
|
)
|
2014-06-30 21:13:35 +08:00
|
|
|
setattr(instance, lh_field.attname, getattr(value, rh_field.attname))
|
2014-05-19 16:45:55 +08:00
|
|
|
|
2008-06-05 08:39:32 +08:00
|
|
|
# Since we already know what the related object is, seed the related
|
2012-04-10 20:29:25 +08:00
|
|
|
# object caches now, too. This avoids another db hit if you get the
|
2008-06-05 08:39:32 +08:00
|
|
|
# object you just set.
|
2012-04-10 20:29:25 +08:00
|
|
|
setattr(instance, self.cache_name, value)
|
|
|
|
if value is not None and not self.field.rel.multiple:
|
2013-11-09 20:25:15 +08:00
|
|
|
setattr(value, self.field.rel.get_cache_name(), instance)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
def create_foreign_related_manager(superclass, rel_field, rel_model):
|
|
|
|
class RelatedManager(superclass):
|
|
|
|
def __init__(self, instance):
|
|
|
|
super(RelatedManager, self).__init__()
|
|
|
|
self.instance = instance
|
2015-01-22 14:43:49 +08:00
|
|
|
self.core_filters = {rel_field.name: instance}
|
2013-09-19 01:31:07 +08:00
|
|
|
self.model = rel_model
|
|
|
|
|
|
|
|
def __call__(self, **kwargs):
|
|
|
|
# We use **kwargs rather than a kwarg argument to enforce the
|
|
|
|
# `manager='manager_name'` syntax.
|
|
|
|
manager = getattr(self.model, kwargs.pop('manager'))
|
|
|
|
manager_class = create_foreign_related_manager(manager.__class__, rel_field, rel_model)
|
|
|
|
return manager_class(self.instance)
|
2013-09-30 11:21:21 +08:00
|
|
|
do_not_call_in_templates = True
|
2013-09-19 01:31:07 +08:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
try:
|
|
|
|
return self.instance._prefetched_objects_cache[rel_field.related_query_name()]
|
|
|
|
except (AttributeError, KeyError):
|
|
|
|
db = self._db or router.db_for_read(self.model, instance=self.instance)
|
|
|
|
empty_strings_as_null = connections[db].features.interprets_empty_strings_as_nulls
|
2013-09-30 13:05:43 +08:00
|
|
|
qs = super(RelatedManager, self).get_queryset()
|
|
|
|
qs._add_hints(instance=self.instance)
|
|
|
|
if self._db:
|
|
|
|
qs = qs.using(self._db)
|
|
|
|
qs = qs.filter(**self.core_filters)
|
2013-09-19 01:31:07 +08:00
|
|
|
for field in rel_field.foreign_related_fields:
|
|
|
|
val = getattr(self.instance, field.attname)
|
|
|
|
if val is None or (val == '' and empty_strings_as_null):
|
|
|
|
return qs.none()
|
|
|
|
qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}}
|
|
|
|
return qs
|
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = super(RelatedManager, self).get_queryset()
|
|
|
|
|
|
|
|
queryset._add_hints(instance=instances[0])
|
|
|
|
queryset = queryset.using(queryset._db or self._db)
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
rel_obj_attr = rel_field.get_local_related_value
|
|
|
|
instance_attr = rel_field.get_foreign_related_value
|
2014-12-07 05:00:09 +08:00
|
|
|
instances_dict = {instance_attr(inst): inst for inst in instances}
|
2013-09-19 01:31:07 +08:00
|
|
|
query = {'%s__in' % rel_field.name: instances}
|
2013-11-07 01:25:05 +08:00
|
|
|
queryset = queryset.filter(**query)
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
# Since we just bypassed this class' get_queryset(), we must manage
|
|
|
|
# the reverse relation manually.
|
2013-11-07 01:25:05 +08:00
|
|
|
for rel_obj in queryset:
|
2013-09-19 01:31:07 +08:00
|
|
|
instance = instances_dict[rel_obj_attr(rel_obj)]
|
|
|
|
setattr(rel_obj, rel_field.name, instance)
|
|
|
|
cache_name = rel_field.related_query_name()
|
2013-11-07 01:25:05 +08:00
|
|
|
return queryset, rel_obj_attr, instance_attr, False, cache_name
|
2013-09-19 01:31:07 +08:00
|
|
|
|
|
|
|
def add(self, *objs):
|
2013-09-27 02:16:50 +08:00
|
|
|
objs = list(objs)
|
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
2014-03-21 21:21:43 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
2013-09-27 02:16:50 +08:00
|
|
|
for obj in objs:
|
|
|
|
if not isinstance(obj, self.model):
|
|
|
|
raise TypeError("'%s' instance expected, got %r" %
|
|
|
|
(self.model._meta.object_name, obj))
|
|
|
|
setattr(obj, rel_field.name, self.instance)
|
|
|
|
obj.save()
|
2013-09-19 01:31:07 +08:00
|
|
|
add.alters_data = True
|
|
|
|
|
|
|
|
def create(self, **kwargs):
|
|
|
|
kwargs[rel_field.name] = self.instance
|
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
return super(RelatedManager, self.db_manager(db)).create(**kwargs)
|
|
|
|
create.alters_data = True
|
|
|
|
|
|
|
|
def get_or_create(self, **kwargs):
|
|
|
|
kwargs[rel_field.name] = self.instance
|
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
return super(RelatedManager, self.db_manager(db)).get_or_create(**kwargs)
|
|
|
|
get_or_create.alters_data = True
|
|
|
|
|
2014-10-08 04:27:31 +08:00
|
|
|
def update_or_create(self, **kwargs):
|
|
|
|
kwargs[rel_field.name] = self.instance
|
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
return super(RelatedManager, self.db_manager(db)).update_or_create(**kwargs)
|
|
|
|
update_or_create.alters_data = True
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
# remove() and clear() are only provided if the ForeignKey can have a value of null.
|
|
|
|
if rel_field.null:
|
2013-11-22 01:14:16 +08:00
|
|
|
def remove(self, *objs, **kwargs):
|
2013-09-27 07:35:53 +08:00
|
|
|
if not objs:
|
|
|
|
return
|
2013-11-22 01:14:16 +08:00
|
|
|
bulk = kwargs.pop('bulk', True)
|
2013-09-19 01:31:07 +08:00
|
|
|
val = rel_field.get_foreign_related_value(self.instance)
|
2013-09-27 07:35:53 +08:00
|
|
|
old_ids = set()
|
2013-09-19 01:31:07 +08:00
|
|
|
for obj in objs:
|
|
|
|
# Is obj actually part of this descriptor set?
|
|
|
|
if rel_field.get_local_related_value(obj) == val:
|
2013-09-27 07:35:53 +08:00
|
|
|
old_ids.add(obj.pk)
|
2013-09-19 01:31:07 +08:00
|
|
|
else:
|
|
|
|
raise rel_field.rel.to.DoesNotExist("%r is not related to %r." % (obj, self.instance))
|
2013-11-22 01:14:16 +08:00
|
|
|
self._clear(self.filter(pk__in=old_ids), bulk)
|
2013-09-19 01:31:07 +08:00
|
|
|
remove.alters_data = True
|
|
|
|
|
2013-11-22 01:14:16 +08:00
|
|
|
def clear(self, **kwargs):
|
|
|
|
bulk = kwargs.pop('bulk', True)
|
|
|
|
self._clear(self, bulk)
|
2013-09-19 01:31:07 +08:00
|
|
|
clear.alters_data = True
|
|
|
|
|
2013-11-22 01:14:16 +08:00
|
|
|
def _clear(self, queryset, bulk):
|
|
|
|
db = router.db_for_write(self.model, instance=self.instance)
|
|
|
|
queryset = queryset.using(db)
|
|
|
|
if bulk:
|
2014-03-30 02:57:28 +08:00
|
|
|
# `QuerySet.update()` is intrinsically atomic.
|
2013-11-22 01:14:16 +08:00
|
|
|
queryset.update(**{rel_field.name: None})
|
|
|
|
else:
|
2014-03-21 21:21:43 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
2013-11-22 01:14:16 +08:00
|
|
|
for obj in queryset:
|
|
|
|
setattr(obj, rel_field.name, None)
|
2013-11-29 04:01:08 +08:00
|
|
|
obj.save(update_fields=[rel_field.name])
|
2013-11-22 01:14:16 +08:00
|
|
|
_clear.alters_data = True
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
return RelatedManager
|
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ForeignRelatedObjectsDescriptor(object):
|
|
|
|
# This class provides the functionality that makes the related-object
|
|
|
|
# managers available as attributes on a model class, for fields that have
|
|
|
|
# multiple "remote" values and have a ForeignKey pointed at them by
|
|
|
|
# some other model. In the example "poll.choice_set", the choice_set
|
|
|
|
# attribute is a ForeignRelatedObjectsDescriptor instance.
|
|
|
|
def __init__(self, related):
|
|
|
|
self.related = related # RelatedObject instance
|
|
|
|
|
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-10-06 19:08:11 +08:00
|
|
|
return self.related_manager_cls(instance)
|
2009-03-20 11:57:12 +08:00
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2014-03-27 19:32:48 +08:00
|
|
|
# Force evaluation of `value` in case it's a queryset whose
|
|
|
|
# value could be affected by `manager.clear()`. Refs #19816.
|
|
|
|
value = tuple(value)
|
2014-03-30 02:57:28 +08:00
|
|
|
|
2014-03-27 19:32:48 +08:00
|
|
|
manager = self.__get__(instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
db = router.db_for_write(manager.model, instance=manager.instance)
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
# If the foreign key can support nulls, then completely clear the related set.
|
|
|
|
# Otherwise, just move the named objects into the set.
|
|
|
|
if self.related.field.null:
|
|
|
|
manager.clear()
|
|
|
|
manager.add(*value)
|
2009-03-20 11:57:12 +08:00
|
|
|
|
2011-10-06 19:08:11 +08:00
|
|
|
@cached_property
|
|
|
|
def related_manager_cls(self):
|
|
|
|
# Dynamically create a class that subclasses the related model's default
|
|
|
|
# manager.
|
2013-09-19 01:31:07 +08:00
|
|
|
return create_foreign_related_manager(
|
2015-01-07 08:16:35 +08:00
|
|
|
self.related.related_model._default_manager.__class__,
|
2013-09-19 01:31:07 +08:00
|
|
|
self.related.field,
|
2015-01-07 08:16:35 +08:00
|
|
|
self.related.related_model,
|
2013-09-19 01:31:07 +08:00
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-09-29 23:30:29 +08:00
|
|
|
|
2011-09-29 23:30:19 +08:00
|
|
|
def create_many_related_manager(superclass, rel):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""Creates a manager that subclasses 'superclass' (which is a Manager)
|
|
|
|
and adds behavior for many-to-many related objects."""
|
|
|
|
class ManyRelatedManager(superclass):
|
2011-10-06 07:14:52 +08:00
|
|
|
def __init__(self, model=None, query_field_name=None, instance=None, symmetrical=None,
|
2011-09-29 23:30:29 +08:00
|
|
|
source_field_name=None, target_field_name=None, reverse=False,
|
2011-10-06 07:14:52 +08:00
|
|
|
through=None, prefetch_cache_name=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
super(ManyRelatedManager, self).__init__()
|
|
|
|
self.model = model
|
2011-10-06 07:14:52 +08:00
|
|
|
self.query_field_name = query_field_name
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
source_field = through._meta.get_field(source_field_name)
|
|
|
|
source_related_fields = source_field.related_fields
|
|
|
|
|
|
|
|
self.core_filters = {}
|
|
|
|
for lh_field, rh_field in source_related_fields:
|
|
|
|
self.core_filters['%s__%s' % (query_field_name, rh_field.name)] = getattr(instance, rh_field.attname)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
self.instance = instance
|
2011-09-29 23:30:29 +08:00
|
|
|
self.symmetrical = symmetrical
|
2013-03-25 00:40:40 +08:00
|
|
|
self.source_field = source_field
|
2013-09-27 07:35:53 +08:00
|
|
|
self.target_field = through._meta.get_field(target_field_name)
|
2009-11-03 22:02:49 +08:00
|
|
|
self.source_field_name = source_field_name
|
|
|
|
self.target_field_name = target_field_name
|
2011-09-29 23:30:29 +08:00
|
|
|
self.reverse = reverse
|
2008-07-29 20:41:08 +08:00
|
|
|
self.through = through
|
2011-10-06 07:14:52 +08:00
|
|
|
self.prefetch_cache_name = prefetch_cache_name
|
2013-03-25 00:40:40 +08:00
|
|
|
self.related_val = source_field.get_foreign_related_value(instance)
|
|
|
|
if None in self.related_val:
|
2012-10-28 22:47:07 +08:00
|
|
|
raise ValueError('"%r" needs to have a value for field "%s" before '
|
|
|
|
'this many-to-many relationship can be used.' %
|
|
|
|
(instance, source_field_name))
|
|
|
|
# Even if this relation is not to pk, we require still pk value.
|
|
|
|
# The wish is that the instance has been already saved to DB,
|
|
|
|
# although having a pk value isn't a guarantee of that.
|
|
|
|
if instance.pk is None:
|
|
|
|
raise ValueError("%r instance needs to have a primary key value before "
|
|
|
|
"a many-to-many relationship can be used." %
|
|
|
|
instance.__class__.__name__)
|
|
|
|
|
2013-09-19 01:31:07 +08:00
|
|
|
def __call__(self, **kwargs):
|
|
|
|
# We use **kwargs rather than a kwarg argument to enforce the
|
|
|
|
# `manager='manager_name'` syntax.
|
|
|
|
manager = getattr(self.model, kwargs.pop('manager'))
|
|
|
|
manager_class = create_many_related_manager(manager.__class__, rel)
|
|
|
|
return manager_class(
|
|
|
|
model=self.model,
|
|
|
|
query_field_name=self.query_field_name,
|
|
|
|
instance=self.instance,
|
|
|
|
symmetrical=self.symmetrical,
|
|
|
|
source_field_name=self.source_field_name,
|
|
|
|
target_field_name=self.target_field_name,
|
|
|
|
reverse=self.reverse,
|
|
|
|
through=self.through,
|
|
|
|
prefetch_cache_name=self.prefetch_cache_name,
|
|
|
|
)
|
2013-09-30 11:21:21 +08:00
|
|
|
do_not_call_in_templates = True
|
2013-09-19 01:31:07 +08:00
|
|
|
|
2013-10-09 20:29:14 +08:00
|
|
|
def _build_remove_filters(self, removed_vals):
|
|
|
|
filters = Q(**{self.source_field_name: self.related_val})
|
|
|
|
# No need to add a subquery condition if removed_vals is a QuerySet without
|
|
|
|
# filters.
|
|
|
|
removed_vals_filters = (not isinstance(removed_vals, QuerySet) or
|
|
|
|
removed_vals._has_filters())
|
|
|
|
if removed_vals_filters:
|
|
|
|
filters &= Q(**{'%s__in' % self.target_field_name: removed_vals})
|
2013-09-27 07:35:53 +08:00
|
|
|
if self.symmetrical:
|
2013-10-09 20:29:14 +08:00
|
|
|
symmetrical_filters = Q(**{self.target_field_name: self.related_val})
|
|
|
|
if removed_vals_filters:
|
|
|
|
symmetrical_filters &= Q(
|
|
|
|
**{'%s__in' % self.source_field_name: removed_vals})
|
|
|
|
filters |= symmetrical_filters
|
2013-09-27 07:35:53 +08:00
|
|
|
return filters
|
|
|
|
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
2011-10-06 07:14:52 +08:00
|
|
|
try:
|
|
|
|
return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
|
|
|
|
except (AttributeError, KeyError):
|
2013-09-30 13:05:43 +08:00
|
|
|
qs = super(ManyRelatedManager, self).get_queryset()
|
|
|
|
qs._add_hints(instance=self.instance)
|
|
|
|
if self._db:
|
|
|
|
qs = qs.using(self._db)
|
|
|
|
return qs._next_is_sticky().filter(**self.core_filters)
|
2011-10-06 07:14:52 +08:00
|
|
|
|
2013-11-07 01:25:05 +08:00
|
|
|
def get_prefetch_queryset(self, instances, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = super(ManyRelatedManager, self).get_queryset()
|
|
|
|
|
|
|
|
queryset._add_hints(instance=instances[0])
|
|
|
|
queryset = queryset.using(queryset._db or self._db)
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
query = {'%s__in' % self.query_field_name: instances}
|
2013-11-07 01:25:05 +08:00
|
|
|
queryset = queryset._next_is_sticky().filter(**query)
|
2011-10-06 07:14:52 +08:00
|
|
|
|
|
|
|
# M2M: need to annotate the query in order to get the primary model
|
|
|
|
# that the secondary model was actually related to. We know that
|
|
|
|
# there will already be a join on the join table, so we can just add
|
|
|
|
# the select.
|
|
|
|
|
|
|
|
# For non-autocreated 'through' models, can't assume we are
|
|
|
|
# dealing with PK values.
|
|
|
|
fk = self.through._meta.get_field(self.source_field_name)
|
|
|
|
join_table = self.through._meta.db_table
|
2013-11-07 01:25:05 +08:00
|
|
|
connection = connections[queryset.db]
|
2011-10-06 07:14:52 +08:00
|
|
|
qn = connection.ops.quote_name
|
2014-12-07 05:00:09 +08:00
|
|
|
queryset = queryset.extra(select={
|
|
|
|
'_prefetch_related_val_%s' % f.attname:
|
|
|
|
'%s.%s' % (qn(join_table), qn(f.column)) for f in fk.local_related_fields})
|
2014-09-04 20:15:09 +08:00
|
|
|
return (
|
|
|
|
queryset,
|
|
|
|
lambda result: tuple(
|
|
|
|
getattr(result, '_prefetch_related_val_%s' % f.attname)
|
|
|
|
for f in fk.local_related_fields
|
|
|
|
),
|
|
|
|
lambda inst: tuple(getattr(inst, f.attname) for f in fk.foreign_related_fields),
|
|
|
|
False,
|
|
|
|
self.prefetch_cache_name,
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-02-09 21:54:46 +08:00
|
|
|
def add(self, *objs):
|
|
|
|
if not rel.through._meta.auto_created:
|
|
|
|
opts = self.through._meta
|
|
|
|
raise AttributeError(
|
2014-09-04 20:15:09 +08:00
|
|
|
"Cannot use add() on a ManyToManyField which specifies an "
|
|
|
|
"intermediary model. Use %s.%s's Manager instead." %
|
2014-02-09 21:54:46 +08:00
|
|
|
(opts.app_label, opts.object_name)
|
|
|
|
)
|
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
self._add_items(self.source_field_name, self.target_field_name, *objs)
|
|
|
|
|
|
|
|
# If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table
|
|
|
|
if self.symmetrical:
|
|
|
|
self._add_items(self.target_field_name, self.source_field_name, *objs)
|
2014-02-09 21:54:46 +08:00
|
|
|
add.alters_data = True
|
|
|
|
|
|
|
|
def remove(self, *objs):
|
|
|
|
if not rel.through._meta.auto_created:
|
|
|
|
opts = self.through._meta
|
|
|
|
raise AttributeError(
|
2014-09-04 20:15:09 +08:00
|
|
|
"Cannot use remove() on a ManyToManyField which specifies "
|
|
|
|
"an intermediary model. Use %s.%s's Manager instead." %
|
2014-02-09 21:54:46 +08:00
|
|
|
(opts.app_label, opts.object_name)
|
|
|
|
)
|
|
|
|
self._remove_items(self.source_field_name, self.target_field_name, *objs)
|
|
|
|
remove.alters_data = True
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def clear(self):
|
2013-09-27 07:35:53 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
signals.m2m_changed.send(sender=self.through, action="pre_clear",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=None, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
filters = self._build_remove_filters(super(ManyRelatedManager, self).get_queryset().using(db))
|
|
|
|
self.through._default_manager.using(db).filter(filters).delete()
|
2013-09-27 07:35:53 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
signals.m2m_changed.send(sender=self.through, action="post_clear",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=None, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
clear.alters_data = True
|
|
|
|
|
|
|
|
def create(self, **kwargs):
|
2008-07-29 20:41:08 +08:00
|
|
|
# This check needs to be done here, since we can't later remove this
|
|
|
|
# from the method lookup table, as we do with add and remove.
|
2011-09-29 23:30:29 +08:00
|
|
|
if not self.through._meta.auto_created:
|
|
|
|
opts = self.through._meta
|
2013-12-13 04:23:24 +08:00
|
|
|
raise AttributeError(
|
2014-09-04 20:15:09 +08:00
|
|
|
"Cannot use create() on a ManyToManyField which specifies "
|
|
|
|
"an intermediary model. Use %s.%s's Manager instead." %
|
2013-12-13 04:23:24 +08:00
|
|
|
(opts.app_label, opts.object_name)
|
|
|
|
)
|
2010-01-22 22:30:06 +08:00
|
|
|
db = router.db_for_write(self.instance.__class__, instance=self.instance)
|
2010-10-28 20:57:04 +08:00
|
|
|
new_obj = super(ManyRelatedManager, self.db_manager(db)).create(**kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.add(new_obj)
|
|
|
|
return new_obj
|
|
|
|
create.alters_data = True
|
|
|
|
|
2008-08-17 04:59:06 +08:00
|
|
|
def get_or_create(self, **kwargs):
|
2010-01-22 22:30:06 +08:00
|
|
|
db = router.db_for_write(self.instance.__class__, instance=self.instance)
|
2014-10-08 04:27:31 +08:00
|
|
|
obj, created = super(ManyRelatedManager, self.db_manager(db)).get_or_create(**kwargs)
|
2008-08-17 04:59:06 +08:00
|
|
|
# We only need to add() if created because if we got an object back
|
|
|
|
# from get() then the relationship already exists.
|
|
|
|
if created:
|
|
|
|
self.add(obj)
|
|
|
|
return obj, created
|
|
|
|
get_or_create.alters_data = True
|
2014-10-08 04:27:31 +08:00
|
|
|
|
|
|
|
def update_or_create(self, **kwargs):
|
|
|
|
db = router.db_for_write(self.instance.__class__, instance=self.instance)
|
|
|
|
obj, created = super(ManyRelatedManager, self.db_manager(db)).update_or_create(**kwargs)
|
|
|
|
# We only need to add() if created because if we got an object back
|
|
|
|
# from get() then the relationship already exists.
|
|
|
|
if created:
|
|
|
|
self.add(obj)
|
|
|
|
return obj, created
|
|
|
|
update_or_create.alters_data = True
|
2008-08-17 04:59:06 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def _add_items(self, source_field_name, target_field_name, *objs):
|
2011-09-27 01:20:20 +08:00
|
|
|
# source_field_name: the PK fieldname in join table for the source object
|
|
|
|
# target_field_name: the PK fieldname in join table for the target object
|
2007-01-30 00:09:25 +08:00
|
|
|
# *objs - objects to add. Either object instances, or primary keys of object instances.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-12-20 21:57:17 +08:00
|
|
|
# If there aren't any objects, there is nothing to do.
|
2009-11-03 22:02:49 +08:00
|
|
|
from django.db.models import Model
|
2006-12-20 21:57:17 +08:00
|
|
|
if objs:
|
2007-01-30 00:09:25 +08:00
|
|
|
new_ids = set()
|
2006-12-20 22:06:27 +08:00
|
|
|
for obj in objs:
|
2007-01-30 00:09:25 +08:00
|
|
|
if isinstance(obj, self.model):
|
2010-01-22 22:30:06 +08:00
|
|
|
if not router.allow_relation(obj, self.instance):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot add "%r": instance is on database "%s", value is on database "%s"' %
|
|
|
|
(obj, self.instance._state.db, obj._state.db)
|
|
|
|
)
|
2013-08-20 22:13:41 +08:00
|
|
|
fk_val = self.through._meta.get_field(
|
|
|
|
target_field_name).get_foreign_related_value(obj)[0]
|
2012-10-28 22:47:07 +08:00
|
|
|
if fk_val is None:
|
2013-12-13 04:23:24 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Cannot add "%r": the value for field "%s" is None' %
|
|
|
|
(obj, target_field_name)
|
|
|
|
)
|
2013-08-20 22:13:41 +08:00
|
|
|
new_ids.add(fk_val)
|
2009-03-31 06:41:49 +08:00
|
|
|
elif isinstance(obj, Model):
|
2013-12-13 04:23:24 +08:00
|
|
|
raise TypeError(
|
|
|
|
"'%s' instance expected, got %r" %
|
|
|
|
(self.model._meta.object_name, obj)
|
|
|
|
)
|
2007-01-30 00:09:25 +08:00
|
|
|
else:
|
|
|
|
new_ids.add(obj)
|
2014-03-30 02:57:28 +08:00
|
|
|
|
2011-01-13 12:11:41 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
vals = (self.through._default_manager.using(db)
|
|
|
|
.values_list(target_field_name, flat=True)
|
|
|
|
.filter(**{
|
|
|
|
source_field_name: self.related_val[0],
|
|
|
|
'%s__in' % target_field_name: new_ids,
|
|
|
|
}))
|
2010-01-13 19:07:16 +08:00
|
|
|
new_ids = new_ids - set(vals)
|
2010-03-30 19:54:56 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
if self.reverse or source_field_name == self.source_field_name:
|
|
|
|
# Don't send the signal when we are inserting the
|
|
|
|
# duplicate data row for symmetrical reverse entries.
|
|
|
|
signals.m2m_changed.send(sender=self.through, action='pre_add',
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=new_ids, using=db)
|
|
|
|
|
|
|
|
# Add the ones that aren't there already
|
|
|
|
self.through._default_manager.using(db).bulk_create([
|
|
|
|
self.through(**{
|
|
|
|
'%s_id' % source_field_name: self.related_val[0],
|
|
|
|
'%s_id' % target_field_name: obj_id,
|
|
|
|
})
|
|
|
|
for obj_id in new_ids
|
|
|
|
])
|
|
|
|
|
|
|
|
if self.reverse or source_field_name == self.source_field_name:
|
|
|
|
# Don't send the signal when we are inserting the
|
|
|
|
# duplicate data row for symmetrical reverse entries.
|
|
|
|
signals.m2m_changed.send(sender=self.through, action='post_add',
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=new_ids, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def _remove_items(self, source_field_name, target_field_name, *objs):
|
2011-09-27 01:20:20 +08:00
|
|
|
# source_field_name: the PK colname in join table for the source object
|
|
|
|
# target_field_name: the PK colname in join table for the target object
|
2006-05-02 09:31:56 +08:00
|
|
|
# *objs - objects to remove
|
2013-09-27 07:35:53 +08:00
|
|
|
if not objs:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Check that all the objects are of the right type
|
|
|
|
old_ids = set()
|
|
|
|
for obj in objs:
|
|
|
|
if isinstance(obj, self.model):
|
|
|
|
fk_val = self.target_field.get_foreign_related_value(obj)[0]
|
|
|
|
old_ids.add(fk_val)
|
|
|
|
else:
|
|
|
|
old_ids.add(obj)
|
2009-11-03 22:02:49 +08:00
|
|
|
|
2011-01-13 12:11:41 +08:00
|
|
|
db = router.db_for_write(self.through, instance=self.instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
# Send a signal to the other end if need be.
|
|
|
|
signals.m2m_changed.send(sender=self.through, action="pre_remove",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=old_ids, using=db)
|
|
|
|
target_model_qs = super(ManyRelatedManager, self).get_queryset()
|
|
|
|
if target_model_qs._has_filters():
|
|
|
|
old_vals = target_model_qs.using(db).filter(**{
|
|
|
|
'%s__in' % self.target_field.related_field.attname: old_ids})
|
|
|
|
else:
|
|
|
|
old_vals = old_ids
|
|
|
|
filters = self._build_remove_filters(old_vals)
|
|
|
|
self.through._default_manager.using(db).filter(filters).delete()
|
2013-09-27 07:35:53 +08:00
|
|
|
|
2014-03-30 02:57:28 +08:00
|
|
|
signals.m2m_changed.send(sender=self.through, action="post_remove",
|
|
|
|
instance=self.instance, reverse=self.reverse,
|
|
|
|
model=self.model, pk_set=old_ids, using=db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
return ManyRelatedManager
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ManyRelatedObjectsDescriptor(object):
|
|
|
|
# This class provides the functionality that makes the related-object
|
|
|
|
# managers available as attributes on a model class, for fields that have
|
|
|
|
# multiple "remote" values and have a ManyToManyField pointed at them by
|
|
|
|
# some other model (rather than having a ManyToManyField themselves).
|
|
|
|
# In the example "publication.article_set", the article_set attribute is a
|
|
|
|
# ManyRelatedObjectsDescriptor instance.
|
|
|
|
def __init__(self, related):
|
|
|
|
self.related = related # RelatedObject instance
|
|
|
|
|
2011-09-30 18:41:25 +08:00
|
|
|
@cached_property
|
|
|
|
def related_manager_cls(self):
|
|
|
|
# Dynamically create a class that subclasses the related
|
|
|
|
# model's default manager.
|
|
|
|
return create_many_related_manager(
|
2015-01-07 08:16:35 +08:00
|
|
|
self.related.related_model._default_manager.__class__,
|
2011-09-30 18:41:25 +08:00
|
|
|
self.related.field.rel
|
|
|
|
)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
rel_model = self.related.related_model
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-09-30 18:41:25 +08:00
|
|
|
manager = self.related_manager_cls(
|
2006-05-02 09:31:56 +08:00
|
|
|
model=rel_model,
|
2011-10-06 07:14:52 +08:00
|
|
|
query_field_name=self.related.field.name,
|
|
|
|
prefetch_cache_name=self.related.field.related_query_name(),
|
2006-05-02 09:31:56 +08:00
|
|
|
instance=instance,
|
|
|
|
symmetrical=False,
|
2009-11-03 22:02:49 +08:00
|
|
|
source_field_name=self.related.field.m2m_reverse_field_name(),
|
2010-01-13 19:07:16 +08:00
|
|
|
target_field_name=self.related.field.m2m_field_name(),
|
2011-09-29 23:30:29 +08:00
|
|
|
reverse=True,
|
|
|
|
through=self.related.field.rel.through,
|
2006-05-02 09:31:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
return manager
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2009-11-03 22:02:49 +08:00
|
|
|
if not self.related.field.rel.through._meta.auto_created:
|
|
|
|
opts = self.related.field.rel.through._meta
|
2014-09-04 20:15:09 +08:00
|
|
|
raise AttributeError(
|
|
|
|
"Cannot set values on a ManyToManyField which specifies an "
|
|
|
|
"intermediary model. Use %s.%s's Manager instead." % (opts.app_label, opts.object_name)
|
|
|
|
)
|
2008-07-29 20:41:08 +08:00
|
|
|
|
2014-03-27 19:32:48 +08:00
|
|
|
# Force evaluation of `value` in case it's a queryset whose
|
|
|
|
# value could be affected by `manager.clear()`. Refs #19816.
|
|
|
|
value = tuple(value)
|
2014-03-30 02:57:28 +08:00
|
|
|
|
2014-03-27 19:32:48 +08:00
|
|
|
manager = self.__get__(instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
db = router.db_for_write(manager.through, instance=manager.instance)
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
manager.clear()
|
|
|
|
manager.add(*value)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ReverseManyRelatedObjectsDescriptor(object):
|
|
|
|
# This class provides the functionality that makes the related-object
|
|
|
|
# managers available as attributes on a model class, for fields that have
|
|
|
|
# multiple "remote" values and have a ManyToManyField defined in their
|
|
|
|
# model (rather than having another model pointed *at* them).
|
|
|
|
# In the example "article.publications", the publications attribute is a
|
|
|
|
# ReverseManyRelatedObjectsDescriptor instance.
|
|
|
|
def __init__(self, m2m_field):
|
|
|
|
self.field = m2m_field
|
2009-11-13 20:34:41 +08:00
|
|
|
|
2011-09-10 03:22:28 +08:00
|
|
|
@property
|
|
|
|
def through(self):
|
2009-11-03 23:02:16 +08:00
|
|
|
# through is provided so that you have easy access to the through
|
2009-11-13 20:34:41 +08:00
|
|
|
# model (Book.authors.through) for inlines, etc. This is done as
|
|
|
|
# a property to ensure that the fully resolved value is returned.
|
|
|
|
return self.field.rel.through
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-09-30 18:41:25 +08:00
|
|
|
@cached_property
|
|
|
|
def related_manager_cls(self):
|
|
|
|
# Dynamically create a class that subclasses the related model's
|
|
|
|
# default manager.
|
|
|
|
return create_many_related_manager(
|
|
|
|
self.field.rel.to._default_manager.__class__,
|
|
|
|
self.field.rel
|
|
|
|
)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __get__(self, instance, instance_type=None):
|
|
|
|
if instance is None:
|
2008-12-03 06:09:51 +08:00
|
|
|
return self
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-09-30 18:41:25 +08:00
|
|
|
manager = self.related_manager_cls(
|
|
|
|
model=self.field.rel.to,
|
2011-10-06 07:14:52 +08:00
|
|
|
query_field_name=self.field.related_query_name(),
|
|
|
|
prefetch_cache_name=self.field.name,
|
2006-05-02 09:31:56 +08:00
|
|
|
instance=instance,
|
2010-04-02 23:44:48 +08:00
|
|
|
symmetrical=self.field.rel.symmetrical,
|
2009-11-03 22:02:49 +08:00
|
|
|
source_field_name=self.field.m2m_field_name(),
|
2010-01-13 19:07:16 +08:00
|
|
|
target_field_name=self.field.m2m_reverse_field_name(),
|
2011-09-29 23:30:29 +08:00
|
|
|
reverse=False,
|
|
|
|
through=self.field.rel.through,
|
2006-05-02 09:31:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
return manager
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
2009-11-03 22:02:49 +08:00
|
|
|
if not self.field.rel.through._meta.auto_created:
|
|
|
|
opts = self.field.rel.through._meta
|
2014-09-04 20:15:09 +08:00
|
|
|
raise AttributeError(
|
|
|
|
"Cannot set values on a ManyToManyField which specifies an "
|
|
|
|
"intermediary model. Use %s.%s's Manager instead." % (opts.app_label, opts.object_name)
|
|
|
|
)
|
2008-07-29 20:41:08 +08:00
|
|
|
|
2014-03-27 19:32:48 +08:00
|
|
|
# Force evaluation of `value` in case it's a queryset whose
|
|
|
|
# value could be affected by `manager.clear()`. Refs #19816.
|
2013-02-23 22:50:33 +08:00
|
|
|
value = tuple(value)
|
2014-03-30 02:57:28 +08:00
|
|
|
|
2014-03-27 19:32:48 +08:00
|
|
|
manager = self.__get__(instance)
|
2014-03-30 02:57:28 +08:00
|
|
|
db = router.db_for_write(manager.through, instance=manager.instance)
|
|
|
|
with transaction.atomic(using=db, savepoint=False):
|
|
|
|
manager.clear()
|
|
|
|
manager.add(*value)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
class ForeignObjectRel(object):
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
auto_created = True
|
|
|
|
concrete = False
|
|
|
|
editable = False
|
|
|
|
is_relation = True
|
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, on_delete=None):
|
2013-03-25 00:40:40 +08:00
|
|
|
self.field = field
|
|
|
|
self.to = to
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
self.related_name = related_name
|
2013-06-27 21:44:21 +08:00
|
|
|
self.related_query_name = related_query_name
|
2013-03-25 00:40:40 +08:00
|
|
|
self.limit_choices_to = {} if limit_choices_to is None else limit_choices_to
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self.parent_link = parent_link
|
2010-11-10 00:46:42 +08:00
|
|
|
self.on_delete = on_delete
|
2015-01-27 22:40:01 +08:00
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
self.symmetrical = False
|
2015-01-27 22:40:01 +08:00
|
|
|
self.multiple = True
|
2013-11-09 20:25:15 +08:00
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# Some of the following cached_properties can't be initialized in
|
2013-11-09 20:25:15 +08:00
|
|
|
# __init__ as the field doesn't have its model yet. Calling these methods
|
|
|
|
# before field.contribute_to_class() has been called will result in
|
|
|
|
# AttributeError
|
|
|
|
@cached_property
|
|
|
|
def model(self):
|
2015-01-07 08:16:35 +08:00
|
|
|
return self.to
|
2013-11-09 20:25:15 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def opts(self):
|
2015-01-07 08:16:35 +08:00
|
|
|
return self.related_model._meta
|
2013-11-09 20:25:15 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def to_opts(self):
|
|
|
|
return self.to._meta
|
|
|
|
|
|
|
|
@cached_property
|
2015-01-07 08:16:35 +08:00
|
|
|
def hidden(self):
|
|
|
|
return self.is_hidden()
|
2013-11-09 20:25:15 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def name(self):
|
2015-01-07 08:16:35 +08:00
|
|
|
return self.field.related_query_name()
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def related_model(self):
|
|
|
|
if not self.field.model:
|
|
|
|
raise AttributeError(
|
|
|
|
"This property can't be accessed before self.field.contribute_to_class has been called.")
|
|
|
|
return self.field.model
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def many_to_many(self):
|
|
|
|
return self.field.many_to_many
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def many_to_one(self):
|
|
|
|
return self.field.one_to_many
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def one_to_many(self):
|
|
|
|
return self.field.many_to_one
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def one_to_one(self):
|
|
|
|
return self.field.one_to_one
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<%s: %s.%s>' % (type(self).__name__, self.opts.app_label, self.opts.model_name)
|
2013-11-09 20:25:15 +08:00
|
|
|
|
|
|
|
def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH,
|
|
|
|
limit_to_currently_related=False):
|
|
|
|
"""
|
|
|
|
Returns choices with a default blank choices included, for use as
|
|
|
|
SelectField choices for this field.
|
|
|
|
|
|
|
|
Analog of django.db.models.fields.Field.get_choices(), provided
|
|
|
|
initially for utilization by RelatedFieldListFilter.
|
|
|
|
"""
|
|
|
|
first_choice = blank_choice if include_blank else []
|
2015-01-07 08:16:35 +08:00
|
|
|
queryset = self.related_model._default_manager.all()
|
2013-11-09 20:25:15 +08:00
|
|
|
if limit_to_currently_related:
|
|
|
|
queryset = queryset.complex_filter(
|
2015-01-07 08:16:35 +08:00
|
|
|
{'%s__isnull' % self.related_model._meta.model_name: False}
|
2013-11-09 20:25:15 +08:00
|
|
|
)
|
|
|
|
lst = [(x._get_pk_val(), smart_text(x)) for x in queryset]
|
|
|
|
return first_choice + lst
|
|
|
|
|
|
|
|
def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
|
|
|
|
# Defer to the actual field definition for db prep
|
|
|
|
return self.field.get_db_prep_lookup(lookup_type, value, connection=connection, prepared=prepared)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2009-11-03 22:02:49 +08:00
|
|
|
def is_hidden(self):
|
|
|
|
"Should the related object be hidden?"
|
2015-01-07 08:16:35 +08:00
|
|
|
return self.related_name is not None and self.related_name[-1] == '+'
|
2009-11-03 22:02:49 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
def get_joining_columns(self):
|
|
|
|
return self.field.get_reverse_joining_columns()
|
|
|
|
|
|
|
|
def get_extra_restriction(self, where_class, alias, related_alias):
|
|
|
|
return self.field.get_extra_restriction(where_class, related_alias, alias)
|
|
|
|
|
2013-03-25 02:57:02 +08:00
|
|
|
def set_field_name(self):
|
|
|
|
"""
|
|
|
|
Sets the related field's name, this is not available until later stages
|
|
|
|
of app loading, so set_field_name is called from
|
|
|
|
set_attributes_from_rel()
|
|
|
|
"""
|
|
|
|
# By default foreign object doesn't relate to any remote field (for
|
|
|
|
# example custom multicolumn joins currently have no remote field).
|
|
|
|
self.field_name = None
|
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
def get_accessor_name(self, model=None):
|
|
|
|
# This method encapsulates the logic that decides what name to give an
|
|
|
|
# accessor descriptor that retrieves related many-to-one or
|
|
|
|
# many-to-many objects. It uses the lower-cased object_name + "_set",
|
|
|
|
# but this can be overridden with the "related_name" option.
|
|
|
|
# Due to backwards compatibility ModelForms need to be able to provide
|
|
|
|
# an alternate model. See BaseInlineFormSet.get_default_prefix().
|
|
|
|
opts = model._meta if model else self.opts
|
2015-01-07 08:16:35 +08:00
|
|
|
model = model or self.related_model
|
2013-11-09 20:25:15 +08:00
|
|
|
if self.multiple:
|
|
|
|
# If this is a symmetrical m2m relation on self, there is no reverse accessor.
|
|
|
|
if self.symmetrical and model == self.to:
|
|
|
|
return None
|
|
|
|
if self.related_name:
|
|
|
|
return self.related_name
|
|
|
|
if opts.default_related_name:
|
|
|
|
return self.opts.default_related_name % {
|
|
|
|
'model_name': self.opts.model_name.lower(),
|
|
|
|
'app_label': self.opts.app_label.lower(),
|
|
|
|
}
|
|
|
|
return opts.model_name + ('_set' if self.multiple else '')
|
|
|
|
|
|
|
|
def get_cache_name(self):
|
|
|
|
return "_%s_cache" % self.get_accessor_name()
|
|
|
|
|
|
|
|
def get_path_info(self):
|
|
|
|
return self.field.get_reverse_path_info()
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
class ManyToOneRel(ForeignObjectRel):
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, field_name, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, on_delete=None):
|
2013-03-25 00:40:40 +08:00
|
|
|
super(ManyToOneRel, self).__init__(
|
2015-01-27 22:40:01 +08:00
|
|
|
field, to,
|
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
parent_link=parent_link,
|
|
|
|
on_delete=on_delete,
|
|
|
|
)
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
self.field_name = field_name
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def get_related_field(self):
|
|
|
|
"""
|
|
|
|
Returns the Field in the 'to' object to which this relationship is
|
|
|
|
tied.
|
|
|
|
"""
|
2015-01-07 08:16:35 +08:00
|
|
|
field = self.to._meta.get_field(self.field_name)
|
|
|
|
if not field.concrete:
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
raise FieldDoesNotExist("No related field named '%s'" %
|
|
|
|
self.field_name)
|
2015-01-07 08:16:35 +08:00
|
|
|
return field
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2013-03-25 02:57:02 +08:00
|
|
|
def set_field_name(self):
|
|
|
|
self.field_name = self.field_name or self.to._meta.pk.name
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
class OneToOneRel(ManyToOneRel):
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, field_name, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, on_delete=None):
|
|
|
|
super(OneToOneRel, self).__init__(
|
|
|
|
field, to, field_name,
|
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
parent_link=parent_link,
|
|
|
|
on_delete=on_delete,
|
|
|
|
)
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
self.multiple = False
|
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2013-11-09 20:25:15 +08:00
|
|
|
class ManyToManyRel(ForeignObjectRel):
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, field, to, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, symmetrical=True, through=None, through_fields=None,
|
|
|
|
db_constraint=True):
|
|
|
|
super(ManyToManyRel, self).__init__(
|
|
|
|
field, to,
|
|
|
|
related_name=related_name,
|
|
|
|
related_query_name=related_query_name,
|
|
|
|
limit_choices_to=limit_choices_to,
|
|
|
|
)
|
|
|
|
|
2013-03-08 03:24:51 +08:00
|
|
|
if through and not db_constraint:
|
|
|
|
raise ValueError("Can't supply a through model and db_constraint=False")
|
2015-01-27 22:40:01 +08:00
|
|
|
self.through = through
|
|
|
|
|
2014-02-20 02:01:55 +08:00
|
|
|
if through_fields and not through:
|
|
|
|
raise ValueError("Cannot specify through_fields without a through model")
|
|
|
|
self.through_fields = through_fields
|
2015-01-27 22:40:01 +08:00
|
|
|
|
|
|
|
self.symmetrical = symmetrical
|
2013-03-08 03:24:51 +08:00
|
|
|
self.db_constraint = db_constraint
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
def is_hidden(self):
|
|
|
|
"Should the related object be hidden?"
|
|
|
|
return self.related_name is not None and self.related_name[-1] == '+'
|
|
|
|
|
2009-03-25 12:41:40 +08:00
|
|
|
def get_related_field(self):
|
|
|
|
"""
|
2014-11-17 00:21:33 +08:00
|
|
|
Returns the field in the 'to' object to which this relationship is tied.
|
|
|
|
Provided for symmetry with ManyToOneRel.
|
2009-03-25 12:41:40 +08:00
|
|
|
"""
|
2014-11-17 00:21:33 +08:00
|
|
|
opts = self.through._meta
|
|
|
|
if self.through_fields:
|
|
|
|
field = opts.get_field(self.through_fields[0])
|
|
|
|
else:
|
|
|
|
for field in opts.fields:
|
|
|
|
rel = getattr(field, 'rel', None)
|
|
|
|
if rel and rel.to == self.to:
|
|
|
|
break
|
|
|
|
return field.foreign_related_fields[0]
|
2009-03-25 12:41:40 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
class ForeignObject(RelatedField):
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
many_to_many = False
|
|
|
|
many_to_one = False
|
|
|
|
one_to_many = True
|
|
|
|
one_to_one = False
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
requires_unique_target = True
|
2013-08-20 13:52:32 +08:00
|
|
|
related_accessor_class = ForeignRelatedObjectsDescriptor
|
2015-01-27 22:40:01 +08:00
|
|
|
rel_class = ForeignObjectRel
|
2013-03-25 00:40:40 +08:00
|
|
|
|
2015-01-27 22:40:01 +08:00
|
|
|
def __init__(self, to, from_fields, to_fields, rel=None, related_name=None,
|
|
|
|
related_query_name=None, limit_choices_to=None, parent_link=False,
|
|
|
|
on_delete=CASCADE, swappable=True, **kwargs):
|
|
|
|
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):
|
|
|
|
rel_is_string = isinstance(self.rel.to, six.string_types)
|
|
|
|
if rel_is_string or not self.requires_unique_target:
|
|
|
|
return []
|
|
|
|
|
|
|
|
# Skip if the
|
|
|
|
try:
|
|
|
|
self.foreign_related_fields
|
|
|
|
except FieldDoesNotExist:
|
|
|
|
return []
|
|
|
|
|
|
|
|
try:
|
2013-11-09 20:25:15 +08:00
|
|
|
self.rel
|
2014-01-20 10:45:21 +08:00
|
|
|
except AttributeError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
has_unique_field = any(rel_field.unique
|
|
|
|
for rel_field in self.foreign_related_fields)
|
|
|
|
if not has_unique_field and len(self.foreign_related_fields) > 1:
|
2014-03-03 18:18:39 +08:00
|
|
|
field_combination = ', '.join("'%s'" % rel_field.name
|
2014-01-20 10:45:21 +08:00
|
|
|
for rel_field in self.foreign_related_fields)
|
|
|
|
model_name = self.rel.to.__name__
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-09-04 20:15:09 +08:00
|
|
|
"None of the fields %s on model '%s' have a unique=True constraint."
|
|
|
|
% (field_combination, model_name),
|
2014-03-03 18:18:39 +08:00
|
|
|
hint=None,
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E310',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
elif not has_unique_field:
|
|
|
|
field_name = self.foreign_related_fields[0].name
|
|
|
|
model_name = self.rel.to.__name__
|
|
|
|
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()
|
|
|
|
kwargs['from_fields'] = self.from_fields
|
|
|
|
kwargs['to_fields'] = self.to_fields
|
2014-08-15 10:47:02 +08:00
|
|
|
if self.rel.related_name is not None:
|
2014-12-12 03:36:13 +08:00
|
|
|
kwargs['related_name'] = self.rel.related_name
|
2014-08-15 10:47:02 +08:00
|
|
|
if self.rel.related_query_name is not None:
|
|
|
|
kwargs['related_query_name'] = self.rel.related_query_name
|
|
|
|
if self.rel.on_delete != CASCADE:
|
|
|
|
kwargs['on_delete'] = self.rel.on_delete
|
|
|
|
if self.rel.parent_link:
|
|
|
|
kwargs['parent_link'] = self.rel.parent_link
|
2014-01-15 22:20:47 +08:00
|
|
|
# Work out string form of "to"
|
2013-12-06 22:21:13 +08:00
|
|
|
if isinstance(self.rel.to, six.string_types):
|
|
|
|
kwargs['to'] = self.rel.to
|
|
|
|
else:
|
|
|
|
kwargs['to'] = "%s.%s" % (self.rel.to._meta.app_label, self.rel.to._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')
|
2013-05-09 21:13:15 +08:00
|
|
|
if isinstance(self.rel.to, six.string_types):
|
2013-12-25 00:26:50 +08:00
|
|
|
raise ValueError('Related model %r cannot be resolved' % self.rel.to)
|
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))
|
2013-03-25 00:40:40 +08:00
|
|
|
to_field = (self.rel.to._meta.pk if to_field_name is None
|
2015-01-07 08:16:35 +08:00
|
|
|
else self.rel.to._meta.get_field(to_field_name))
|
2013-03-25 00:40:40 +08:00
|
|
|
related_fields.append((from_field, to_field))
|
|
|
|
return related_fields
|
|
|
|
|
|
|
|
@property
|
|
|
|
def related_fields(self):
|
|
|
|
if not hasattr(self, '_related_fields'):
|
|
|
|
self._related_fields = self.resolve_related_fields()
|
|
|
|
return self._related_fields
|
|
|
|
|
|
|
|
@property
|
|
|
|
def reverse_related_fields(self):
|
|
|
|
return [(rhs_field, lhs_field) for lhs_field, rhs_field in self.related_fields]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def local_related_fields(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(lhs_field for lhs_field, rhs_field in self.related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def foreign_related_fields(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_local_related_value(self, instance):
|
|
|
|
return self.get_instance_value_for_fields(instance, self.local_related_fields)
|
|
|
|
|
|
|
|
def get_foreign_related_value(self, instance):
|
|
|
|
return self.get_instance_value_for_fields(instance, self.foreign_related_fields)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_instance_value_for_fields(instance, fields):
|
2013-08-20 21:23:25 +08:00
|
|
|
ret = []
|
2014-05-13 00:46:47 +08:00
|
|
|
opts = instance._meta
|
2013-08-20 21:23:25 +08:00
|
|
|
for field in fields:
|
|
|
|
# Gotcha: in some cases (like fixture loading) a model can have
|
|
|
|
# different values in parent_ptr_id and parent's id. So, use
|
|
|
|
# instance.pk (that is, parent_ptr_id) when asked for instance.id.
|
|
|
|
if field.primary_key:
|
2013-08-20 22:13:41 +08:00
|
|
|
possible_parent_link = opts.get_ancestor_link(field.model)
|
2014-05-13 00:46:47 +08:00
|
|
|
if (not possible_parent_link or
|
|
|
|
possible_parent_link.primary_key or
|
|
|
|
possible_parent_link.model._meta.abstract):
|
2013-08-20 22:13:41 +08:00
|
|
|
ret.append(instance.pk)
|
|
|
|
continue
|
|
|
|
ret.append(getattr(instance, field.attname))
|
2013-08-20 21:23:25 +08:00
|
|
|
return tuple(ret)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_attname_column(self):
|
|
|
|
attname, column = super(ForeignObject, self).get_attname_column()
|
|
|
|
return attname, None
|
|
|
|
|
|
|
|
def get_joining_columns(self, reverse_join=False):
|
|
|
|
source = self.reverse_related_fields if reverse_join else self.related_fields
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple((lhs_field.column, rhs_field.column) for lhs_field, rhs_field in source)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_reverse_joining_columns(self):
|
|
|
|
return self.get_joining_columns(reverse_join=True)
|
|
|
|
|
|
|
|
def get_extra_descriptor_filter(self, instance):
|
|
|
|
"""
|
|
|
|
Returns an extra filter condition for related object fetching when
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Returns 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.
|
|
|
|
"""
|
|
|
|
opts = self.rel.to._meta
|
|
|
|
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
|
|
|
|
from_opts = self.rel.to._meta
|
|
|
|
pathinfos = [PathInfo(from_opts, opts, (opts.pk,), self.rel, not self.unique, False)]
|
|
|
|
return pathinfos
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
def get_lookup_constraint(self, constraint_class, alias, targets, sources, lookups,
|
2013-03-25 00:40:40 +08:00
|
|
|
raw_value):
|
2014-01-18 17:09:43 +08:00
|
|
|
from django.db.models.sql.where import SubqueryConstraint, AND, OR
|
2013-03-25 00:40:40 +08:00
|
|
|
root_constraint = constraint_class()
|
|
|
|
assert len(targets) == len(sources)
|
2014-01-18 17:09:43 +08:00
|
|
|
if len(lookups) > 1:
|
|
|
|
raise exceptions.FieldError('Relation fields do not support nested lookups')
|
|
|
|
lookup_type = lookups[0]
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_normalized_value(value):
|
|
|
|
from django.db.models import Model
|
|
|
|
if isinstance(value, Model):
|
|
|
|
value_list = []
|
|
|
|
for source in sources:
|
|
|
|
# Account for one-to-one relations when sent a different model
|
2013-07-26 18:02:32 +08:00
|
|
|
while not isinstance(value, source.model) and source.rel:
|
2013-03-25 00:40:40 +08:00
|
|
|
source = source.rel.to._meta.get_field(source.rel.field_name)
|
|
|
|
value_list.append(getattr(value, source.attname))
|
|
|
|
return tuple(value_list)
|
|
|
|
elif not isinstance(value, tuple):
|
|
|
|
return (value,)
|
|
|
|
return value
|
|
|
|
|
|
|
|
is_multicolumn = len(self.related_fields) > 1
|
|
|
|
if (hasattr(raw_value, '_as_sql') or
|
|
|
|
hasattr(raw_value, 'get_compiler')):
|
|
|
|
root_constraint.add(SubqueryConstraint(alias, [target.column for target in targets],
|
|
|
|
[source.name for source in sources], raw_value),
|
|
|
|
AND)
|
|
|
|
elif lookup_type == 'isnull':
|
2014-12-01 15:28:01 +08:00
|
|
|
root_constraint.add(IsNull(targets[0].get_col(alias, sources[0]), raw_value), AND)
|
2013-03-25 00:40:40 +08:00
|
|
|
elif (lookup_type == 'exact' or (lookup_type in ['gt', 'lt', 'gte', 'lte']
|
|
|
|
and not is_multicolumn)):
|
|
|
|
value = get_normalized_value(raw_value)
|
2014-01-18 17:09:43 +08:00
|
|
|
for target, source, val in zip(targets, sources, value):
|
|
|
|
lookup_class = target.get_lookup(lookup_type)
|
2013-03-25 00:40:40 +08:00
|
|
|
root_constraint.add(
|
2014-12-01 15:28:01 +08:00
|
|
|
lookup_class(target.get_col(alias, source), val), AND)
|
2013-03-25 00:40:40 +08:00
|
|
|
elif lookup_type in ['range', 'in'] and not is_multicolumn:
|
|
|
|
values = [get_normalized_value(value) for value in raw_value]
|
|
|
|
value = [val[0] for val in values]
|
2014-01-18 17:09:43 +08:00
|
|
|
lookup_class = targets[0].get_lookup(lookup_type)
|
2014-12-01 15:28:01 +08:00
|
|
|
root_constraint.add(lookup_class(targets[0].get_col(alias, sources[0]), value), AND)
|
2013-03-25 00:40:40 +08:00
|
|
|
elif lookup_type == 'in':
|
|
|
|
values = [get_normalized_value(value) for value in raw_value]
|
2015-02-04 16:21:29 +08:00
|
|
|
root_constraint.connector = OR
|
2013-03-25 00:40:40 +08:00
|
|
|
for value in values:
|
|
|
|
value_constraint = constraint_class()
|
2014-01-18 17:09:43 +08:00
|
|
|
for source, target, val in zip(sources, targets, value):
|
|
|
|
lookup_class = target.get_lookup('exact')
|
2014-12-01 15:28:01 +08:00
|
|
|
lookup = lookup_class(target.get_col(alias, source), val)
|
2014-01-18 17:09:43 +08:00
|
|
|
value_constraint.add(lookup, AND)
|
2013-03-25 00:40:40 +08:00
|
|
|
root_constraint.add(value_constraint, OR)
|
|
|
|
else:
|
|
|
|
raise TypeError('Related Field got invalid lookup: %s' % lookup_type)
|
|
|
|
return root_constraint
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attnames(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(field.attname for field in self.local_related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def get_defaults(self):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(field.get_default() for field in self.local_related_fields)
|
2013-03-25 00:40:40 +08:00
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name, virtual_only=False):
|
|
|
|
super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
|
|
|
|
setattr(cls, self.name, ReverseSingleRelatedObjectDescriptor(self))
|
|
|
|
|
|
|
|
def contribute_to_related_class(self, cls, related):
|
|
|
|
# Internal FK's - i.e., those with a related name ending with '+' -
|
|
|
|
# and swapped models don't get a related descriptor.
|
2015-01-07 08:16:35 +08:00
|
|
|
if not self.rel.is_hidden() and not related.related_model._meta.swapped:
|
2013-08-20 13:52:32 +08:00
|
|
|
setattr(cls, related.get_accessor_name(), self.related_accessor_class(related))
|
2014-02-02 03:23:31 +08:00
|
|
|
# While 'limit_choices_to' might be a callable, simply pass
|
|
|
|
# it along for later - this is too early because it's still
|
|
|
|
# model load time.
|
2013-03-25 00:40:40 +08:00
|
|
|
if self.rel.limit_choices_to:
|
|
|
|
cls._meta.related_fkey_lookups.append(self.rel.limit_choices_to)
|
|
|
|
|
|
|
|
|
|
|
|
class ForeignKey(ForeignObject):
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
many_to_many = False
|
|
|
|
many_to_one = False
|
|
|
|
one_to_many = True
|
|
|
|
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-01-27 22:40:01 +08:00
|
|
|
def __init__(self, to, to_field=None, related_name=None, related_query_name=None,
|
|
|
|
limit_choices_to=None, parent_link=False, on_delete=CASCADE,
|
|
|
|
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-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__(
|
|
|
|
to, from_fields=['self'], to_fields=[to_field], **kwargs)
|
|
|
|
|
|
|
|
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):
|
|
|
|
on_delete = getattr(self.rel, 'on_delete', None)
|
|
|
|
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.
|
2014-06-24 11:39:17 +08:00
|
|
|
to_meta = getattr(self.rel.to, "_meta", None)
|
|
|
|
if self.rel.field_name and (not to_meta or (to_meta.pk and self.rel.field_name != to_meta.pk.name)):
|
2013-05-18 19:48:57 +08:00
|
|
|
kwargs['to_field'] = self.rel.field_name
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2013-03-25 00:40:40 +08:00
|
|
|
@property
|
|
|
|
def related_field(self):
|
|
|
|
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
|
2013-03-25 00:40:40 +08:00
|
|
|
from_opts = self.rel.to._meta
|
|
|
|
pathinfos = [PathInfo(from_opts, opts, (opts.pk,), self.rel, not self.unique, False)]
|
|
|
|
return pathinfos
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
def validate(self, value, model_instance):
|
|
|
|
if self.rel.parent_link:
|
|
|
|
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)
|
|
|
|
qs = self.rel.to._default_manager.using(using).filter(
|
2013-10-18 06:27:45 +08:00
|
|
|
**{self.rel.field_name: value}
|
|
|
|
)
|
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={
|
|
|
|
'model': self.rel.to._meta.verbose_name, 'pk': value,
|
|
|
|
'field': self.rel.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()
|
|
|
|
if isinstance(field_default, self.rel.to):
|
2013-03-25 00:40:40 +08:00
|
|
|
return getattr(field_default, self.related_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
|
|
|
|
(not self.related_field.empty_strings_allowed or
|
|
|
|
connection.features.interprets_empty_strings_as_nulls)):
|
2006-05-02 09:31:56 +08:00
|
|
|
return None
|
|
|
|
else:
|
2013-09-30 03:56:04 +08:00
|
|
|
return self.related_field.get_db_prep_save(value, connection=connection)
|
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):
|
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)
|
2009-11-03 22:02:49 +08:00
|
|
|
if self.rel.field_name is None:
|
|
|
|
self.rel.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)
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(self.rel.to, 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" %
|
|
|
|
(self.name, self.rel.to))
|
2008-09-02 06:43:38 +08:00
|
|
|
defaults = {
|
|
|
|
'form_class': forms.ModelChoiceField,
|
2014-02-02 03:23:31 +08:00
|
|
|
'queryset': self.rel.to._default_manager.using(db),
|
2008-09-02 06:43:38 +08:00
|
|
|
'to_field_name': self.rel.field_name,
|
|
|
|
}
|
2007-01-22 14:32:14 +08:00
|
|
|
defaults.update(kwargs)
|
2007-04-28 21:55:24 +08:00
|
|
|
return super(ForeignKey, self).formfield(**defaults)
|
2006-12-27 13:23:21 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def db_type(self, connection):
|
2007-07-20 14:28:56 +08:00
|
|
|
# The database column type of a ForeignKey is the column type
|
|
|
|
# of the field to which it points. An exception is if the ForeignKey
|
|
|
|
# points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField,
|
|
|
|
# in which case the column type is simply that of an IntegerField.
|
2008-09-01 08:49:03 +08:00
|
|
|
# If the database needs similar types for key fields however, the only
|
|
|
|
# thing we can do is making AutoField an IntegerField.
|
2013-03-25 00:40:40 +08:00
|
|
|
rel_field = self.related_field
|
2008-09-01 08:49:03 +08:00
|
|
|
if (isinstance(rel_field, AutoField) or
|
|
|
|
(not connection.features.related_fields_match_type and
|
|
|
|
isinstance(rel_field, (PositiveIntegerField,
|
|
|
|
PositiveSmallIntegerField)))):
|
2009-12-22 23:18:51 +08:00
|
|
|
return IntegerField().db_type(connection=connection)
|
|
|
|
return rel_field.db_type(connection=connection)
|
2007-07-20 14:28:56 +08:00
|
|
|
|
2012-09-08 03:40:59 +08:00
|
|
|
def db_parameters(self, connection):
|
|
|
|
return {"type": self.db_type(connection), "check": []}
|
|
|
|
|
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
|
|
|
|
that always carries a "unique" constraint with it and the reverse relation
|
|
|
|
always returns the object pointed to (since there will only ever be one),
|
2009-12-17 02:13:34 +08:00
|
|
|
rather than returning a list.
|
|
|
|
"""
|
2015-01-07 08:16:35 +08:00
|
|
|
# Field flags
|
|
|
|
many_to_many = False
|
|
|
|
many_to_one = False
|
|
|
|
one_to_many = False
|
|
|
|
one_to_one = True
|
|
|
|
|
2013-08-20 13:52:32 +08:00
|
|
|
related_accessor_class = SingleRelatedObjectDescriptor
|
2015-01-27 22:40:01 +08:00
|
|
|
rel_class = OneToOneRel
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
description = _("One-to-one relationship")
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __init__(self, to, 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-01-27 22:40:01 +08:00
|
|
|
super(OneToOneField, self).__init__(to, 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):
|
|
|
|
if self.rel.parent_link:
|
|
|
|
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):
|
|
|
|
if isinstance(data, self.rel.to):
|
|
|
|
setattr(instance, self.name, data)
|
|
|
|
else:
|
|
|
|
setattr(instance, self.attname, data)
|
|
|
|
|
2014-09-09 01:38:07 +08:00
|
|
|
def _check_unique(self, **kwargs):
|
|
|
|
# override ForeignKey since check isn't applicable here
|
|
|
|
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
|
|
|
|
managed = True
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(field.rel.to, six.string_types) and field.rel.to != RECURSIVE_RELATIONSHIP_CONSTANT:
|
2009-11-03 22:02:49 +08:00
|
|
|
to_model = field.rel.to
|
2010-01-14 22:55:36 +08:00
|
|
|
to = to_model.split('.')[-1]
|
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 set_managed(field, model, cls):
|
|
|
|
field.rel.through._meta.managed = model._meta.managed or cls._meta.managed
|
|
|
|
add_lazy_relation(klass, field, to_model, set_managed)
|
2012-07-20 20:22:00 +08:00
|
|
|
elif isinstance(field.rel.to, six.string_types):
|
2009-11-03 22:02:49 +08:00
|
|
|
to = klass._meta.object_name
|
|
|
|
to_model = klass
|
|
|
|
managed = klass._meta.managed
|
|
|
|
else:
|
|
|
|
to = field.rel.to._meta.object_name
|
|
|
|
to_model = field.rel.to
|
|
|
|
managed = klass._meta.managed or to_model._meta.managed
|
|
|
|
name = '%s_%s' % (klass._meta.object_name, field.name)
|
2010-02-22 22:04:13 +08:00
|
|
|
if field.rel.to == RECURSIVE_RELATIONSHIP_CONSTANT or to == klass._meta.object_name:
|
2009-11-03 22:02:49 +08:00
|
|
|
from_ = 'from_%s' % to.lower()
|
2009-11-04 07:34:58 +08:00
|
|
|
to = 'to_%s' % to.lower()
|
2009-11-03 22:02:49 +08:00
|
|
|
else:
|
2013-02-05 17:16:07 +08:00
|
|
|
from_ = klass._meta.model_name
|
2009-11-03 22:02:49 +08:00
|
|
|
to = to.lower()
|
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),
|
|
|
|
'managed': managed,
|
|
|
|
'auto_created': klass,
|
2009-11-20 08:59:38 +08:00
|
|
|
'app_label': klass._meta.app_label,
|
2011-10-15 05:49:43 +08:00
|
|
|
'db_tablespace': klass._meta.db_tablespace,
|
2010-04-27 20:35:49 +08:00
|
|
|
'unique_together': (from_, to),
|
2010-04-27 22:42:55 +08:00
|
|
|
'verbose_name': '%(from)s-%(to)s relationship' % {'from': from_, 'to': to},
|
|
|
|
'verbose_name_plural': '%(from)s-%(to)s relationships' % {'from': from_, 'to': to},
|
2013-12-24 19:25:17 +08:00
|
|
|
'apps': field.model._meta.apps,
|
2009-11-03 22:02:49 +08:00
|
|
|
})
|
2009-11-05 19:57:28 +08:00
|
|
|
# Construct and return the new class.
|
2013-04-06 01:59:15 +08:00
|
|
|
return type(str(name), (models.Model,), {
|
2009-11-03 22:02:49 +08:00
|
|
|
'Meta': meta,
|
2009-11-20 08:59:38 +08:00
|
|
|
'__module__': klass.__module__,
|
2014-09-04 20:15:09 +08:00
|
|
|
from_: models.ForeignKey(
|
|
|
|
klass,
|
|
|
|
related_name='%s+' % name,
|
|
|
|
db_tablespace=field.db_tablespace,
|
|
|
|
db_constraint=field.rel.db_constraint,
|
|
|
|
),
|
|
|
|
to: models.ForeignKey(
|
|
|
|
to_model,
|
|
|
|
related_name='%s+' % name,
|
|
|
|
db_tablespace=field.db_tablespace,
|
|
|
|
db_constraint=field.rel.db_constraint,
|
|
|
|
)
|
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-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,
|
|
|
|
)
|
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 = []
|
|
|
|
|
|
|
|
if self.null:
|
|
|
|
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):
|
2014-02-20 02:01:55 +08:00
|
|
|
if hasattr(self.rel.through, '_meta'):
|
|
|
|
qualified_model_name = "%s.%s" % (
|
|
|
|
self.rel.through._meta.app_label, self.rel.through.__name__)
|
|
|
|
else:
|
|
|
|
qualified_model_name = self.rel.through
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = []
|
|
|
|
|
|
|
|
if self.rel.through not in apps.get_models(include_auto_created=True):
|
|
|
|
# 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
|
|
|
|
to_model = self.rel.to
|
|
|
|
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
|
|
|
|
relationship_model_name = self.rel.through._meta.object_name
|
|
|
|
self_referential = from_model == to_model
|
|
|
|
|
|
|
|
# Check symmetrical attribute.
|
|
|
|
if (self_referential and self.rel.symmetrical and
|
|
|
|
not self.rel.through._meta.auto_created):
|
|
|
|
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:
|
|
|
|
seen_self = sum(from_model == getattr(field.rel, 'to', None)
|
|
|
|
for field in self.rel.through._meta.fields)
|
|
|
|
|
2014-02-20 02:01:55 +08:00
|
|
|
if seen_self > 2 and not self.rel.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."),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=self.rel.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
|
|
|
|
seen_from = sum(from_model == getattr(field.rel, 'to', None)
|
|
|
|
for field in self.rel.through._meta.fields)
|
|
|
|
seen_to = sum(to_model == getattr(field.rel, 'to', None)
|
|
|
|
for field in self.rel.through._meta.fields)
|
|
|
|
|
2014-02-20 02:01:55 +08:00
|
|
|
if seen_from > 1 and not self.rel.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
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-02-20 02:01:55 +08:00
|
|
|
if seen_to > 1 and not self.rel.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,
|
|
|
|
obj=self.rel.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
|
|
|
|
|
|
|
# Validate `through_fields`
|
|
|
|
if self.rel.through_fields is not None:
|
|
|
|
# Validate that we're given an iterable of at least two items
|
|
|
|
# and that none of them is "falsy"
|
|
|
|
if not (len(self.rel.through_fields) >= 2 and
|
|
|
|
self.rel.through_fields[0] and self.rel.through_fields[1]):
|
|
|
|
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
|
|
|
|
# expected models
|
|
|
|
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
|
|
|
|
|
|
|
source, through, target = from_model, self.rel.through, self.rel.to
|
|
|
|
source_field_name, target_field_name = self.rel.through_fields[:2]
|
|
|
|
|
|
|
|
for field_name, related_model in ((source_field_name, source),
|
|
|
|
(target_field_name, target)):
|
|
|
|
|
|
|
|
possible_field_names = []
|
|
|
|
for f in through._meta.fields:
|
|
|
|
if hasattr(f, 'rel') and getattr(f.rel, 'to', None) == related_model:
|
|
|
|
possible_field_names.append(f.name)
|
|
|
|
if possible_field_names:
|
|
|
|
hint = ("Did you mean one of the following foreign "
|
|
|
|
"keys to '%s': %s?") % (related_model._meta.object_name,
|
|
|
|
', '.join(possible_field_names))
|
|
|
|
else:
|
|
|
|
hint = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
field = through._meta.get_field(field_name)
|
|
|
|
except FieldDoesNotExist:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
("The intermediary model '%s' has no field '%s'.") % (
|
|
|
|
qualified_model_name, field_name),
|
|
|
|
hint=hint,
|
|
|
|
obj=self,
|
|
|
|
id='fields.E338',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
if not (hasattr(field, 'rel') and
|
|
|
|
getattr(field.rel, 'to', None) == related_model):
|
|
|
|
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()
|
|
|
|
# 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
|
2013-05-18 19:48:57 +08:00
|
|
|
if self.rel.db_constraint is not True:
|
2013-12-06 22:01:36 +08:00
|
|
|
kwargs['db_constraint'] = self.rel.db_constraint
|
2014-08-15 10:47:02 +08:00
|
|
|
if self.rel.related_name is not None:
|
2014-12-12 03:36:13 +08:00
|
|
|
kwargs['related_name'] = self.rel.related_name
|
2014-08-15 10:47:02 +08:00
|
|
|
if self.rel.related_query_name is not None:
|
|
|
|
kwargs['related_query_name'] = self.rel.related_query_name
|
2013-05-18 19:48:57 +08:00
|
|
|
# Rel needs more work.
|
2013-06-29 00:53:49 +08:00
|
|
|
if isinstance(self.rel.to, six.string_types):
|
2013-05-18 19:48:57 +08:00
|
|
|
kwargs['to'] = self.rel.to
|
|
|
|
else:
|
|
|
|
kwargs['to'] = "%s.%s" % (self.rel.to._meta.app_label, self.rel.to._meta.object_name)
|
2014-03-09 07:57:25 +08:00
|
|
|
if getattr(self.rel, 'through', None) is not None:
|
|
|
|
if isinstance(self.rel.through, six.string_types):
|
|
|
|
kwargs['through'] = self.rel.through
|
2014-03-09 10:35:35 +08:00
|
|
|
elif not self.rel.through._meta.auto_created:
|
2014-03-09 07:57:25 +08:00
|
|
|
kwargs['through'] = "%s.%s" % (self.rel.through._meta.app_label, self.rel.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:
|
|
|
|
# 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 ManyToManyField 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-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 = []
|
|
|
|
int_model = self.rel.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):
|
|
|
|
"Function that can be curried to provide the m2m table name for this relation"
|
2008-07-29 20:41:08 +08:00
|
|
|
if self.rel.through is not None:
|
2009-11-03 22:02:49 +08:00
|
|
|
return self.rel.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):
|
2010-02-22 22:04:13 +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)
|
2014-02-20 02:01:55 +08:00
|
|
|
if self.rel.through_fields is not None:
|
|
|
|
link_field_name = self.rel.through_fields[0]
|
|
|
|
else:
|
|
|
|
link_field_name = None
|
2009-11-03 22:02:49 +08:00
|
|
|
for f in self.rel.through._meta.fields:
|
2015-01-07 08:16:35 +08:00
|
|
|
if (f.is_relation and f.rel.to == related.related_model and
|
|
|
|
(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):
|
2010-02-22 22:04:13 +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
|
2014-02-20 02:01:55 +08:00
|
|
|
if self.rel.through_fields is not None:
|
|
|
|
link_field_name = self.rel.through_fields[1]
|
|
|
|
else:
|
|
|
|
link_field_name = None
|
2009-11-03 22:02:49 +08:00
|
|
|
for f in self.rel.through._meta.fields:
|
2015-01-07 08:16:35 +08:00
|
|
|
# NOTE f.rel.to != f.related_model
|
|
|
|
if f.is_relation and f.rel.to == related.model:
|
|
|
|
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.
|
2009-11-03 22:02:49 +08:00
|
|
|
if self.rel.symmetrical and (self.rel.to == "self" or self.rel.to == cls._meta.object_name):
|
2008-08-30 05:24:00 +08:00
|
|
|
self.rel.related_name = "%s_rel_+" % name
|
|
|
|
|
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.
|
|
|
|
if not self.rel.through and not cls._meta.abstract and not cls._meta.swapped:
|
2009-11-03 22:02:49 +08:00
|
|
|
self.rel.through = create_many_to_many_intermediary_model(self, cls)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Add the descriptor for the m2m relation
|
|
|
|
setattr(cls, self.name, ReverseManyRelatedObjectsDescriptor(self))
|
|
|
|
|
|
|
|
# Set up the accessor for the m2m table name for the relation
|
|
|
|
self.m2m_db_table = curry(self._get_m2m_db_table, cls._meta)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2008-07-29 20:41:08 +08:00
|
|
|
# Populate some necessary rel arguments so that cross-app relations
|
|
|
|
# work correctly.
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(self.rel.through, six.string_types):
|
2008-07-29 20:41:08 +08:00
|
|
|
def resolve_through_model(field, model, cls):
|
2009-11-03 22:02:49 +08:00
|
|
|
field.rel.through = model
|
2008-07-29 20:41:08 +08:00
|
|
|
add_lazy_relation(cls, self, self.rel.through, resolve_through_model)
|
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-01-07 08:16:35 +08:00
|
|
|
if not self.rel.is_hidden() and not related.related_model._meta.swapped:
|
2006-05-02 09:31:56 +08:00
|
|
|
setattr(cls, related.get_accessor_name(), ManyRelatedObjectsDescriptor(related))
|
|
|
|
|
|
|
|
# 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
|
|
|
|
2011-01-27 03:10:08 +08:00
|
|
|
get_m2m_rel = curry(self._get_m2m_attr, related, 'rel')
|
|
|
|
self.m2m_target_field_name = lambda: get_m2m_rel().field_name
|
|
|
|
get_m2m_reverse_rel = curry(self._get_m2m_reverse_attr, related, 'rel')
|
|
|
|
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):
|
|
|
|
"Returns the value of this field in the given model instance."
|
|
|
|
return getattr(obj, self.attname).all()
|
|
|
|
|
2007-08-06 21:58:56 +08:00
|
|
|
def save_form_data(self, instance, data):
|
|
|
|
setattr(instance, self.attname, data)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2007-01-22 14:32:14 +08:00
|
|
|
def formfield(self, **kwargs):
|
2009-12-22 23:18:51 +08:00
|
|
|
db = kwargs.pop('using', None)
|
|
|
|
defaults = {
|
|
|
|
'form_class': forms.ModelMultipleChoiceField,
|
2014-02-02 03:23:31 +08:00
|
|
|
'queryset': self.rel.to._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}
|