2011-03-28 10:11:19 +08:00
|
|
|
import copy
|
2014-05-16 20:25:45 +08:00
|
|
|
import inspect
|
2014-01-11 06:06:19 +08:00
|
|
|
import warnings
|
2017-09-07 01:11:18 +08:00
|
|
|
from functools import partialmethod
|
2015-01-28 20:35:27 +08:00
|
|
|
from itertools import chain
|
2011-01-26 11:42:31 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.conf import settings
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.core import checks
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.exceptions import (
|
|
|
|
NON_FIELD_ERRORS, FieldDoesNotExist, FieldError, MultipleObjectsReturned,
|
|
|
|
ObjectDoesNotExist, ValidationError,
|
|
|
|
)
|
|
|
|
from django.db import (
|
2016-05-18 22:30:42 +08:00
|
|
|
DEFAULT_DB_ALIAS, DJANGO_VERSION_PICKLE_KEY, DatabaseError, connection,
|
|
|
|
connections, router, transaction,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2019-08-30 16:28:18 +08:00
|
|
|
from django.db.models import (
|
|
|
|
NOT_PROVIDED, ExpressionWrapper, IntegerField, Max, Value,
|
|
|
|
)
|
2014-07-05 14:03:52 +08:00
|
|
|
from django.db.models.constants import LOOKUP_SEP
|
2018-08-06 10:30:44 +08:00
|
|
|
from django.db.models.constraints import CheckConstraint, UniqueConstraint
|
2015-07-22 22:43:21 +08:00
|
|
|
from django.db.models.deletion import CASCADE, Collector
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.fields.related import (
|
2016-11-21 08:39:32 +08:00
|
|
|
ForeignObjectRel, OneToOneField, lazy_related_operation, resolve_relation,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2019-08-30 16:28:18 +08:00
|
|
|
from django.db.models.functions import Coalesce
|
2016-04-17 19:55:55 +08:00
|
|
|
from django.db.models.manager import Manager
|
2014-04-06 02:58:59 +08:00
|
|
|
from django.db.models.options import Options
|
2010-11-10 00:46:42 +08:00
|
|
|
from django.db.models.query import Q
|
2016-12-16 02:42:44 +08:00
|
|
|
from django.db.models.signals import (
|
|
|
|
class_prepared, post_init, post_save, pre_init, pre_save,
|
|
|
|
)
|
2015-03-03 16:43:56 +08:00
|
|
|
from django.db.models.utils import make_model_tuple
|
2019-03-22 20:21:00 +08:00
|
|
|
from django.utils.encoding import force_str
|
2019-11-07 22:35:33 +08:00
|
|
|
from django.utils.hashable import make_hashable
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.text import capfirst, get_text_list
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-06-06 19:10:20 +08:00
|
|
|
from django.utils.version import get_version
|
2011-07-13 17:35:51 +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
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Deferred:
|
2016-02-02 17:33:09 +08:00
|
|
|
def __repr__(self):
|
2017-01-20 17:20:53 +08:00
|
|
|
return '<Deferred field>'
|
2016-02-02 17:33:09 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-20 17:20:53 +08:00
|
|
|
return '<Deferred field>'
|
2016-02-02 17:33:09 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2016-02-02 17:33:09 +08:00
|
|
|
DEFERRED = Deferred()
|
|
|
|
|
|
|
|
|
2017-10-13 22:29:34 +08:00
|
|
|
def subclass_exception(name, bases, module, attached_to):
|
2012-07-21 16:01:46 +08:00
|
|
|
"""
|
|
|
|
Create exception subclass. Used by ModelBase below.
|
|
|
|
|
2017-10-13 22:29:34 +08:00
|
|
|
The exception is created in a way that allows it to be pickled, assuming
|
|
|
|
that the returned exception class will be added as an attribute to the
|
|
|
|
'attached_to' class.
|
2012-07-21 16:01:46 +08:00
|
|
|
"""
|
2017-10-13 22:29:34 +08:00
|
|
|
return type(name, bases, {
|
|
|
|
'__module__': module,
|
|
|
|
'__qualname__': '%s.%s' % (attached_to.__qualname__, name),
|
|
|
|
})
|
2012-07-21 16:01:46 +08:00
|
|
|
|
|
|
|
|
2018-12-23 07:11:24 +08:00
|
|
|
def _has_contribute_to_class(value):
|
|
|
|
# Only call contribute_to_class() if it's bound.
|
|
|
|
return not inspect.isclass(value) and hasattr(value, 'contribute_to_class')
|
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ModelBase(type):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Metaclass for all models."""
|
2017-10-14 09:29:00 +08:00
|
|
|
def __new__(cls, name, bases, attrs, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super_new = super().__new__
|
2013-01-29 13:28:09 +08:00
|
|
|
|
|
|
|
# Also ensure initialization is only performed for subclasses of Model
|
|
|
|
# (excluding Model class itself).
|
2014-04-29 12:44:04 +08:00
|
|
|
parents = [b for b in bases if isinstance(b, ModelBase)]
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if not parents:
|
2008-07-06 19:55:30 +08:00
|
|
|
return super_new(cls, name, bases, attrs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Create the class.
|
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
|
|
|
module = attrs.pop('__module__')
|
2016-12-06 08:37:23 +08:00
|
|
|
new_attrs = {'__module__': module}
|
|
|
|
classcell = attrs.pop('__classcell__', None)
|
|
|
|
if classcell is not None:
|
|
|
|
new_attrs['__classcell__'] = classcell
|
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
|
|
|
attr_meta = attrs.pop('Meta', None)
|
2018-12-23 07:11:24 +08:00
|
|
|
# Pass all attrs without a (Django-specific) contribute_to_class()
|
|
|
|
# method to type.__new__() so that they're properly initialized
|
|
|
|
# (i.e. __set_name__()).
|
2019-03-15 01:23:50 +08:00
|
|
|
contributable_attrs = {}
|
2018-12-23 07:11:24 +08:00
|
|
|
for obj_name, obj in list(attrs.items()):
|
2019-03-15 01:23:50 +08:00
|
|
|
if _has_contribute_to_class(obj):
|
|
|
|
contributable_attrs[obj_name] = obj
|
|
|
|
else:
|
|
|
|
new_attrs[obj_name] = obj
|
2018-12-23 07:11:24 +08:00
|
|
|
new_class = super_new(cls, name, bases, new_attrs, **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
|
|
|
abstract = getattr(attr_meta, 'abstract', False)
|
2018-01-31 23:21:41 +08:00
|
|
|
meta = attr_meta or getattr(new_class, 'Meta', None)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
base_meta = getattr(new_class, '_meta', None)
|
|
|
|
|
2015-02-10 05:41:57 +08:00
|
|
|
app_label = None
|
|
|
|
|
2013-12-31 23:23:42 +08:00
|
|
|
# Look for an application configuration to attach the model to.
|
|
|
|
app_config = apps.get_containing_app_config(module)
|
|
|
|
|
2008-06-29 10:35:08 +08:00
|
|
|
if getattr(meta, 'app_label', None) is None:
|
2013-12-31 23:23:42 +08:00
|
|
|
if app_config is None:
|
2014-11-20 06:56:04 +08:00
|
|
|
if not abstract:
|
2015-02-10 05:41:57 +08:00
|
|
|
raise RuntimeError(
|
|
|
|
"Model class %s.%s doesn't declare an explicit "
|
2015-12-25 02:28:32 +08:00
|
|
|
"app_label and isn't in an application in "
|
|
|
|
"INSTALLED_APPS." % (module, name)
|
|
|
|
)
|
2013-12-31 23:23:42 +08:00
|
|
|
|
|
|
|
else:
|
2015-02-10 05:41:57 +08:00
|
|
|
app_label = app_config.label
|
2008-06-29 10:35:08 +08:00
|
|
|
|
2015-02-10 05:41:57 +08:00
|
|
|
new_class.add_to_class('_meta', Options(meta, app_label))
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if not abstract:
|
2013-10-20 07:33:10 +08:00
|
|
|
new_class.add_to_class(
|
|
|
|
'DoesNotExist',
|
|
|
|
subclass_exception(
|
2017-01-20 17:20:53 +08:00
|
|
|
'DoesNotExist',
|
2014-09-04 20:15:09 +08:00
|
|
|
tuple(
|
|
|
|
x.DoesNotExist for x in parents if hasattr(x, '_meta') and not x._meta.abstract
|
|
|
|
) or (ObjectDoesNotExist,),
|
2013-10-20 07:33:10 +08:00
|
|
|
module,
|
|
|
|
attached_to=new_class))
|
|
|
|
new_class.add_to_class(
|
|
|
|
'MultipleObjectsReturned',
|
|
|
|
subclass_exception(
|
2017-01-20 17:20:53 +08:00
|
|
|
'MultipleObjectsReturned',
|
2014-09-04 20:15:09 +08:00
|
|
|
tuple(
|
|
|
|
x.MultipleObjectsReturned for x in parents if hasattr(x, '_meta') and not x._meta.abstract
|
|
|
|
) or (MultipleObjectsReturned,),
|
2013-10-20 07:33:10 +08:00
|
|
|
module,
|
|
|
|
attached_to=new_class))
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if base_meta and not base_meta.abstract:
|
|
|
|
# Non-abstract child classes inherit some attributes from their
|
|
|
|
# non-abstract parent (unless an ABC comes before it in the
|
|
|
|
# method resolution order).
|
|
|
|
if not hasattr(meta, 'ordering'):
|
|
|
|
new_class._meta.ordering = base_meta.ordering
|
|
|
|
if not hasattr(meta, 'get_latest_by'):
|
|
|
|
new_class._meta.get_latest_by = base_meta.get_latest_by
|
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
is_proxy = new_class._meta.proxy
|
|
|
|
|
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
|
|
|
# If the model is a proxy, ensure that the base class
|
|
|
|
# hasn't been swapped out.
|
|
|
|
if is_proxy and base_meta and base_meta.swapped:
|
|
|
|
raise TypeError("%s cannot proxy the swapped model '%s'." % (name, base_meta.swapped))
|
|
|
|
|
2019-03-15 01:23:50 +08:00
|
|
|
# Add remaining attributes (those with a contribute_to_class() method)
|
|
|
|
# to the class.
|
|
|
|
for obj_name, obj in contributable_attrs.items():
|
2006-05-02 09:31:56 +08:00
|
|
|
new_class.add_to_class(obj_name, obj)
|
|
|
|
|
2009-03-18 17:47:08 +08:00
|
|
|
# All the fields of any type declared on this model
|
2015-01-07 08:16:35 +08:00
|
|
|
new_fields = chain(
|
|
|
|
new_class._meta.local_fields,
|
|
|
|
new_class._meta.local_many_to_many,
|
2016-03-21 01:10:55 +08:00
|
|
|
new_class._meta.private_fields
|
2013-12-13 04:23:24 +08:00
|
|
|
)
|
2015-01-07 08:16:35 +08:00
|
|
|
field_names = {f.name for f in new_fields}
|
2009-03-18 17:47:08 +08:00
|
|
|
|
|
|
|
# Basic setup for proxy models.
|
|
|
|
if is_proxy:
|
|
|
|
base = None
|
2013-10-11 19:25:14 +08:00
|
|
|
for parent in [kls for kls in parents if hasattr(kls, '_meta')]:
|
2009-03-18 17:47:08 +08:00
|
|
|
if parent._meta.abstract:
|
|
|
|
if parent._meta.fields:
|
2014-09-04 20:15:09 +08:00
|
|
|
raise TypeError(
|
|
|
|
"Abstract base class containing model fields not "
|
|
|
|
"permitted for proxy model '%s'." % name
|
|
|
|
)
|
2009-03-18 17:47:08 +08:00
|
|
|
else:
|
|
|
|
continue
|
2016-03-28 03:20:54 +08:00
|
|
|
if base is None:
|
2009-03-18 17:47:08 +08:00
|
|
|
base = parent
|
2016-03-28 03:20:54 +08:00
|
|
|
elif parent._meta.concrete_model is not base._meta.concrete_model:
|
|
|
|
raise TypeError("Proxy model '%s' has more than one non-abstract model base class." % name)
|
2009-03-18 17:47:08 +08:00
|
|
|
if base is None:
|
2013-04-05 06:16:16 +08:00
|
|
|
raise TypeError("Proxy model '%s' has no non-abstract model base class." % name)
|
2009-03-18 17:47:08 +08:00
|
|
|
new_class._meta.setup_proxy(base)
|
2012-02-22 13:26:50 +08:00
|
|
|
new_class._meta.concrete_model = base._meta.concrete_model
|
|
|
|
else:
|
|
|
|
new_class._meta.concrete_model = new_class
|
2009-03-18 17:47:08 +08:00
|
|
|
|
2013-08-12 19:30:38 +08:00
|
|
|
# Collect the parent links for multi-table inheritance.
|
|
|
|
parent_links = {}
|
|
|
|
for base in reversed([new_class] + parents):
|
|
|
|
# Conceptually equivalent to `if base is Model`.
|
|
|
|
if not hasattr(base, '_meta'):
|
|
|
|
continue
|
|
|
|
# Skip concrete parent classes.
|
|
|
|
if base != new_class and not base._meta.abstract:
|
|
|
|
continue
|
|
|
|
# Locate OneToOneField instances.
|
|
|
|
for field in base._meta.local_fields:
|
2020-01-16 15:06:16 +08:00
|
|
|
if isinstance(field, OneToOneField) and field.remote_field.parent_link:
|
2015-03-03 16:43:56 +08:00
|
|
|
related = resolve_relation(new_class, field.remote_field.model)
|
|
|
|
parent_links[make_model_tuple(related)] = field
|
2015-08-03 20:03:41 +08:00
|
|
|
|
|
|
|
# Track fields inherited from base models.
|
|
|
|
inherited_attributes = set()
|
2013-08-12 19:30:38 +08:00
|
|
|
# Do the appropriate setup for any model parents.
|
2015-08-03 20:03:41 +08:00
|
|
|
for base in new_class.mro():
|
|
|
|
if base not in parents or not hasattr(base, '_meta'):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# Things without _meta aren't functional models, so they're
|
|
|
|
# uninteresting parents.
|
2017-04-27 20:16:19 +08:00
|
|
|
inherited_attributes.update(base.__dict__)
|
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
|
|
|
continue
|
2008-09-03 13:53:50 +08:00
|
|
|
|
2009-03-04 18:39:29 +08:00
|
|
|
parent_fields = base._meta.local_fields + base._meta.local_many_to_many
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if not base._meta.abstract:
|
2015-08-03 20:03:41 +08:00
|
|
|
# Check for clashes between locally declared fields and those
|
|
|
|
# on the base classes.
|
|
|
|
for field in parent_fields:
|
|
|
|
if field.name in field_names:
|
|
|
|
raise FieldError(
|
|
|
|
'Local field %r in class %r clashes with field of '
|
|
|
|
'the same name from base class %r.' % (
|
|
|
|
field.name,
|
|
|
|
name,
|
|
|
|
base.__name__,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
inherited_attributes.add(field.name)
|
|
|
|
|
2008-09-04 02:38:43 +08:00
|
|
|
# Concrete classes...
|
2012-02-22 13:26:50 +08:00
|
|
|
base = base._meta.concrete_model
|
2015-03-03 16:43:56 +08:00
|
|
|
base_key = make_model_tuple(base)
|
|
|
|
if base_key in parent_links:
|
|
|
|
field = parent_links[base_key]
|
2009-03-18 17:47:08 +08:00
|
|
|
elif not is_proxy:
|
2013-02-05 17:16:07 +08:00
|
|
|
attr_name = '%s_ptr' % base._meta.model_name
|
2015-07-22 22:43:21 +08:00
|
|
|
field = OneToOneField(
|
|
|
|
base,
|
|
|
|
on_delete=CASCADE,
|
|
|
|
name=attr_name,
|
|
|
|
auto_created=True,
|
|
|
|
parent_link=True,
|
|
|
|
)
|
2015-08-03 20:03:41 +08:00
|
|
|
|
|
|
|
if attr_name in field_names:
|
|
|
|
raise FieldError(
|
|
|
|
"Auto-generated field '%s' in class %r for "
|
|
|
|
"parent_link to base class %r clashes with "
|
|
|
|
"declared field of the same name." % (
|
|
|
|
attr_name,
|
|
|
|
name,
|
|
|
|
base.__name__,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-01-20 02:35:49 +08:00
|
|
|
# Only add the ptr field if it's not already present;
|
|
|
|
# e.g. migrations will already have it specified
|
|
|
|
if not hasattr(new_class, attr_name):
|
|
|
|
new_class.add_to_class(attr_name, field)
|
2009-03-18 17:47:08 +08:00
|
|
|
else:
|
|
|
|
field = None
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
new_class._meta.parents[base] = field
|
|
|
|
else:
|
2016-03-29 02:14:24 +08:00
|
|
|
base_parents = base._meta.parents.copy()
|
|
|
|
|
2015-08-03 20:03:41 +08:00
|
|
|
# Add fields from abstract base class if it wasn't overridden.
|
2008-09-02 23:26:00 +08:00
|
|
|
for field in parent_fields:
|
2015-08-03 20:03:41 +08:00
|
|
|
if (field.name not in field_names and
|
|
|
|
field.name not in new_class.__dict__ and
|
|
|
|
field.name not in inherited_attributes):
|
|
|
|
new_field = copy.deepcopy(field)
|
|
|
|
new_class.add_to_class(field.name, new_field)
|
|
|
|
# Replace parent links defined on this base by the new
|
|
|
|
# field. It will be appropriately resolved if required.
|
|
|
|
if field.one_to_one:
|
|
|
|
for parent, parent_link in base_parents.items():
|
|
|
|
if field == parent_link:
|
|
|
|
base_parents[parent] = new_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
|
|
|
|
2008-09-04 02:38:43 +08:00
|
|
|
# Pass any non-abstract parent classes onto child.
|
2016-03-29 02:14:24 +08:00
|
|
|
new_class._meta.parents.update(base_parents)
|
2008-09-04 02:38:43 +08:00
|
|
|
|
2016-03-21 01:10:55 +08:00
|
|
|
# Inherit private fields (like GenericForeignKey) from the parent
|
2009-03-04 18:39:29 +08:00
|
|
|
# class
|
2016-03-21 01:10:55 +08:00
|
|
|
for field in base._meta.private_fields:
|
2015-08-03 20:03:41 +08:00
|
|
|
if field.name in field_names:
|
|
|
|
if not base._meta.abstract:
|
|
|
|
raise FieldError(
|
|
|
|
'Local field %r in class %r clashes with field of '
|
|
|
|
'the same name from base class %r.' % (
|
|
|
|
field.name,
|
|
|
|
name,
|
|
|
|
base.__name__,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2018-03-13 10:42:48 +08:00
|
|
|
field = copy.deepcopy(field)
|
2018-08-09 09:25:18 +08:00
|
|
|
if not base._meta.abstract:
|
|
|
|
field.mti_inherited = True
|
2018-03-13 10:42:48 +08:00
|
|
|
new_class.add_to_class(field.name, field)
|
2008-09-03 13:53:50 +08:00
|
|
|
|
2017-06-07 22:13:12 +08:00
|
|
|
# Copy indexes so that index names are unique when models extend an
|
|
|
|
# abstract model.
|
|
|
|
new_class._meta.indexes = [copy.deepcopy(idx) for idx in new_class._meta.indexes]
|
2016-06-20 23:50:05 +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
|
|
|
if abstract:
|
|
|
|
# Abstract base models can't be instantiated and don't appear in
|
|
|
|
# the list of models for an app. We do the final setup for them a
|
|
|
|
# little differently from normal models.
|
|
|
|
attr_meta.abstract = False
|
|
|
|
new_class.Meta = attr_meta
|
|
|
|
return new_class
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
new_class._prepare()
|
2013-12-24 19:25:17 +08:00
|
|
|
new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
|
2014-01-05 16:32:22 +08:00
|
|
|
return new_class
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-02-08 20:01:23 +08:00
|
|
|
def add_to_class(cls, name, value):
|
2018-12-23 07:11:24 +08:00
|
|
|
if _has_contribute_to_class(value):
|
2008-02-08 20:01:23 +08:00
|
|
|
value.contribute_to_class(cls, name)
|
|
|
|
else:
|
|
|
|
setattr(cls, name, value)
|
|
|
|
|
|
|
|
def _prepare(cls):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Create some methods once self._meta has been populated."""
|
2008-02-08 20:01:23 +08:00
|
|
|
opts = cls._meta
|
|
|
|
opts._prepare(cls)
|
|
|
|
|
|
|
|
if opts.order_with_respect_to:
|
2017-09-07 01:11:18 +08:00
|
|
|
cls.get_next_in_order = partialmethod(cls._get_next_or_previous_in_order, is_next=True)
|
|
|
|
cls.get_previous_in_order = partialmethod(cls._get_next_or_previous_in_order, is_next=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
|
|
|
|
2015-03-27 00:52:11 +08:00
|
|
|
# Defer creating accessors on the foreign class until it has been
|
|
|
|
# created and registered. If remote_field is None, we're ordering
|
|
|
|
# with respect to a GenericForeignKey and don't know what the
|
|
|
|
# foreign class is - we'll add those accessors later in
|
|
|
|
# contribute_to_class().
|
|
|
|
if opts.order_with_respect_to.remote_field:
|
|
|
|
wrt = opts.order_with_respect_to
|
|
|
|
remote = wrt.remote_field.model
|
|
|
|
lazy_related_operation(make_foreign_order_accessors, cls, remote)
|
2008-02-08 20:01:23 +08:00
|
|
|
|
|
|
|
# Give the class a docstring -- its definition.
|
|
|
|
if cls.__doc__ is None:
|
2015-01-07 08:16:35 +08:00
|
|
|
cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join(f.name for f in opts.fields))
|
2008-02-08 20:01:23 +08:00
|
|
|
|
2015-04-27 05:05:50 +08:00
|
|
|
get_absolute_url_override = settings.ABSOLUTE_URL_OVERRIDES.get(opts.label_lower)
|
2014-09-09 20:29:49 +08:00
|
|
|
if get_absolute_url_override:
|
|
|
|
setattr(cls, 'get_absolute_url', get_absolute_url_override)
|
2008-02-08 20:01:23 +08:00
|
|
|
|
2017-01-01 02:07:35 +08:00
|
|
|
if not opts.managers:
|
2016-04-17 19:55:55 +08:00
|
|
|
if any(f.name == 'objects' for f in opts.fields):
|
|
|
|
raise ValueError(
|
|
|
|
"Model %s must specify a custom Manager, because it has a "
|
|
|
|
"field named 'objects'." % cls.__name__
|
|
|
|
)
|
|
|
|
manager = Manager()
|
|
|
|
manager.auto_created = True
|
|
|
|
cls.add_to_class('objects', manager)
|
2016-02-19 03:27:55 +08:00
|
|
|
|
2017-09-17 14:26:18 +08:00
|
|
|
# Set the name of _meta.indexes. This can't be done in
|
|
|
|
# Options.contribute_to_class() because fields haven't been added to
|
|
|
|
# the model at that point.
|
|
|
|
for index in cls._meta.indexes:
|
|
|
|
if not index.name:
|
|
|
|
index.set_name_with_model(cls)
|
|
|
|
|
2016-12-16 02:42:44 +08:00
|
|
|
class_prepared.send(sender=cls)
|
2008-02-08 20:01:23 +08:00
|
|
|
|
2016-04-17 19:55:55 +08:00
|
|
|
@property
|
|
|
|
def _base_manager(cls):
|
|
|
|
return cls._meta.base_manager
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _default_manager(cls):
|
|
|
|
return cls._meta.default_manager
|
|
|
|
|
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
|
|
|
|
2017-08-04 14:20:15 +08:00
|
|
|
class ModelStateFieldsCacheDescriptor:
|
|
|
|
def __get__(self, instance, cls=None):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
|
|
|
res = instance.fields_cache = {}
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ModelState:
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Store model instance state."""
|
2017-08-04 14:20:15 +08:00
|
|
|
db = None
|
|
|
|
# If true, uniqueness validation checks will consider this a new, unsaved
|
|
|
|
# object. Necessary for correct validation of new instances of objects with
|
|
|
|
# explicit (non-auto) PKs. This impacts validation only; it has no effect
|
|
|
|
# on the actual save.
|
|
|
|
adding = True
|
|
|
|
fields_cache = ModelStateFieldsCacheDescriptor()
|
2009-12-22 23:18:51 +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
|
|
|
|
2017-01-07 19:11:46 +08:00
|
|
|
class Model(metaclass=ModelBase):
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-12-16 02:42:44 +08:00
|
|
|
# Alias some things as locals to avoid repeat global lookups
|
|
|
|
cls = self.__class__
|
|
|
|
opts = self._meta
|
|
|
|
_setattr = setattr
|
|
|
|
_DEFERRED = DEFERRED
|
|
|
|
|
|
|
|
pre_init.send(sender=cls, args=args, kwargs=kwargs)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2010-04-30 22:27:29 +08:00
|
|
|
# Set up the storage for instance state
|
2009-12-22 23:18:51 +08:00
|
|
|
self._state = ModelState()
|
|
|
|
|
2007-02-26 14:21:24 +08:00
|
|
|
# There is a rather weird disparity here; if kwargs, it's set, then args
|
2007-06-23 22:16:00 +08:00
|
|
|
# overrides it. It should be one or the other; don't duplicate the work
|
2007-02-26 14:21:24 +08:00
|
|
|
# The reason for the kwargs check is that standard iterator passes in by
|
2008-02-14 07:28:58 +08:00
|
|
|
# args, and instantiation for iteration is 33% faster.
|
2016-12-16 02:42:44 +08:00
|
|
|
if len(args) > len(opts.concrete_fields):
|
2007-02-26 14:21:24 +08:00
|
|
|
# Daft, but matches old exception sans the err msg.
|
|
|
|
raise IndexError("Number of args exceeds number of fields")
|
2007-02-26 14:16:19 +08:00
|
|
|
|
|
|
|
if not kwargs:
|
2016-12-16 02:42:44 +08:00
|
|
|
fields_iter = iter(opts.concrete_fields)
|
2012-05-07 23:25:12 +08:00
|
|
|
# The ordering of the zip calls matter - zip throws StopIteration
|
2007-02-26 14:21:24 +08:00
|
|
|
# when an iter throws it. So if the first iter throws it, the second
|
|
|
|
# is *not* consumed. We rely on this, so don't change the order
|
|
|
|
# without changing the logic.
|
2012-05-07 23:25:12 +08:00
|
|
|
for val, field in zip(args, fields_iter):
|
2016-12-16 02:42:44 +08:00
|
|
|
if val is _DEFERRED:
|
2016-02-02 17:33:09 +08:00
|
|
|
continue
|
2016-12-16 02:42:44 +08:00
|
|
|
_setattr(self, field.attname, val)
|
2007-02-26 14:16:19 +08:00
|
|
|
else:
|
2007-02-26 14:21:24 +08:00
|
|
|
# Slower, kwargs-ready version.
|
2016-12-16 02:42:44 +08:00
|
|
|
fields_iter = iter(opts.fields)
|
2012-05-07 23:25:12 +08:00
|
|
|
for val, field in zip(args, fields_iter):
|
2016-12-16 02:42:44 +08:00
|
|
|
if val is _DEFERRED:
|
2016-02-02 17:33:09 +08:00
|
|
|
continue
|
2016-12-16 02:42:44 +08:00
|
|
|
_setattr(self, field.attname, val)
|
2007-02-26 14:16:19 +08:00
|
|
|
kwargs.pop(field.name, None)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2007-02-26 14:21:24 +08:00
|
|
|
# Now we're left with the unprocessed fields that *must* come from
|
|
|
|
# keywords, or default.
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2007-02-26 14:16:19 +08:00
|
|
|
for field in fields_iter:
|
2009-03-06 12:51:05 +08:00
|
|
|
is_related_object = False
|
2016-02-02 17:33:09 +08:00
|
|
|
# Virtual field
|
|
|
|
if field.attname not in kwargs and field.column is None:
|
2009-03-22 09:45:22 +08:00
|
|
|
continue
|
2007-02-26 14:16:19 +08:00
|
|
|
if kwargs:
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(field.remote_field, ForeignObjectRel):
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
2007-02-26 14:16:19 +08:00
|
|
|
# Assume object instance was passed in.
|
|
|
|
rel_obj = kwargs.pop(field.name)
|
2009-03-06 12:51:05 +08:00
|
|
|
is_related_object = True
|
2006-05-02 09:31:56 +08:00
|
|
|
except KeyError:
|
|
|
|
try:
|
2007-02-26 14:16:19 +08:00
|
|
|
# Object instance wasn't passed in -- must be an ID.
|
|
|
|
val = kwargs.pop(field.attname)
|
|
|
|
except KeyError:
|
|
|
|
val = field.get_default()
|
|
|
|
else:
|
2009-10-29 22:32:01 +08:00
|
|
|
try:
|
|
|
|
val = kwargs.pop(field.attname)
|
|
|
|
except KeyError:
|
|
|
|
# This is done with an exception rather than the
|
|
|
|
# default argument on pop because we don't want
|
|
|
|
# get_default() to be evaluated, and then not used.
|
|
|
|
# Refs #12057.
|
|
|
|
val = field.get_default()
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2007-02-26 14:16:19 +08:00
|
|
|
val = field.get_default()
|
2013-03-25 00:40:40 +08:00
|
|
|
|
2009-03-06 12:51:05 +08:00
|
|
|
if is_related_object:
|
|
|
|
# If we are passed a related instance, set it using the
|
|
|
|
# field.name instead of field.attname (e.g. "user" instead of
|
|
|
|
# "user_id") so that the object gets properly cached (and type
|
|
|
|
# checked) by the RelatedObjectDescriptor.
|
2016-12-16 02:42:44 +08:00
|
|
|
if rel_obj is not _DEFERRED:
|
|
|
|
_setattr(self, field.name, rel_obj)
|
2008-08-02 07:16:59 +08:00
|
|
|
else:
|
2016-12-16 02:42:44 +08:00
|
|
|
if val is not _DEFERRED:
|
|
|
|
_setattr(self, field.attname, val)
|
2007-02-26 14:16:19 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
if kwargs:
|
2016-12-16 02:42:44 +08:00
|
|
|
property_names = opts._property_names
|
|
|
|
for prop in tuple(kwargs):
|
2007-02-26 14:16:19 +08:00
|
|
|
try:
|
2016-04-20 04:34:08 +08:00
|
|
|
# Any remaining kwargs must correspond to properties or
|
|
|
|
# virtual fields.
|
2016-12-16 02:42:44 +08:00
|
|
|
if prop in property_names or opts.get_field(prop):
|
|
|
|
if kwargs[prop] is not _DEFERRED:
|
|
|
|
_setattr(self, prop, kwargs[prop])
|
2016-01-22 00:49:40 +08:00
|
|
|
del kwargs[prop]
|
2016-04-20 04:34:08 +08:00
|
|
|
except (AttributeError, FieldDoesNotExist):
|
2007-02-26 14:16:19 +08:00
|
|
|
pass
|
2017-07-31 23:02:23 +08:00
|
|
|
for kwarg in kwargs:
|
2018-06-25 21:39:16 +08:00
|
|
|
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2016-12-16 02:42:44 +08:00
|
|
|
post_init.send(sender=cls, instance=self)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-06-06 20:35:40 +08:00
|
|
|
@classmethod
|
|
|
|
def from_db(cls, db, field_names, values):
|
2016-02-02 17:33:09 +08:00
|
|
|
if len(values) != len(cls._meta.concrete_fields):
|
2017-08-02 21:16:36 +08:00
|
|
|
values_iter = iter(values)
|
|
|
|
values = [
|
|
|
|
next(values_iter) if f.attname in field_names else DEFERRED
|
|
|
|
for f in cls._meta.concrete_fields
|
|
|
|
]
|
2016-02-02 17:33:09 +08:00
|
|
|
new = cls(*values)
|
2014-06-06 20:35:40 +08:00
|
|
|
new._state.adding = False
|
|
|
|
new._state.db = db
|
|
|
|
return new
|
|
|
|
|
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 __repr__(self):
|
2017-06-12 03:52:19 +08:00
|
|
|
return '<%s: %s>' % (self.__class__.__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
|
|
|
|
|
|
|
def __str__(self):
|
2017-04-09 02:38:48 +08:00
|
|
|
return '%s object (%s)' % (self.__class__.__name__, self.pk)
|
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 __eq__(self, other):
|
2013-08-14 16:05:01 +08:00
|
|
|
if not isinstance(other, Model):
|
2019-09-03 10:09:31 +08:00
|
|
|
return NotImplemented
|
2013-08-14 16:05:01 +08:00
|
|
|
if self._meta.concrete_model != other._meta.concrete_model:
|
|
|
|
return False
|
2017-06-06 03:20:34 +08:00
|
|
|
my_pk = self.pk
|
2013-08-14 16:05:01 +08:00
|
|
|
if my_pk is None:
|
|
|
|
return self is other
|
2017-06-06 03:20:34 +08:00
|
|
|
return my_pk == other.pk
|
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 __hash__(self):
|
2017-06-06 03:20:34 +08:00
|
|
|
if self.pk is None:
|
2013-08-14 16:05:01 +08:00
|
|
|
raise TypeError("Model instances without primary key value are unhashable")
|
2017-06-06 03:20:34 +08:00
|
|
|
return hash(self.pk)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2009-03-19 17:06:04 +08:00
|
|
|
def __reduce__(self):
|
2017-08-12 03:27:25 +08:00
|
|
|
data = self.__getstate__()
|
2014-06-06 19:10:20 +08:00
|
|
|
data[DJANGO_VERSION_PICKLE_KEY] = get_version()
|
2016-02-02 17:33:09 +08:00
|
|
|
class_id = self._meta.app_label, self._meta.object_name
|
|
|
|
return model_unpickle, (class_id,), data
|
2009-03-19 17:06:04 +08:00
|
|
|
|
2017-08-12 03:27:25 +08:00
|
|
|
def __getstate__(self):
|
|
|
|
"""Hook to allow choosing the attributes to pickle."""
|
|
|
|
return self.__dict__
|
|
|
|
|
2014-06-06 19:10:20 +08:00
|
|
|
def __setstate__(self, state):
|
|
|
|
msg = None
|
|
|
|
pickled_version = state.get(DJANGO_VERSION_PICKLE_KEY)
|
|
|
|
if pickled_version:
|
|
|
|
current_version = get_version()
|
|
|
|
if current_version != pickled_version:
|
2016-03-29 06:33:29 +08:00
|
|
|
msg = (
|
|
|
|
"Pickled model instance's Django version %s does not match "
|
|
|
|
"the current version %s." % (pickled_version, current_version)
|
|
|
|
)
|
2014-06-06 19:10:20 +08:00
|
|
|
else:
|
|
|
|
msg = "Pickled model instance's Django version is not specified."
|
|
|
|
|
|
|
|
if msg:
|
|
|
|
warnings.warn(msg, RuntimeWarning, stacklevel=2)
|
|
|
|
|
|
|
|
self.__dict__.update(state)
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def _get_pk_val(self, meta=None):
|
2018-01-04 07:52:12 +08:00
|
|
|
meta = meta or self._meta
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
return getattr(self, meta.pk.attname)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def _set_pk_val(self, value):
|
2019-10-28 13:28:40 +08:00
|
|
|
for parent_link in self._meta.parents.values():
|
|
|
|
if parent_link and parent_link != self._meta.pk:
|
|
|
|
setattr(self, parent_link.target_field.attname, value)
|
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 setattr(self, self._meta.pk.attname, value)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
pk = property(_get_pk_val, _set_pk_val)
|
|
|
|
|
2014-07-05 14:03:52 +08:00
|
|
|
def get_deferred_fields(self):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a set containing names of deferred fields for this instance.
|
2014-07-05 14:03:52 +08:00
|
|
|
"""
|
|
|
|
return {
|
|
|
|
f.attname for f in self._meta.concrete_fields
|
2016-02-02 17:33:09 +08:00
|
|
|
if f.attname not in self.__dict__
|
2014-07-05 14:03:52 +08:00
|
|
|
}
|
|
|
|
|
2016-05-17 02:49:03 +08:00
|
|
|
def refresh_from_db(self, using=None, fields=None):
|
2014-07-05 14:03:52 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Reload field values from the database.
|
2014-07-05 14:03:52 +08:00
|
|
|
|
|
|
|
By default, the reloading happens from the database this instance was
|
|
|
|
loaded from, or by the read router if this instance wasn't loaded from
|
|
|
|
any database. The using parameter will override the default.
|
|
|
|
|
|
|
|
Fields can be used to specify which fields to reload. The fields
|
|
|
|
should be an iterable of field attnames. If fields is None, then
|
|
|
|
all non-deferred fields are reloaded.
|
|
|
|
|
|
|
|
When accessing deferred fields of an instance, the deferred loading
|
|
|
|
of the field will call this method.
|
|
|
|
"""
|
2018-08-18 15:33:43 +08:00
|
|
|
if fields is None:
|
|
|
|
self._prefetched_objects_cache = {}
|
|
|
|
else:
|
|
|
|
prefetched_objects_cache = getattr(self, '_prefetched_objects_cache', ())
|
|
|
|
for field in fields:
|
|
|
|
if field in prefetched_objects_cache:
|
|
|
|
del prefetched_objects_cache[field]
|
|
|
|
fields.remove(field)
|
2017-11-30 00:54:34 +08:00
|
|
|
if not fields:
|
2014-07-05 14:03:52 +08:00
|
|
|
return
|
|
|
|
if any(LOOKUP_SEP in f for f in fields):
|
|
|
|
raise ValueError(
|
|
|
|
'Found "%s" in fields argument. Relations and transforms '
|
|
|
|
'are not allowed in fields.' % LOOKUP_SEP)
|
|
|
|
|
2018-01-02 22:42:24 +08:00
|
|
|
hints = {'instance': self}
|
|
|
|
db_instance_qs = self.__class__._base_manager.db_manager(using, hints=hints).filter(pk=self.pk)
|
2014-07-05 14:03:52 +08:00
|
|
|
|
|
|
|
# Use provided fields, if not set then reload all non-deferred fields.
|
2016-02-02 17:33:09 +08:00
|
|
|
deferred_fields = self.get_deferred_fields()
|
2014-07-05 14:03:52 +08:00
|
|
|
if fields is not None:
|
|
|
|
fields = list(fields)
|
|
|
|
db_instance_qs = db_instance_qs.only(*fields)
|
2016-02-02 17:33:09 +08:00
|
|
|
elif deferred_fields:
|
2014-07-05 14:03:52 +08:00
|
|
|
fields = [f.attname for f in self._meta.concrete_fields
|
|
|
|
if f.attname not in deferred_fields]
|
|
|
|
db_instance_qs = db_instance_qs.only(*fields)
|
|
|
|
|
|
|
|
db_instance = db_instance_qs.get()
|
|
|
|
non_loaded_fields = db_instance.get_deferred_fields()
|
|
|
|
for field in self._meta.concrete_fields:
|
|
|
|
if field.attname in non_loaded_fields:
|
|
|
|
# This field wasn't refreshed - skip ahead.
|
|
|
|
continue
|
|
|
|
setattr(self, field.attname, getattr(db_instance, field.attname))
|
2018-01-30 23:43:53 +08:00
|
|
|
# Clear cached foreign keys.
|
2016-06-05 07:10:37 +08:00
|
|
|
if field.is_relation and field.is_cached(self):
|
2018-01-30 23:43:53 +08:00
|
|
|
field.delete_cached_value(self)
|
2017-09-20 01:51:19 +08:00
|
|
|
|
|
|
|
# Clear cached relations.
|
|
|
|
for field in self._meta.related_objects:
|
|
|
|
if field.is_cached(self):
|
|
|
|
field.delete_cached_value(self)
|
|
|
|
|
2014-07-05 14:03:52 +08:00
|
|
|
self._state.db = db_instance._state.db
|
|
|
|
|
2008-12-08 16:15:37 +08:00
|
|
|
def serializable_value(self, field_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return the value of the field name for this instance. If the field is
|
|
|
|
a foreign key, return the id value instead of the object. If there's
|
|
|
|
no Field object with this name on the model, return the model
|
|
|
|
attribute's value.
|
2008-12-09 14:49:40 +08:00
|
|
|
|
2008-12-08 16:15:37 +08:00
|
|
|
Used to serialize a field's value (in the serializer, or form output,
|
2008-12-09 14:49:40 +08:00
|
|
|
for example). Normally, you would just access the attribute directly
|
|
|
|
and not use this method.
|
2008-12-08 16:15:37 +08:00
|
|
|
"""
|
2008-12-09 14:49:40 +08:00
|
|
|
try:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = self._meta.get_field(field_name)
|
2008-12-09 14:49:40 +08:00
|
|
|
except FieldDoesNotExist:
|
|
|
|
return getattr(self, field_name)
|
2008-12-08 16:15:37 +08:00
|
|
|
return getattr(self, field.attname)
|
|
|
|
|
2012-05-12 15:24:20 +08:00
|
|
|
def save(self, force_insert=False, force_update=False, using=None,
|
|
|
|
update_fields=None):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Save the current instance. Override this in a subclass if you want 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
|
|
|
control the saving process.
|
2008-08-10 01:19:23 +08:00
|
|
|
|
|
|
|
The 'force_insert' and 'force_update' parameters can be used to insist
|
|
|
|
that the "save" must be an SQL insert or update (or equivalent for
|
|
|
|
non-SQL backends), respectively. Normally, they should not be set.
|
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-07-24 19:51:40 +08:00
|
|
|
# Ensure that a model instance without a PK hasn't been assigned to
|
|
|
|
# a ForeignKey or OneToOneField on this model. If the field is
|
|
|
|
# nullable, allowing the save() would result in silent data loss.
|
|
|
|
for field in self._meta.concrete_fields:
|
2016-06-05 07:10:37 +08:00
|
|
|
# If the related field isn't cached, then an instance hasn't
|
|
|
|
# been assigned and there's no need to worry about this check.
|
|
|
|
if field.is_relation and field.is_cached(self):
|
2015-07-24 19:51:40 +08:00
|
|
|
obj = getattr(self, field.name, None)
|
2019-05-28 21:40:31 +08:00
|
|
|
if not obj:
|
|
|
|
continue
|
2015-07-24 19:51:40 +08:00
|
|
|
# A pk may have been assigned manually to a model instance not
|
|
|
|
# saved to the database (or auto-generated in a case like
|
|
|
|
# UUIDField), but we allow the save to proceed and rely on the
|
|
|
|
# database to raise an IntegrityError if applicable. If
|
|
|
|
# constraints aren't supported by the database, there's the
|
|
|
|
# unavoidable risk of data corruption.
|
2019-05-28 21:40:31 +08:00
|
|
|
if obj.pk is None:
|
|
|
|
# Remove the object from a related instance cache.
|
|
|
|
if not field.remote_field.multiple:
|
|
|
|
field.remote_field.delete_cached_value(obj)
|
|
|
|
raise ValueError(
|
|
|
|
"save() prohibited to prevent data loss due to "
|
|
|
|
"unsaved related object '%s'." % field.name
|
|
|
|
)
|
|
|
|
elif getattr(self, field.attname) is None:
|
|
|
|
# Use pk from related object if it has been saved after
|
|
|
|
# an assignment.
|
|
|
|
setattr(self, field.attname, obj.pk)
|
2018-10-29 10:54:02 +08:00
|
|
|
# If the relationship's pk/to_field was changed, clear the
|
|
|
|
# cached relationship.
|
2019-05-28 21:40:31 +08:00
|
|
|
if getattr(obj, field.target_field.attname) != getattr(self, field.attname):
|
2017-09-21 12:27:04 +08:00
|
|
|
field.delete_cached_value(self)
|
2015-07-24 19:51:40 +08:00
|
|
|
|
2012-08-13 03:17:54 +08:00
|
|
|
using = using or router.db_for_write(self.__class__, instance=self)
|
2012-05-12 15:24:20 +08:00
|
|
|
if force_insert and (force_update or update_fields):
|
2010-01-11 02:36:20 +08:00
|
|
|
raise ValueError("Cannot force both insert and updating in model saving.")
|
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
|
|
|
|
2016-02-02 17:33:09 +08:00
|
|
|
deferred_fields = self.get_deferred_fields()
|
2012-05-12 15:24:20 +08:00
|
|
|
if update_fields is not None:
|
|
|
|
# If update_fields is empty, skip the save. We do also check for
|
|
|
|
# no-op saves later on for inheritance cases. This bailout is
|
|
|
|
# still needed for skipping signal sending.
|
2017-11-30 00:54:34 +08:00
|
|
|
if not update_fields:
|
2012-05-12 15:24:20 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
update_fields = frozenset(update_fields)
|
2012-07-05 21:39:19 +08:00
|
|
|
field_names = set()
|
|
|
|
|
|
|
|
for field in self._meta.fields:
|
|
|
|
if not field.primary_key:
|
|
|
|
field_names.add(field.name)
|
|
|
|
|
|
|
|
if field.name != field.attname:
|
|
|
|
field_names.add(field.attname)
|
|
|
|
|
2012-05-12 15:24:20 +08:00
|
|
|
non_model_fields = update_fields.difference(field_names)
|
|
|
|
|
|
|
|
if non_model_fields:
|
|
|
|
raise ValueError("The following fields do not exist in this "
|
|
|
|
"model or are m2m fields: %s"
|
|
|
|
% ', '.join(non_model_fields))
|
|
|
|
|
2012-08-13 03:17:54 +08:00
|
|
|
# If saving to the same database, and this model is deferred, then
|
2019-04-14 15:44:56 +08:00
|
|
|
# automatically do an "update_fields" save on the loaded fields.
|
2016-02-02 17:33:09 +08:00
|
|
|
elif not force_insert and deferred_fields and using == self._state.db:
|
2012-08-13 03:17:54 +08:00
|
|
|
field_names = set()
|
2013-03-25 00:40:40 +08:00
|
|
|
for field in self._meta.concrete_fields:
|
2012-08-13 03:17:54 +08:00
|
|
|
if not field.primary_key and not hasattr(field, 'through'):
|
|
|
|
field_names.add(field.attname)
|
|
|
|
loaded_fields = field_names.difference(deferred_fields)
|
|
|
|
if loaded_fields:
|
|
|
|
update_fields = frozenset(loaded_fields)
|
|
|
|
|
2012-05-12 15:24:20 +08:00
|
|
|
self.save_base(using=using, force_insert=force_insert,
|
|
|
|
force_update=force_update, update_fields=update_fields)
|
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
|
|
|
save.alters_data = True
|
|
|
|
|
2012-11-29 18:10:31 +08:00
|
|
|
def save_base(self, raw=False, force_insert=False,
|
2012-05-12 15:24:20 +08:00
|
|
|
force_update=False, using=None, update_fields=None):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Handle the parts of saving which should be done only once per save,
|
2012-11-29 18:10:31 +08:00
|
|
|
yet need to be done in raw saves, too. This includes some sanity
|
|
|
|
checks and signal sending.
|
|
|
|
|
|
|
|
The 'raw' argument is telling save_base not to save any parent
|
|
|
|
models and not to do any changes to the values before save. This
|
|
|
|
is used by fixture loading.
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
2010-01-22 22:30:06 +08:00
|
|
|
using = using or router.db_for_write(self.__class__, instance=self)
|
2012-05-12 15:24:20 +08:00
|
|
|
assert not (force_insert and (force_update or update_fields))
|
2017-11-30 00:54:34 +08:00
|
|
|
assert update_fields is None or update_fields
|
2012-11-29 18:10:31 +08:00
|
|
|
cls = origin = self.__class__
|
|
|
|
# Skip proxies, but keep the origin as the proxy model.
|
|
|
|
if cls._meta.proxy:
|
|
|
|
cls = cls._meta.concrete_model
|
|
|
|
meta = cls._meta
|
|
|
|
if not meta.auto_created:
|
2016-12-16 02:42:44 +08:00
|
|
|
pre_save.send(
|
|
|
|
sender=origin, instance=self, raw=raw, using=using,
|
|
|
|
update_fields=update_fields,
|
|
|
|
)
|
2018-09-27 15:45:10 +08:00
|
|
|
# A transaction isn't needed if one query is issued.
|
|
|
|
if meta.parents:
|
|
|
|
context_manager = transaction.atomic(using=using, savepoint=False)
|
|
|
|
else:
|
|
|
|
context_manager = transaction.mark_for_rollback_on_error(using=using)
|
|
|
|
with context_manager:
|
2018-07-20 20:59:15 +08:00
|
|
|
parent_inserted = False
|
2012-11-29 18:10:31 +08:00
|
|
|
if not raw:
|
2018-07-20 20:59:15 +08:00
|
|
|
parent_inserted = self._save_parents(cls, using, update_fields)
|
|
|
|
updated = self._save_table(
|
|
|
|
raw, cls, force_insert or parent_inserted,
|
|
|
|
force_update, using, update_fields,
|
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
# Store the database on which the object was saved
|
|
|
|
self._state.db = using
|
2010-11-19 06:43:46 +08:00
|
|
|
# Once saved, this is no longer a to-be-added instance.
|
|
|
|
self._state.adding = False
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Signal that the save is complete
|
2012-11-29 18:10:31 +08:00
|
|
|
if not meta.auto_created:
|
2016-12-16 02:42:44 +08:00
|
|
|
post_save.send(
|
|
|
|
sender=origin, instance=self, created=(not updated),
|
|
|
|
update_fields=update_fields, raw=raw, using=using,
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-05-13 22:56:47 +08:00
|
|
|
save_base.alters_data = True
|
|
|
|
|
2012-11-29 18:10:31 +08:00
|
|
|
def _save_parents(self, cls, using, update_fields):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Save all the parents of cls using values from self."""
|
2012-11-29 18:10:31 +08:00
|
|
|
meta = cls._meta
|
2018-07-20 20:59:15 +08:00
|
|
|
inserted = False
|
2012-11-29 18:10:31 +08:00
|
|
|
for parent, field in meta.parents.items():
|
|
|
|
# Make sure the link fields are synced between parent and self.
|
2016-04-04 08:37:32 +08:00
|
|
|
if (field and getattr(self, parent._meta.pk.attname) is None and
|
|
|
|
getattr(self, field.attname) is not None):
|
2012-11-29 18:10:31 +08:00
|
|
|
setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
|
2018-07-20 20:59:15 +08:00
|
|
|
parent_inserted = self._save_parents(cls=parent, using=using, update_fields=update_fields)
|
|
|
|
updated = self._save_table(
|
|
|
|
cls=parent, using=using, update_fields=update_fields,
|
|
|
|
force_insert=parent_inserted,
|
|
|
|
)
|
|
|
|
if not updated:
|
|
|
|
inserted = True
|
2012-11-29 18:10:31 +08:00
|
|
|
# Set the parent's PK value to self.
|
|
|
|
if field:
|
|
|
|
setattr(self, field.attname, self._get_pk_val(parent._meta))
|
|
|
|
# Since we didn't have an instance of the parent handy set
|
|
|
|
# attname directly, bypassing the descriptor. Invalidate
|
|
|
|
# the related object cache, in case it's been accidentally
|
|
|
|
# populated. A fresh instance will be re-built from the
|
|
|
|
# database if necessary.
|
2016-06-05 07:10:37 +08:00
|
|
|
if field.is_cached(self):
|
|
|
|
field.delete_cached_value(self)
|
2018-07-20 20:59:15 +08:00
|
|
|
return inserted
|
2012-11-29 18:10:31 +08:00
|
|
|
|
|
|
|
def _save_table(self, raw=False, cls=None, force_insert=False,
|
|
|
|
force_update=False, using=None, update_fields=None):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Do the heavy-lifting involved in saving. Update or insert the data
|
2012-11-29 18:10:31 +08:00
|
|
|
for a single table.
|
|
|
|
"""
|
|
|
|
meta = cls._meta
|
2013-03-25 00:40:40 +08:00
|
|
|
non_pks = [f for f in meta.local_concrete_fields if not f.primary_key]
|
2012-11-29 18:10:31 +08:00
|
|
|
|
|
|
|
if update_fields:
|
|
|
|
non_pks = [f for f in non_pks
|
|
|
|
if f.name in update_fields or f.attname in update_fields]
|
|
|
|
|
|
|
|
pk_val = self._get_pk_val(meta)
|
2015-01-28 19:40:48 +08:00
|
|
|
if pk_val is None:
|
|
|
|
pk_val = meta.pk.get_pk_value_on_save(self)
|
|
|
|
setattr(self, meta.pk.attname, pk_val)
|
2012-11-29 18:10:31 +08:00
|
|
|
pk_set = pk_val is not None
|
|
|
|
if not pk_set and (force_update or update_fields):
|
|
|
|
raise ValueError("Cannot force an update in save() with no primary key.")
|
|
|
|
updated = False
|
2019-08-17 21:30:29 +08:00
|
|
|
# Skip an UPDATE when adding an instance and primary key has a default.
|
|
|
|
if (
|
2019-12-12 11:34:28 +08:00
|
|
|
not raw and
|
2019-08-17 21:30:29 +08:00
|
|
|
not force_insert and
|
|
|
|
self._state.adding and
|
|
|
|
self._meta.pk.default and
|
|
|
|
self._meta.pk.default is not NOT_PROVIDED
|
|
|
|
):
|
|
|
|
force_insert = True
|
2012-11-29 18:10:31 +08:00
|
|
|
# If possible, try an UPDATE. If that doesn't update anything, do an INSERT.
|
|
|
|
if pk_set and not force_insert:
|
|
|
|
base_qs = cls._base_manager.using(using)
|
2013-05-17 22:33:36 +08:00
|
|
|
values = [(f, None, (getattr(self, f.attname) if raw else f.pre_save(self, False)))
|
2012-11-29 18:10:31 +08:00
|
|
|
for f in non_pks]
|
2013-08-30 14:41:07 +08:00
|
|
|
forced_update = update_fields or force_update
|
|
|
|
updated = self._do_update(base_qs, using, pk_val, values, update_fields,
|
|
|
|
forced_update)
|
2012-11-29 18:10:31 +08:00
|
|
|
if force_update and not updated:
|
|
|
|
raise DatabaseError("Forced update did not affect any rows.")
|
|
|
|
if update_fields and not updated:
|
|
|
|
raise DatabaseError("Save with update_fields did not affect any rows.")
|
|
|
|
if not updated:
|
|
|
|
if meta.order_with_respect_to:
|
|
|
|
# If this is a model with an order_with_respect_to
|
|
|
|
# autopopulate the _order field
|
|
|
|
field = meta.order_with_respect_to
|
2015-03-27 00:52:11 +08:00
|
|
|
filter_args = field.get_filter_kwargs_for_object(self)
|
2019-08-30 16:28:18 +08:00
|
|
|
self._order = cls._base_manager.using(using).filter(**filter_args).aggregate(
|
|
|
|
_order__max=Coalesce(
|
|
|
|
ExpressionWrapper(Max('_order') + Value(1), output_field=IntegerField()),
|
|
|
|
Value(0),
|
|
|
|
),
|
|
|
|
)['_order__max']
|
2013-03-25 00:40:40 +08:00
|
|
|
fields = meta.local_concrete_fields
|
2012-11-29 18:10:31 +08:00
|
|
|
if not pk_set:
|
2016-11-24 19:01:36 +08:00
|
|
|
fields = [f for f in fields if f is not meta.auto_field]
|
2012-11-29 18:10:31 +08:00
|
|
|
|
2019-07-24 14:42:41 +08:00
|
|
|
returning_fields = meta.db_returning_fields
|
|
|
|
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
|
|
|
|
for result, field in zip(results, returning_fields):
|
|
|
|
setattr(self, field.attname, result)
|
2012-11-29 18:10:31 +08:00
|
|
|
return updated
|
|
|
|
|
2013-08-30 14:41:07 +08:00
|
|
|
def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):
|
2012-11-29 18:10:31 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Try to update the model. Return True if the model was updated (if an
|
|
|
|
update query was done and a matching row was found in the DB).
|
2012-11-29 18:10:31 +08:00
|
|
|
"""
|
2013-08-30 14:41:07 +08:00
|
|
|
filtered = base_qs.filter(pk=pk_val)
|
2013-05-30 22:25:11 +08:00
|
|
|
if not values:
|
|
|
|
# We can end up here when saving a model in inheritance chain where
|
|
|
|
# update_fields doesn't target any field in current model. In that
|
|
|
|
# case we just say the update succeeded. Another case ending up here
|
|
|
|
# is a model with just PK - in that case check that the PK still
|
|
|
|
# exists.
|
2013-08-30 14:41:07 +08:00
|
|
|
return update_fields is not None or filtered.exists()
|
|
|
|
if self._meta.select_on_save and not forced_update:
|
2018-01-04 07:52:12 +08:00
|
|
|
return (
|
|
|
|
filtered.exists() and
|
2014-07-12 04:11:04 +08:00
|
|
|
# It may happen that the object is deleted from the DB right after
|
|
|
|
# this check, causing the subsequent UPDATE to return zero matching
|
|
|
|
# rows. The same result can occur in some rare cases when the
|
|
|
|
# database returns zero despite the UPDATE being executed
|
|
|
|
# successfully (a row is matched and updated). In order to
|
|
|
|
# distinguish these two cases, the object's existence in the
|
|
|
|
# database is again checked for if the UPDATE query returns 0.
|
2018-01-04 07:52:12 +08:00
|
|
|
(filtered._update(values) > 0 or filtered.exists())
|
|
|
|
)
|
2013-08-30 14:41:07 +08:00
|
|
|
return filtered._update(values) > 0
|
2012-11-29 18:10:31 +08:00
|
|
|
|
2019-07-24 14:42:41 +08:00
|
|
|
def _do_insert(self, manager, using, fields, returning_fields, raw):
|
2012-11-29 18:10:31 +08:00
|
|
|
"""
|
2019-07-24 14:42:41 +08:00
|
|
|
Do an INSERT. If returning_fields is defined then this method should
|
|
|
|
return the newly created data for the model.
|
2012-11-29 18:10:31 +08:00
|
|
|
"""
|
2019-07-24 14:42:41 +08:00
|
|
|
return manager._insert(
|
|
|
|
[self], fields=fields, returning_fields=returning_fields,
|
|
|
|
using=using, raw=raw,
|
|
|
|
)
|
2012-11-29 18:10:31 +08:00
|
|
|
|
2015-02-07 07:16:26 +08:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
2010-01-22 22:30:06 +08:00
|
|
|
using = using or router.db_for_write(self.__class__, instance=self)
|
2017-06-06 03:20:34 +08:00
|
|
|
assert self.pk is not None, (
|
2014-09-04 20:15:09 +08:00
|
|
|
"%s object can't be deleted because its %s attribute is set to None." %
|
|
|
|
(self._meta.object_name, self._meta.pk.attname)
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
collector = Collector(using=using)
|
2015-02-07 07:16:26 +08:00
|
|
|
collector.collect([self], keep_parents=keep_parents)
|
2015-03-07 16:56:25 +08:00
|
|
|
return collector.delete()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
delete.alters_data = True
|
|
|
|
|
|
|
|
def _get_FIELD_display(self, field):
|
|
|
|
value = getattr(self, field.attname)
|
2019-11-07 22:35:33 +08:00
|
|
|
choices_dict = dict(make_hashable(field.flatchoices))
|
2019-03-22 20:21:00 +08:00
|
|
|
# force_str() to coerce lazy strings.
|
2019-11-07 22:35:33 +08:00
|
|
|
return force_str(choices_dict.get(make_hashable(value), value), strings_only=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-06-20 10:17:14 +08:00
|
|
|
def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
|
2010-09-11 08:20:35 +08:00
|
|
|
if not self.pk:
|
|
|
|
raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
|
2013-05-17 22:33:36 +08:00
|
|
|
op = 'gt' if is_next else 'lt'
|
|
|
|
order = '' if is_next else '-'
|
2017-03-04 22:47:49 +08:00
|
|
|
param = getattr(self, field.attname)
|
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
|
|
|
q = Q(**{'%s__%s' % (field.name, op): param})
|
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
|
|
|
q = q | Q(**{field.name: param, 'pk__%s' % op: self.pk})
|
2014-09-04 20:15:09 +08:00
|
|
|
qs = self.__class__._default_manager.using(self._state.db).filter(**kwargs).filter(q).order_by(
|
|
|
|
'%s%s' % (order, field.name), '%spk' % order
|
|
|
|
)
|
2006-05-07 02:46:53 +08:00
|
|
|
try:
|
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 qs[0]
|
2006-05-24 03:55:30 +08:00
|
|
|
except IndexError:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise self.DoesNotExist("%s matching query does not exist." % self.__class__._meta.object_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def _get_next_or_previous_in_order(self, is_next):
|
|
|
|
cachename = "__%s_order_cache" % is_next
|
|
|
|
if not hasattr(self, cachename):
|
2013-05-17 22:33:36 +08:00
|
|
|
op = 'gt' if is_next else 'lt'
|
|
|
|
order = '_order' if is_next else '-_order'
|
2006-05-02 09:31:56 +08:00
|
|
|
order_field = self._meta.order_with_respect_to
|
2015-03-27 00:52:11 +08:00
|
|
|
filter_args = order_field.get_filter_kwargs_for_object(self)
|
2016-04-17 19:55:55 +08:00
|
|
|
obj = self.__class__._default_manager.filter(**filter_args).filter(**{
|
|
|
|
'_order__%s' % op: self.__class__._default_manager.values('_order').filter(**{
|
2009-12-22 23:18:51 +08:00
|
|
|
self._meta.pk.name: self.pk
|
|
|
|
})
|
|
|
|
}).order_by(order)[:1].get()
|
2006-05-02 09:31:56 +08:00
|
|
|
setattr(self, cachename, obj)
|
|
|
|
return getattr(self, cachename)
|
|
|
|
|
2014-11-16 03:36:41 +08:00
|
|
|
def prepare_database_save(self, field):
|
2013-09-07 06:13:51 +08:00
|
|
|
if self.pk is None:
|
|
|
|
raise ValueError("Unsaved model instance %r cannot be used in an ORM query." % self)
|
2015-04-07 21:08:05 +08:00
|
|
|
return getattr(self, field.remote_field.get_related_field().attname)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
def clean(self):
|
2010-01-05 11:56:19 +08:00
|
|
|
"""
|
|
|
|
Hook for doing any extra model-wide validation after clean() has been
|
2010-01-12 10:29:45 +08:00
|
|
|
called on every field by self.clean_fields. Any ValidationError raised
|
|
|
|
by this method will not be associated with a particular field; it will
|
|
|
|
have a special-case association with the field defined by NON_FIELD_ERRORS.
|
2010-01-05 11:56:19 +08:00
|
|
|
"""
|
2010-01-12 10:29:45 +08:00
|
|
|
pass
|
2010-01-05 11:56:19 +08:00
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
def validate_unique(self, exclude=None):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Check unique constraints on the model and raise ValidationError if any
|
|
|
|
failed.
|
2010-01-12 10:29:45 +08:00
|
|
|
"""
|
|
|
|
unique_checks, date_checks = self._get_unique_checks(exclude=exclude)
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
errors = self._perform_unique_checks(unique_checks)
|
|
|
|
date_errors = self._perform_date_checks(date_checks)
|
|
|
|
|
|
|
|
for k, v in date_errors.items():
|
2010-01-12 22:26:17 +08:00
|
|
|
errors.setdefault(k, []).extend(v)
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
def _get_unique_checks(self, exclude=None):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a list of checks to perform. Since validate_unique() could be
|
2010-01-12 10:29:45 +08:00
|
|
|
called from a ModelForm, some fields may have been excluded; we can't
|
|
|
|
perform a unique check on a model that is missing fields involved
|
2017-01-25 07:04:12 +08:00
|
|
|
in that check. Fields that did not validate should also be excluded,
|
|
|
|
but they need to be passed in via the exclude argument.
|
2010-01-12 10:29:45 +08:00
|
|
|
"""
|
|
|
|
if exclude is None:
|
|
|
|
exclude = []
|
|
|
|
unique_checks = []
|
2010-03-17 03:32:11 +08:00
|
|
|
|
|
|
|
unique_togethers = [(self.__class__, self._meta.unique_together)]
|
2018-08-06 10:30:44 +08:00
|
|
|
constraints = [(self.__class__, self._meta.constraints)]
|
2015-01-22 11:15:59 +08:00
|
|
|
for parent_class in self._meta.get_parent_list():
|
2010-03-17 03:32:11 +08:00
|
|
|
if parent_class._meta.unique_together:
|
|
|
|
unique_togethers.append((parent_class, parent_class._meta.unique_together))
|
2018-08-06 10:30:44 +08:00
|
|
|
if parent_class._meta.constraints:
|
|
|
|
constraints.append((parent_class, parent_class._meta.constraints))
|
2010-03-17 03:32:11 +08:00
|
|
|
|
|
|
|
for model_class, unique_together in unique_togethers:
|
|
|
|
for check in unique_together:
|
2017-12-27 02:44:12 +08:00
|
|
|
if not any(name in exclude for name in check):
|
|
|
|
# Add the check if the field isn't excluded.
|
2010-03-17 03:32:11 +08:00
|
|
|
unique_checks.append((model_class, tuple(check)))
|
2010-01-05 11:56:19 +08:00
|
|
|
|
2018-08-06 10:30:44 +08:00
|
|
|
for model_class, model_constraints in constraints:
|
|
|
|
for constraint in model_constraints:
|
|
|
|
if (isinstance(constraint, UniqueConstraint) and
|
2018-12-28 03:21:59 +08:00
|
|
|
# Partial unique constraints can't be validated.
|
|
|
|
constraint.condition is None and
|
2018-08-06 10:30:44 +08:00
|
|
|
not any(name in exclude for name in constraint.fields)):
|
|
|
|
unique_checks.append((model_class, constraint.fields))
|
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
# These are checks for the unique_for_<date/year/month>.
|
2010-01-05 11:56:19 +08:00
|
|
|
date_checks = []
|
|
|
|
|
|
|
|
# Gather a list of checks for fields declared as unique and add them to
|
2010-01-12 10:29:45 +08:00
|
|
|
# the list of checks.
|
2010-03-17 03:32:11 +08:00
|
|
|
|
|
|
|
fields_with_class = [(self.__class__, self._meta.local_fields)]
|
2015-01-22 11:15:59 +08:00
|
|
|
for parent_class in self._meta.get_parent_list():
|
2010-03-17 03:32:11 +08:00
|
|
|
fields_with_class.append((parent_class, parent_class._meta.local_fields))
|
|
|
|
|
|
|
|
for model_class, fields in fields_with_class:
|
|
|
|
for f in fields:
|
|
|
|
name = f.name
|
|
|
|
if name in exclude:
|
|
|
|
continue
|
|
|
|
if f.unique:
|
|
|
|
unique_checks.append((model_class, (name,)))
|
2010-08-17 15:07:28 +08:00
|
|
|
if f.unique_for_date and f.unique_for_date not in exclude:
|
2010-03-17 03:32:11 +08:00
|
|
|
date_checks.append((model_class, 'date', name, f.unique_for_date))
|
2010-08-17 15:07:28 +08:00
|
|
|
if f.unique_for_year and f.unique_for_year not in exclude:
|
2010-03-17 03:32:11 +08:00
|
|
|
date_checks.append((model_class, 'year', name, f.unique_for_year))
|
2010-08-17 15:07:28 +08:00
|
|
|
if f.unique_for_month and f.unique_for_month not in exclude:
|
2010-03-17 03:32:11 +08:00
|
|
|
date_checks.append((model_class, 'month', name, f.unique_for_month))
|
2010-01-05 11:56:19 +08:00
|
|
|
return unique_checks, date_checks
|
|
|
|
|
|
|
|
def _perform_unique_checks(self, unique_checks):
|
|
|
|
errors = {}
|
|
|
|
|
2010-03-17 03:32:11 +08:00
|
|
|
for model_class, unique_check in unique_checks:
|
2010-01-05 11:56:19 +08:00
|
|
|
# Try to look up an existing object with the same values as this
|
|
|
|
# object's values for all the unique field.
|
|
|
|
|
|
|
|
lookup_kwargs = {}
|
|
|
|
for field_name in unique_check:
|
|
|
|
f = self._meta.get_field(field_name)
|
|
|
|
lookup_value = getattr(self, f.attname)
|
2016-05-18 22:30:42 +08:00
|
|
|
# TODO: Handle multiple backends with different feature flags.
|
|
|
|
if (lookup_value is None or
|
|
|
|
(lookup_value == '' and connection.features.interprets_empty_strings_as_nulls)):
|
2010-01-05 11:56:19 +08:00
|
|
|
# no value, skip the lookup
|
|
|
|
continue
|
2010-11-19 06:43:46 +08:00
|
|
|
if f.primary_key and not self._state.adding:
|
2010-01-22 22:30:06 +08:00
|
|
|
# no need to check for unique primary key when editing
|
2010-01-05 11:56:19 +08:00
|
|
|
continue
|
|
|
|
lookup_kwargs[str(field_name)] = lookup_value
|
|
|
|
|
|
|
|
# some fields were skipped, no reason to do the check
|
2012-08-14 20:09:23 +08:00
|
|
|
if len(unique_check) != len(lookup_kwargs):
|
2010-01-05 11:56:19 +08:00
|
|
|
continue
|
|
|
|
|
2010-03-17 03:32:11 +08:00
|
|
|
qs = model_class._default_manager.filter(**lookup_kwargs)
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
# Exclude the current object from the query if we are editing an
|
|
|
|
# instance (as opposed to creating a new one)
|
2012-04-21 01:34:29 +08:00
|
|
|
# Note that we need to use the pk as defined by model_class, not
|
|
|
|
# self.pk. These can be different fields because model inheritance
|
|
|
|
# allows single model to have effectively multiple primary keys.
|
|
|
|
# Refs #17615.
|
|
|
|
model_class_pk = self._get_pk_val(model_class._meta)
|
|
|
|
if not self._state.adding and model_class_pk is not None:
|
|
|
|
qs = qs.exclude(pk=model_class_pk)
|
2010-01-05 12:12:09 +08:00
|
|
|
if qs.exists():
|
2010-01-05 11:56:19 +08:00
|
|
|
if len(unique_check) == 1:
|
|
|
|
key = unique_check[0]
|
|
|
|
else:
|
|
|
|
key = NON_FIELD_ERRORS
|
2010-03-17 03:32:11 +08:00
|
|
|
errors.setdefault(key, []).append(self.unique_error_message(model_class, unique_check))
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
def _perform_date_checks(self, date_checks):
|
|
|
|
errors = {}
|
2010-03-17 03:32:11 +08:00
|
|
|
for model_class, lookup_type, field, unique_for in date_checks:
|
2010-01-05 11:56:19 +08:00
|
|
|
lookup_kwargs = {}
|
|
|
|
# there's a ticket to add a date lookup, we can remove this special
|
|
|
|
# case if that makes it's way in
|
|
|
|
date = getattr(self, unique_for)
|
2011-01-09 21:26:39 +08:00
|
|
|
if date is None:
|
|
|
|
continue
|
2010-01-05 11:56:19 +08:00
|
|
|
if lookup_type == 'date':
|
|
|
|
lookup_kwargs['%s__day' % unique_for] = date.day
|
|
|
|
lookup_kwargs['%s__month' % unique_for] = date.month
|
|
|
|
lookup_kwargs['%s__year' % unique_for] = date.year
|
|
|
|
else:
|
|
|
|
lookup_kwargs['%s__%s' % (unique_for, lookup_type)] = getattr(date, lookup_type)
|
|
|
|
lookup_kwargs[field] = getattr(self, field)
|
|
|
|
|
2010-03-17 03:32:11 +08:00
|
|
|
qs = model_class._default_manager.filter(**lookup_kwargs)
|
2010-01-05 11:56:19 +08:00
|
|
|
# Exclude the current object from the query if we are editing an
|
|
|
|
# instance (as opposed to creating a new one)
|
2010-11-19 06:43:46 +08:00
|
|
|
if not self._state.adding and self.pk is not None:
|
2010-01-05 11:56:19 +08:00
|
|
|
qs = qs.exclude(pk=self.pk)
|
|
|
|
|
2010-01-05 12:30:51 +08:00
|
|
|
if qs.exists():
|
2010-01-05 11:56:19 +08:00
|
|
|
errors.setdefault(field, []).append(
|
|
|
|
self.date_error_message(lookup_type, field, unique_for)
|
|
|
|
)
|
|
|
|
return errors
|
|
|
|
|
2014-02-04 02:31:27 +08:00
|
|
|
def date_error_message(self, lookup_type, field_name, unique_for):
|
2010-01-05 11:56:19 +08:00
|
|
|
opts = self._meta
|
2014-02-04 02:31:27 +08:00
|
|
|
field = opts.get_field(field_name)
|
|
|
|
return ValidationError(
|
|
|
|
message=field.error_messages['unique_for_date'],
|
|
|
|
code='unique_for_date',
|
|
|
|
params={
|
|
|
|
'model': self,
|
2016-12-29 23:27:49 +08:00
|
|
|
'model_name': capfirst(opts.verbose_name),
|
2014-02-04 02:31:27 +08:00
|
|
|
'lookup_type': lookup_type,
|
|
|
|
'field': field_name,
|
2016-12-29 23:27:49 +08:00
|
|
|
'field_label': capfirst(field.verbose_name),
|
2014-02-04 02:31:27 +08:00
|
|
|
'date_field': unique_for,
|
2016-12-29 23:27:49 +08:00
|
|
|
'date_field_label': capfirst(opts.get_field(unique_for).verbose_name),
|
2014-02-04 02:31:27 +08:00
|
|
|
}
|
|
|
|
)
|
2010-01-05 11:56:19 +08:00
|
|
|
|
2010-03-17 03:32:11 +08:00
|
|
|
def unique_error_message(self, model_class, unique_check):
|
|
|
|
opts = model_class._meta
|
2014-02-04 02:31:27 +08:00
|
|
|
|
|
|
|
params = {
|
|
|
|
'model': self,
|
|
|
|
'model_class': model_class,
|
2016-12-29 23:27:49 +08:00
|
|
|
'model_name': capfirst(opts.verbose_name),
|
2014-02-04 02:31:27 +08:00
|
|
|
'unique_check': unique_check,
|
|
|
|
}
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
# A unique field
|
|
|
|
if len(unique_check) == 1:
|
2014-02-04 02:31:27 +08:00
|
|
|
field = opts.get_field(unique_check[0])
|
2016-12-29 23:27:49 +08:00
|
|
|
params['field_label'] = capfirst(field.verbose_name)
|
2014-02-04 02:31:27 +08:00
|
|
|
return ValidationError(
|
|
|
|
message=field.error_messages['unique'],
|
|
|
|
code='unique',
|
|
|
|
params=params,
|
|
|
|
)
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
# unique_together
|
|
|
|
else:
|
2012-08-14 20:36:11 +08:00
|
|
|
field_labels = [capfirst(opts.get_field(f).verbose_name) for f in unique_check]
|
2016-12-29 23:27:49 +08:00
|
|
|
params['field_labels'] = get_text_list(field_labels, _('and'))
|
2014-02-04 02:31:27 +08:00
|
|
|
return ValidationError(
|
|
|
|
message=_("%(model_name)s with this %(field_labels)s already exists."),
|
|
|
|
code='unique_together',
|
|
|
|
params=params,
|
|
|
|
)
|
2010-01-05 11:56:19 +08:00
|
|
|
|
2013-04-05 03:21:57 +08:00
|
|
|
def full_clean(self, exclude=None, validate_unique=True):
|
2010-01-12 10:29:45 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Call clean_fields(), clean(), and validate_unique() on the model.
|
|
|
|
Raise a ValidationError for any errors that occur.
|
2010-01-12 10:29:45 +08:00
|
|
|
"""
|
|
|
|
errors = {}
|
|
|
|
if exclude is None:
|
|
|
|
exclude = []
|
2014-05-01 17:55:52 +08:00
|
|
|
else:
|
|
|
|
exclude = list(exclude)
|
2010-01-12 10:29:45 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.clean_fields(exclude=exclude)
|
2012-04-29 00:09:37 +08:00
|
|
|
except ValidationError as e:
|
2010-01-12 10:29:45 +08:00
|
|
|
errors = e.update_error_dict(errors)
|
|
|
|
|
|
|
|
# Form.clean() is run even if other validation fails, so do the
|
|
|
|
# same with Model.clean() for consistency.
|
|
|
|
try:
|
|
|
|
self.clean()
|
2012-04-29 00:09:37 +08:00
|
|
|
except ValidationError as e:
|
2010-01-12 10:29:45 +08:00
|
|
|
errors = e.update_error_dict(errors)
|
|
|
|
|
|
|
|
# Run unique checks, but only for fields that passed validation.
|
2013-04-05 03:21:57 +08:00
|
|
|
if validate_unique:
|
2017-05-28 07:08:46 +08:00
|
|
|
for name in errors:
|
2013-04-05 03:21:57 +08:00
|
|
|
if name != NON_FIELD_ERRORS and name not in exclude:
|
|
|
|
exclude.append(name)
|
|
|
|
try:
|
|
|
|
self.validate_unique(exclude=exclude)
|
|
|
|
except ValidationError as e:
|
|
|
|
errors = e.update_error_dict(errors)
|
2010-01-12 10:29:45 +08:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
|
|
|
def clean_fields(self, exclude=None):
|
2010-01-05 11:56:19 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Clean all fields and raise a ValidationError containing a dict
|
2010-01-05 11:56:19 +08:00
|
|
|
of all validation errors if any occur.
|
|
|
|
"""
|
2010-01-12 10:29:45 +08:00
|
|
|
if exclude is None:
|
|
|
|
exclude = []
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
errors = {}
|
|
|
|
for f in self._meta.fields:
|
|
|
|
if f.name in exclude:
|
|
|
|
continue
|
2010-01-12 10:29:45 +08:00
|
|
|
# Skip validation for empty fields with blank=True. The developer
|
|
|
|
# is responsible for making sure they have a valid value.
|
|
|
|
raw_value = getattr(self, f.attname)
|
2013-03-06 23:58:04 +08:00
|
|
|
if f.blank and raw_value in f.empty_values:
|
2010-01-12 10:29:45 +08:00
|
|
|
continue
|
2010-01-05 11:56:19 +08:00
|
|
|
try:
|
2010-01-12 10:29:45 +08:00
|
|
|
setattr(self, f.attname, f.clean(raw_value, self))
|
2012-04-29 00:09:37 +08:00
|
|
|
except ValidationError as e:
|
2013-04-05 03:21:57 +08:00
|
|
|
errors[f.name] = e.error_list
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
@classmethod
|
|
|
|
def check(cls, **kwargs):
|
2017-12-11 20:08:45 +08:00
|
|
|
errors = [*cls._check_swappable(), *cls._check_model(), *cls._check_managers(**kwargs)]
|
2014-01-20 10:45:21 +08:00
|
|
|
if not cls._meta.swapped:
|
2017-12-11 20:08:45 +08:00
|
|
|
errors += [
|
|
|
|
*cls._check_fields(**kwargs),
|
|
|
|
*cls._check_m2m_through_same_relationship(),
|
|
|
|
*cls._check_long_column_names(),
|
|
|
|
]
|
2016-10-11 15:59:17 +08:00
|
|
|
clash_errors = (
|
2017-12-29 09:22:20 +08:00
|
|
|
*cls._check_id_field(),
|
|
|
|
*cls._check_field_name_clashes(),
|
|
|
|
*cls._check_model_name_db_lookup_clashes(),
|
|
|
|
*cls._check_property_name_related_field_accessor_clashes(),
|
2018-05-03 15:08:29 +08:00
|
|
|
*cls._check_single_primary_key(),
|
2016-10-11 15:59:17 +08:00
|
|
|
)
|
2014-02-08 05:34:56 +08:00
|
|
|
errors.extend(clash_errors)
|
|
|
|
# If there are field name clashes, hide consequent column name
|
|
|
|
# clashes.
|
|
|
|
if not clash_errors:
|
|
|
|
errors.extend(cls._check_column_name_clashes())
|
2017-12-11 20:08:45 +08:00
|
|
|
errors += [
|
|
|
|
*cls._check_index_together(),
|
|
|
|
*cls._check_unique_together(),
|
2017-12-28 07:56:24 +08:00
|
|
|
*cls._check_indexes(),
|
2017-12-11 20:08:45 +08:00
|
|
|
*cls._check_ordering(),
|
2016-11-05 21:12:12 +08:00
|
|
|
*cls._check_constraints(),
|
2017-12-11 20:08:45 +08:00
|
|
|
]
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _check_swappable(cls):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Check if the swapped model exists."""
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = []
|
|
|
|
if cls._meta.swapped:
|
|
|
|
try:
|
2014-01-26 19:57:08 +08:00
|
|
|
apps.get_model(cls._meta.swapped)
|
2014-01-20 10:45:21 +08:00
|
|
|
except ValueError:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2014-03-03 15:35:42 +08:00
|
|
|
"'%s' is not of the form 'app_label.app_name'." % cls._meta.swappable,
|
|
|
|
id='models.E001',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
2014-01-26 19:57:08 +08:00
|
|
|
except LookupError:
|
|
|
|
app_label, model_name = cls._meta.swapped.split('.')
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2015-02-09 21:53:58 +08:00
|
|
|
"'%s' references '%s.%s', which has not been "
|
|
|
|
"installed, or is abstract." % (
|
2014-03-03 15:35:42 +08:00
|
|
|
cls._meta.swappable, app_label, model_name
|
2014-01-26 19:57:08 +08:00
|
|
|
),
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E002',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
2014-01-26 19:57:08 +08:00
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
2014-05-29 06:26:57 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_model(cls):
|
|
|
|
errors = []
|
|
|
|
if cls._meta.proxy:
|
|
|
|
if cls._meta.local_fields or cls._meta.local_many_to_many:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"Proxy model '%s' contains model fields." % cls.__name__,
|
|
|
|
id='models.E017',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return errors
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_managers(cls, **kwargs):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Perform all manager checks."""
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = []
|
2016-02-19 03:27:55 +08:00
|
|
|
for manager in cls._meta.managers:
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.extend(manager.check(**kwargs))
|
|
|
|
return errors
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _check_fields(cls, **kwargs):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Perform all field checks."""
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = []
|
|
|
|
for field in cls._meta.local_fields:
|
|
|
|
errors.extend(field.check(**kwargs))
|
|
|
|
for field in cls._meta.local_many_to_many:
|
|
|
|
errors.extend(field.check(from_model=cls, **kwargs))
|
|
|
|
return errors
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _check_m2m_through_same_relationship(cls):
|
|
|
|
""" Check if no relationship model is used by more than one m2m field.
|
|
|
|
"""
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
seen_intermediary_signatures = []
|
|
|
|
|
|
|
|
fields = cls._meta.local_many_to_many
|
|
|
|
|
|
|
|
# Skip when the target model wasn't found.
|
2015-02-26 22:19:17 +08:00
|
|
|
fields = (f for f in fields if isinstance(f.remote_field.model, ModelBase))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
# Skip when the relationship model wasn't found.
|
2015-02-26 22:19:17 +08:00
|
|
|
fields = (f for f in fields if isinstance(f.remote_field.through, ModelBase))
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
for f in fields:
|
2018-07-19 06:21:40 +08:00
|
|
|
signature = (f.remote_field.model, cls, f.remote_field.through, f.remote_field.through_fields)
|
2014-01-20 10:45:21 +08:00
|
|
|
if signature in seen_intermediary_signatures:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2018-07-19 06:21:40 +08:00
|
|
|
"The model has two identical many-to-many relations "
|
|
|
|
"through the intermediate model '%s'." %
|
|
|
|
f.remote_field.through._meta.label,
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E003',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
seen_intermediary_signatures.append(signature)
|
|
|
|
return errors
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _check_id_field(cls):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Check if `id` field is a primary key."""
|
2017-06-02 07:08:59 +08:00
|
|
|
fields = [f for f in cls._meta.local_fields if f.name == 'id' and f != cls._meta.pk]
|
2014-01-20 10:45:21 +08:00
|
|
|
# fields is empty or consists of the invalid "id" field
|
|
|
|
if fields and not fields[0].primary_key and cls._meta.pk.name == 'id':
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2015-02-09 21:53:58 +08:00
|
|
|
"'id' can only be used as a field name if the field also "
|
|
|
|
"sets 'primary_key=True'.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E004',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2014-02-08 05:34:56 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_field_name_clashes(cls):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Forbid field shadowing in multi-table inheritance."""
|
2014-02-08 05:34:56 +08:00
|
|
|
errors = []
|
|
|
|
used_fields = {} # name or attname -> field
|
|
|
|
|
|
|
|
# Check that multi-inheritance doesn't cause field name shadowing.
|
2015-01-30 17:40:25 +08:00
|
|
|
for parent in cls._meta.get_parent_list():
|
2014-02-08 05:34:56 +08:00
|
|
|
for f in parent._meta.local_fields:
|
|
|
|
clash = used_fields.get(f.name) or used_fields.get(f.attname) or None
|
|
|
|
if clash:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2015-02-09 21:53:58 +08:00
|
|
|
"The field '%s' from parent model "
|
|
|
|
"'%s' clashes with the field '%s' "
|
|
|
|
"from parent model '%s'." % (
|
2014-02-08 05:34:56 +08:00
|
|
|
clash.name, clash.model._meta,
|
|
|
|
f.name, f.model._meta
|
|
|
|
),
|
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E005',
|
2014-02-08 05:34:56 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
used_fields[f.name] = f
|
|
|
|
used_fields[f.attname] = f
|
|
|
|
|
|
|
|
# Check that fields defined in the model don't clash with fields from
|
2015-08-26 01:37:32 +08:00
|
|
|
# parents, including auto-generated fields like multi-table inheritance
|
|
|
|
# child accessors.
|
|
|
|
for parent in cls._meta.get_parent_list():
|
|
|
|
for f in parent._meta.get_fields():
|
|
|
|
if f not in used_fields:
|
|
|
|
used_fields[f.name] = f
|
|
|
|
|
2014-02-08 05:34:56 +08:00
|
|
|
for f in cls._meta.local_fields:
|
|
|
|
clash = used_fields.get(f.name) or used_fields.get(f.attname) or None
|
|
|
|
# Note that we may detect clash between user-defined non-unique
|
|
|
|
# field "id" and automatically added unique field "id", both
|
|
|
|
# defined at the same model. This special case is considered in
|
|
|
|
# _check_id_field and here we ignore it.
|
2016-03-29 06:33:29 +08:00
|
|
|
id_conflict = f.name == "id" and clash and clash.name == "id" and clash.model == cls
|
2014-02-08 05:34:56 +08:00
|
|
|
if clash and not id_conflict:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2015-02-09 21:53:58 +08:00
|
|
|
"The field '%s' clashes with the field '%s' "
|
|
|
|
"from model '%s'." % (
|
2014-03-03 15:35:42 +08:00
|
|
|
f.name, clash.name, clash.model._meta
|
2014-02-08 05:34:56 +08:00
|
|
|
),
|
|
|
|
obj=f,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E006',
|
2014-02-08 05:34:56 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
used_fields[f.name] = f
|
|
|
|
used_fields[f.attname] = f
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_column_name_clashes(cls):
|
|
|
|
# Store a list of column names which have already been used by other fields.
|
|
|
|
used_column_names = []
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
for f in cls._meta.local_fields:
|
|
|
|
_, column_name = f.get_attname_column()
|
|
|
|
|
|
|
|
# Ensure the column name is not already in use.
|
|
|
|
if column_name and column_name in used_column_names:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2015-02-09 21:53:58 +08:00
|
|
|
"Field '%s' has column name '%s' that is used by "
|
|
|
|
"another field." % (f.name, column_name),
|
2014-03-03 15:35:42 +08:00
|
|
|
hint="Specify a 'db_column' for the field.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E007'
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
used_column_names.append(column_name)
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
2016-10-11 15:59:17 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_model_name_db_lookup_clashes(cls):
|
|
|
|
errors = []
|
|
|
|
model_name = cls.__name__
|
|
|
|
if model_name.startswith('_') or model_name.endswith('_'):
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"The model name '%s' cannot start or end with an underscore "
|
|
|
|
"as it collides with the query lookup syntax." % model_name,
|
|
|
|
obj=cls,
|
|
|
|
id='models.E023'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif LOOKUP_SEP in model_name:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"The model name '%s' cannot contain double underscores as "
|
|
|
|
"it collides with the query lookup syntax." % model_name,
|
|
|
|
obj=cls,
|
|
|
|
id='models.E024'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return errors
|
|
|
|
|
2017-12-29 09:22:20 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_property_name_related_field_accessor_clashes(cls):
|
|
|
|
errors = []
|
|
|
|
property_names = cls._meta._property_names
|
|
|
|
related_field_accessors = (
|
|
|
|
f.get_attname() for f in cls._meta._get_fields(reverse=False)
|
|
|
|
if f.is_relation and f.related_model is not None
|
|
|
|
)
|
|
|
|
for accessor in related_field_accessors:
|
|
|
|
if accessor in property_names:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"The property '%s' clashes with a related field "
|
|
|
|
"accessor." % accessor,
|
|
|
|
obj=cls,
|
|
|
|
id='models.E025',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return errors
|
|
|
|
|
2018-05-03 15:08:29 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_single_primary_key(cls):
|
|
|
|
errors = []
|
|
|
|
if sum(1 for f in cls._meta.local_fields if f.primary_key) > 1:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2018-05-03 21:28:37 +08:00
|
|
|
"The model cannot have more than one field with "
|
|
|
|
"'primary_key=True'.",
|
2018-05-03 15:08:29 +08:00
|
|
|
obj=cls,
|
|
|
|
id='models.E026',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return errors
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_index_together(cls):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Check the value of "index_together" option."""
|
2014-01-20 10:45:21 +08:00
|
|
|
if not isinstance(cls._meta.index_together, (tuple, list)):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 15:35:42 +08:00
|
|
|
"'index_together' must be a list or tuple.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E008',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2016-03-29 06:33:29 +08:00
|
|
|
elif any(not isinstance(fields, (tuple, list)) for fields in cls._meta.index_together):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 15:35:42 +08:00
|
|
|
"All 'index_together' elements must be lists or tuples.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E009',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
else:
|
|
|
|
errors = []
|
|
|
|
for fields in cls._meta.index_together:
|
|
|
|
errors.extend(cls._check_local_fields(fields, "index_together"))
|
|
|
|
return errors
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _check_unique_together(cls):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Check the value of "unique_together" option."""
|
2014-01-20 10:45:21 +08:00
|
|
|
if not isinstance(cls._meta.unique_together, (tuple, list)):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 15:35:42 +08:00
|
|
|
"'unique_together' must be a list or tuple.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E010',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2016-03-29 06:33:29 +08:00
|
|
|
elif any(not isinstance(fields, (tuple, list)) for fields in cls._meta.unique_together):
|
2014-01-20 10:45:21 +08:00
|
|
|
return [
|
|
|
|
checks.Error(
|
2014-03-03 15:35:42 +08:00
|
|
|
"All 'unique_together' elements must be lists or tuples.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E011',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
else:
|
|
|
|
errors = []
|
|
|
|
for fields in cls._meta.unique_together:
|
|
|
|
errors.extend(cls._check_local_fields(fields, "unique_together"))
|
|
|
|
return errors
|
|
|
|
|
2017-12-28 07:56:24 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_indexes(cls):
|
2019-07-05 00:21:50 +08:00
|
|
|
"""Check the fields and names of indexes."""
|
|
|
|
errors = []
|
|
|
|
for index in cls._meta.indexes:
|
|
|
|
# Index name can't start with an underscore or a number, restricted
|
|
|
|
# for cross-database compatibility with Oracle.
|
|
|
|
if index.name[0] == '_' or index.name[0].isdigit():
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"The index name '%s' cannot start with an underscore "
|
|
|
|
"or a number." % index.name,
|
|
|
|
obj=cls,
|
|
|
|
id='models.E033',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if len(index.name) > index.max_name_length:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"The index name '%s' cannot be longer than %d "
|
|
|
|
"characters." % (index.name, index.max_name_length),
|
|
|
|
obj=cls,
|
|
|
|
id='models.E034',
|
|
|
|
),
|
|
|
|
)
|
2017-12-28 07:56:24 +08:00
|
|
|
fields = [field for index in cls._meta.indexes for field, _ in index.fields_orders]
|
2019-07-05 00:21:50 +08:00
|
|
|
errors.extend(cls._check_local_fields(fields, 'indexes'))
|
|
|
|
return errors
|
2017-12-28 07:56:24 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_local_fields(cls, fields, option):
|
|
|
|
from django.db import models
|
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
# In order to avoid hitting the relation tree prematurely, we use our
|
|
|
|
# own fields_map instead of using get_field()
|
2019-04-30 18:00:34 +08:00
|
|
|
forward_fields_map = {}
|
|
|
|
for field in cls._meta._get_fields(reverse=False):
|
|
|
|
forward_fields_map[field.name] = field
|
|
|
|
if hasattr(field, 'attname'):
|
|
|
|
forward_fields_map[field.attname] = field
|
2015-01-07 08:16:35 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = []
|
|
|
|
for field_name in fields:
|
|
|
|
try:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = forward_fields_map[field_name]
|
|
|
|
except KeyError:
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2017-02-03 09:43:21 +08:00
|
|
|
"'%s' refers to the nonexistent field '%s'." % (
|
2015-02-09 21:53:58 +08:00
|
|
|
option, field_name,
|
|
|
|
),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E012',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(field.remote_field, models.ManyToManyRel):
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2015-02-09 21:53:58 +08:00
|
|
|
"'%s' refers to a ManyToManyField '%s', but "
|
|
|
|
"ManyToManyFields are not permitted in '%s'." % (
|
|
|
|
option, field_name, option,
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E013',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
2014-04-15 02:41:47 +08:00
|
|
|
)
|
|
|
|
elif field not in cls._meta.local_fields:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"'%s' refers to field '%s' which is not local to model '%s'."
|
|
|
|
% (option, field_name, cls._meta.object_name),
|
|
|
|
hint="This issue may be caused by multi-table inheritance.",
|
2014-04-15 02:41:47 +08:00
|
|
|
obj=cls,
|
|
|
|
id='models.E016',
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
return errors
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _check_ordering(cls):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""
|
|
|
|
Check "ordering" option -- is it a list of strings and do all fields
|
|
|
|
exist?
|
|
|
|
"""
|
2015-03-17 22:42:53 +08:00
|
|
|
if cls._meta._ordering_clash:
|
|
|
|
return [
|
|
|
|
checks.Error(
|
|
|
|
"'ordering' and 'order_with_respect_to' cannot be used together.",
|
|
|
|
obj=cls,
|
|
|
|
id='models.E021',
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
if cls._meta.order_with_respect_to or not cls._meta.ordering:
|
2014-01-20 10:45:21 +08:00
|
|
|
return []
|
|
|
|
|
|
|
|
if not isinstance(cls._meta.ordering, (list, tuple)):
|
|
|
|
return [
|
|
|
|
checks.Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"'ordering' must be a tuple or list (even if you want to order by only one field).",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=cls,
|
2014-03-03 15:35:42 +08:00
|
|
|
id='models.E014',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
fields = cls._meta.ordering
|
|
|
|
|
2017-06-26 02:17:13 +08:00
|
|
|
# Skip expressions and '?' fields.
|
|
|
|
fields = (f for f in fields if isinstance(f, str) and f != '?')
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
# Convert "-field" to "field".
|
|
|
|
fields = ((f[1:] if f.startswith('-') else f) for f in fields)
|
|
|
|
|
2019-03-02 02:38:21 +08:00
|
|
|
# Separate related fields and non-related fields.
|
2019-03-02 00:09:33 +08:00
|
|
|
_fields = []
|
|
|
|
related_fields = []
|
|
|
|
for f in fields:
|
|
|
|
if LOOKUP_SEP in f:
|
|
|
|
related_fields.append(f)
|
|
|
|
else:
|
|
|
|
_fields.append(f)
|
|
|
|
fields = _fields
|
|
|
|
|
|
|
|
# Check related fields.
|
|
|
|
for field in related_fields:
|
|
|
|
_cls = cls
|
|
|
|
fld = None
|
|
|
|
for part in field.split(LOOKUP_SEP):
|
|
|
|
try:
|
2019-09-27 18:16:26 +08:00
|
|
|
# pk is an alias that won't be found by opts.get_field.
|
|
|
|
if part == 'pk':
|
|
|
|
fld = _cls._meta.pk
|
|
|
|
else:
|
|
|
|
fld = _cls._meta.get_field(part)
|
2019-03-02 00:09:33 +08:00
|
|
|
if fld.is_relation:
|
|
|
|
_cls = fld.get_path_info()[-1].to_opts.model
|
2019-09-26 16:33:54 +08:00
|
|
|
else:
|
|
|
|
_cls = None
|
2019-03-02 00:09:33 +08:00
|
|
|
except (FieldDoesNotExist, AttributeError):
|
|
|
|
if fld is None or fld.get_transform(part) is None:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
"'ordering' refers to the nonexistent field, "
|
2019-03-02 02:38:21 +08:00
|
|
|
"related field, or lookup '%s'." % field,
|
2019-03-02 00:09:33 +08:00
|
|
|
obj=cls,
|
|
|
|
id='models.E015',
|
|
|
|
)
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
# Skip ordering on pk. This is always a valid order_by field
|
|
|
|
# but is an alias and therefore won't be found by opts.get_field.
|
2015-01-07 08:16:35 +08:00
|
|
|
fields = {f for f in fields if f != 'pk'}
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2017-02-03 09:43:21 +08:00
|
|
|
# Check for invalid or nonexistent fields in ordering.
|
2015-01-07 08:16:35 +08:00
|
|
|
invalid_fields = []
|
|
|
|
|
|
|
|
# Any field name that is not present in field_names does not exist.
|
|
|
|
# Also, ordering by m2m fields is not allowed.
|
|
|
|
opts = cls._meta
|
|
|
|
valid_fields = set(chain.from_iterable(
|
|
|
|
(f.name, f.attname) if not (f.auto_created and not f.concrete) else (f.field.related_query_name(),)
|
|
|
|
for f in chain(opts.fields, opts.related_objects)
|
|
|
|
))
|
|
|
|
|
|
|
|
invalid_fields.extend(fields - valid_fields)
|
|
|
|
|
|
|
|
for invalid_field in invalid_fields:
|
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
2019-03-02 02:38:21 +08:00
|
|
|
"'ordering' refers to the nonexistent field, related "
|
|
|
|
"field, or lookup '%s'." % invalid_field,
|
2015-01-07 08:16:35 +08:00
|
|
|
obj=cls,
|
|
|
|
id='models.E015',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
2015-01-07 08:16:35 +08:00
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
return errors
|
|
|
|
|
2014-06-10 20:04:19 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_long_column_names(cls):
|
|
|
|
"""
|
|
|
|
Check that any auto-generated column names are shorter than the limits
|
|
|
|
for each database in which the model will be created.
|
|
|
|
"""
|
|
|
|
errors = []
|
|
|
|
allowed_len = None
|
|
|
|
db_alias = None
|
|
|
|
|
|
|
|
# Find the minimum max allowed length among all specified db_aliases.
|
2017-05-28 07:08:46 +08:00
|
|
|
for db in settings.DATABASES:
|
2014-06-10 20:04:19 +08:00
|
|
|
# skip databases where the model won't be created
|
2015-02-19 15:27:58 +08:00
|
|
|
if not router.allow_migrate_model(db, cls):
|
2014-06-10 20:04:19 +08:00
|
|
|
continue
|
|
|
|
connection = connections[db]
|
|
|
|
max_name_length = connection.ops.max_name_length()
|
2014-07-15 00:23:57 +08:00
|
|
|
if max_name_length is None or connection.features.truncates_names:
|
2014-06-10 20:04:19 +08:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if allowed_len is None:
|
|
|
|
allowed_len = max_name_length
|
|
|
|
db_alias = db
|
|
|
|
elif max_name_length < allowed_len:
|
|
|
|
allowed_len = max_name_length
|
|
|
|
db_alias = db
|
|
|
|
|
|
|
|
if allowed_len is None:
|
|
|
|
return errors
|
|
|
|
|
|
|
|
for f in cls._meta.local_fields:
|
|
|
|
_, column_name = f.get_attname_column()
|
|
|
|
|
|
|
|
# Check if auto-generated name for the field is too long
|
|
|
|
# for the database.
|
2016-04-04 08:37:32 +08:00
|
|
|
if f.db_column is None and column_name is not None and len(column_name) > allowed_len:
|
2014-06-10 20:04:19 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
'Autogenerated column name too long for field "%s". '
|
|
|
|
'Maximum length is "%s" for database "%s".'
|
|
|
|
% (column_name, allowed_len, db_alias),
|
|
|
|
hint="Set the column name manually using 'db_column'.",
|
|
|
|
obj=cls,
|
|
|
|
id='models.E018',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
for f in cls._meta.local_many_to_many:
|
2016-06-30 19:22:10 +08:00
|
|
|
# Skip nonexistent models.
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(f.remote_field.through, str):
|
2016-06-30 19:22:10 +08:00
|
|
|
continue
|
|
|
|
|
2014-06-10 20:04:19 +08:00
|
|
|
# Check if auto-generated name for the M2M field is too long
|
|
|
|
# for the database.
|
2015-02-26 22:19:17 +08:00
|
|
|
for m2m in f.remote_field.through._meta.local_fields:
|
2014-06-10 20:04:19 +08:00
|
|
|
_, rel_name = m2m.get_attname_column()
|
2016-04-04 08:37:32 +08:00
|
|
|
if m2m.db_column is None and rel_name is not None and len(rel_name) > allowed_len:
|
2014-06-10 20:04:19 +08:00
|
|
|
errors.append(
|
|
|
|
checks.Error(
|
|
|
|
'Autogenerated column name too long for M2M field '
|
|
|
|
'"%s". Maximum length is "%s" for database "%s".'
|
|
|
|
% (rel_name, allowed_len, db_alias),
|
2016-02-13 00:36:46 +08:00
|
|
|
hint=(
|
|
|
|
"Use 'through' to create a separate model for "
|
|
|
|
"M2M and then set column_name using 'db_column'."
|
|
|
|
),
|
2014-06-10 20:04:19 +08:00
|
|
|
obj=cls,
|
|
|
|
id='models.E019',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
@classmethod
|
|
|
|
def _check_constraints(cls):
|
|
|
|
errors = []
|
|
|
|
for db in settings.DATABASES:
|
|
|
|
if not router.allow_migrate_model(db, cls):
|
|
|
|
continue
|
|
|
|
connection = connections[db]
|
2019-08-10 14:41:18 +08:00
|
|
|
if (
|
|
|
|
connection.features.supports_table_check_constraints or
|
|
|
|
'supports_table_check_constraints' in cls._meta.required_db_features
|
|
|
|
):
|
2016-11-05 21:12:12 +08:00
|
|
|
continue
|
|
|
|
if any(isinstance(constraint, CheckConstraint) for constraint in cls._meta.constraints):
|
|
|
|
errors.append(
|
|
|
|
checks.Warning(
|
|
|
|
'%s does not support check constraints.' % connection.display_name,
|
|
|
|
hint=(
|
|
|
|
"A constraint won't be created. Silence this "
|
|
|
|
"warning if you don't care about it."
|
|
|
|
),
|
|
|
|
obj=cls,
|
|
|
|
id='models.W027',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return errors
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
############################################
|
|
|
|
# HELPER FUNCTIONS (CURRIED MODEL METHODS) #
|
|
|
|
############################################
|
|
|
|
|
|
|
|
# ORDERING METHODS #########################
|
|
|
|
|
2017-09-07 01:11:18 +08:00
|
|
|
def method_set_order(self, ordered_obj, id_list, using=None):
|
2009-12-22 23:18:51 +08:00
|
|
|
if using is None:
|
|
|
|
using = DEFAULT_DB_ALIAS
|
2015-03-27 00:52:11 +08:00
|
|
|
order_wrt = ordered_obj._meta.order_with_respect_to
|
|
|
|
filter_args = order_wrt.get_forward_related_filter(self)
|
2019-01-17 11:56:06 +08:00
|
|
|
ordered_obj.objects.db_manager(using).filter(**filter_args).bulk_update([
|
|
|
|
ordered_obj(pk=pk, _order=order) for order, pk in enumerate(id_list)
|
|
|
|
], ['_order'])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2017-09-07 01:11:18 +08:00
|
|
|
def method_get_order(self, ordered_obj):
|
2015-03-27 00:52:11 +08:00
|
|
|
order_wrt = ordered_obj._meta.order_with_respect_to
|
|
|
|
filter_args = order_wrt.get_forward_related_filter(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
|
|
|
pk_name = ordered_obj._meta.pk.name
|
2015-03-27 00:52:11 +08:00
|
|
|
return ordered_obj.objects.filter(**filter_args).values_list(pk_name, flat=True)
|
|
|
|
|
|
|
|
|
|
|
|
def make_foreign_order_accessors(model, related_model):
|
|
|
|
setattr(
|
|
|
|
related_model,
|
|
|
|
'get_%s_order' % model.__name__.lower(),
|
2017-09-07 01:11:18 +08:00
|
|
|
partialmethod(method_get_order, model)
|
2015-03-27 00:52:11 +08:00
|
|
|
)
|
|
|
|
setattr(
|
|
|
|
related_model,
|
|
|
|
'set_%s_order' % model.__name__.lower(),
|
2017-09-07 01:11:18 +08:00
|
|
|
partialmethod(method_set_order, model)
|
2015-03-27 00:52:11 +08:00
|
|
|
)
|
2008-07-09 05:53:38 +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
|
|
|
########
|
|
|
|
# MISC #
|
|
|
|
########
|
|
|
|
|
|
|
|
|
2016-02-02 17:33:09 +08:00
|
|
|
def model_unpickle(model_id):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Used to unpickle Model subclasses with deferred fields."""
|
2013-05-21 17:57:24 +08:00
|
|
|
if isinstance(model_id, tuple):
|
2013-12-24 19:25:17 +08:00
|
|
|
model = apps.get_model(*model_id)
|
2013-05-21 17:57:24 +08:00
|
|
|
else:
|
|
|
|
# Backwards compat - the model was cached directly in earlier versions.
|
|
|
|
model = model_id
|
2016-02-02 17:33:09 +08:00
|
|
|
return model.__new__(model)
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2009-03-19 17:06:04 +08:00
|
|
|
model_unpickle.__safe_for_unpickle__ = True
|