Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
import copy
|
|
|
|
import types
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
from itertools import izip
|
2008-07-09 05:53:38 +08:00
|
|
|
try:
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set # Python 2.3 fallback.
|
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
|
|
|
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
import django.db.models.manager # Imported to register signal handler.
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError
|
2008-08-09 04:59:02 +08:00
|
|
|
from django.db.models.fields import AutoField
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField
|
2008-06-22 04:57:05 +08:00
|
|
|
from django.db.models.query import delete_objects, Q, CollectedObjects
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.db.models.options import Options
|
2008-08-10 01:19:23 +08:00
|
|
|
from django.db import connection, transaction, DatabaseError
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db.models import signals
|
2006-06-27 00:20:58 +08:00
|
|
|
from django.db.models.loading import register_models, get_model
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.utils.functional import curry
|
2007-09-15 02:36:04 +08:00
|
|
|
from django.utils.encoding import smart_str, force_unicode, smart_unicode
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
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
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class ModelBase(type):
|
2008-07-09 05:53:38 +08:00
|
|
|
"""
|
|
|
|
Metaclass for all models.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
def __new__(cls, name, bases, attrs):
|
2008-07-06 19:55:30 +08:00
|
|
|
super_new = super(ModelBase, cls).__new__
|
|
|
|
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
|
|
|
# If this isn't a subclass of Model, don't do anything special.
|
|
|
|
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__')
|
2008-07-06 19:55:30 +08:00
|
|
|
new_class = super_new(cls, name, bases, {'__module__': module})
|
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)
|
|
|
|
abstract = getattr(attr_meta, 'abstract', False)
|
|
|
|
if not attr_meta:
|
|
|
|
meta = getattr(new_class, 'Meta', None)
|
|
|
|
else:
|
|
|
|
meta = attr_meta
|
|
|
|
base_meta = getattr(new_class, '_meta', None)
|
|
|
|
|
2008-06-29 10:35:08 +08:00
|
|
|
if getattr(meta, 'app_label', None) is None:
|
|
|
|
# Figure out the app_label by looking one level up.
|
|
|
|
# For 'django.contrib.sites.models', this would be 'sites'.
|
|
|
|
model_module = sys.modules[new_class.__module__]
|
|
|
|
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
|
|
|
|
else:
|
|
|
|
kwargs = {}
|
|
|
|
|
|
|
|
new_class.add_to_class('_meta', Options(meta, **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
|
|
|
if not abstract:
|
|
|
|
new_class.add_to_class('DoesNotExist',
|
|
|
|
subclass_exception('DoesNotExist', ObjectDoesNotExist, module))
|
|
|
|
new_class.add_to_class('MultipleObjectsReturned',
|
|
|
|
subclass_exception('MultipleObjectsReturned', MultipleObjectsReturned, module))
|
|
|
|
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
|
|
|
|
|
|
|
|
old_default_mgr = None
|
|
|
|
if getattr(new_class, '_default_manager', None):
|
|
|
|
# We have a parent who set the default manager.
|
|
|
|
if new_class._default_manager.model._meta.abstract:
|
|
|
|
old_default_mgr = new_class._default_manager
|
|
|
|
new_class._default_manager = None
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-07-06 21:25:12 +08:00
|
|
|
# Bail out early if we have already created this class.
|
2006-07-30 05:04:41 +08:00
|
|
|
m = get_model(new_class._meta.app_label, name, False)
|
2006-07-06 21:25:12 +08:00
|
|
|
if m is not None:
|
|
|
|
return m
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Add all attributes to the class.
|
|
|
|
for obj_name, obj in attrs.items():
|
|
|
|
new_class.add_to_class(obj_name, obj)
|
|
|
|
|
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
|
|
|
# Do the appropriate setup for any model parents.
|
|
|
|
o2o_map = dict([(f.rel.to, f) for f in new_class._meta.local_fields
|
|
|
|
if isinstance(f, OneToOneField)])
|
|
|
|
for base in parents:
|
|
|
|
if not hasattr(base, '_meta'):
|
|
|
|
# Things without _meta aren't functional models, so they're
|
|
|
|
# uninteresting parents.
|
|
|
|
continue
|
|
|
|
if not base._meta.abstract:
|
|
|
|
if base in o2o_map:
|
|
|
|
field = o2o_map[base]
|
|
|
|
field.primary_key = True
|
|
|
|
new_class._meta.setup_pk(field)
|
|
|
|
else:
|
|
|
|
attr_name = '%s_ptr' % base._meta.module_name
|
|
|
|
field = OneToOneField(base, name=attr_name,
|
|
|
|
auto_created=True, parent_link=True)
|
|
|
|
new_class.add_to_class(attr_name, field)
|
|
|
|
new_class._meta.parents[base] = field
|
|
|
|
else:
|
|
|
|
# The abstract base class case.
|
|
|
|
names = set([f.name for f in new_class._meta.local_fields + new_class._meta.many_to_many])
|
|
|
|
for field in base._meta.local_fields + base._meta.local_many_to_many:
|
|
|
|
if field.name in names:
|
|
|
|
raise FieldError('Local field %r in class %r clashes with field of similar name from abstract base class %r'
|
|
|
|
% (field.name, name, base.__name__))
|
|
|
|
new_class.add_to_class(field.name, copy.deepcopy(field))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if old_default_mgr and not new_class._default_manager:
|
|
|
|
new_class._default_manager = old_default_mgr._copy_to_model(new_class)
|
2006-05-02 09:31:56 +08:00
|
|
|
new_class._prepare()
|
|
|
|
register_models(new_class._meta.app_label, 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
|
|
|
|
2006-07-04 14:44:01 +08:00
|
|
|
# Because of the way imports happen (recursively), we may or may not be
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# the first time this model tries to register with the framework. There
|
|
|
|
# should only be one class for each model, so we always return the
|
2006-07-04 14:44:01 +08:00
|
|
|
# registered version.
|
2006-07-30 05:04:41 +08:00
|
|
|
return get_model(new_class._meta.app_label, name, False)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-02-08 20:01:23 +08:00
|
|
|
def add_to_class(cls, name, value):
|
|
|
|
if hasattr(value, 'contribute_to_class'):
|
|
|
|
value.contribute_to_class(cls, name)
|
|
|
|
else:
|
|
|
|
setattr(cls, name, value)
|
|
|
|
|
|
|
|
def _prepare(cls):
|
2008-07-09 05:53:38 +08:00
|
|
|
"""
|
|
|
|
Creates 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:
|
|
|
|
cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
|
|
|
|
cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
|
|
|
|
setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
|
|
|
|
setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
|
|
|
|
|
|
|
|
# Give the class a docstring -- its definition.
|
|
|
|
if cls.__doc__ is None:
|
|
|
|
cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
|
|
|
|
|
|
|
|
if hasattr(cls, 'get_absolute_url'):
|
|
|
|
cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
|
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.class_prepared.send(sender=cls)
|
2008-02-08 20:01:23 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Model(object):
|
|
|
|
__metaclass__ = ModelBase
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
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.
|
2007-02-26 14:16:19 +08:00
|
|
|
args_len = len(args)
|
|
|
|
if args_len > len(self._meta.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
|
|
|
|
|
|
|
fields_iter = iter(self._meta.fields)
|
|
|
|
if not kwargs:
|
2007-02-26 14:21:24 +08:00
|
|
|
# The ordering of the izip calls matter - izip throws StopIteration
|
|
|
|
# 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.
|
2007-02-26 14:16:19 +08:00
|
|
|
for val, field in izip(args, fields_iter):
|
|
|
|
setattr(self, field.attname, val)
|
|
|
|
else:
|
2007-02-26 14:21:24 +08:00
|
|
|
# Slower, kwargs-ready version.
|
2007-02-26 14:16:19 +08:00
|
|
|
for val, field in izip(args, fields_iter):
|
|
|
|
setattr(self, field.attname, val)
|
|
|
|
kwargs.pop(field.name, None)
|
2007-02-26 14:21:24 +08:00
|
|
|
# Maintain compatibility with existing calls.
|
2007-02-26 14:16:19 +08:00
|
|
|
if isinstance(field.rel, ManyToOneRel):
|
|
|
|
kwargs.pop(field.attname, 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:
|
2008-08-02 07:16:59 +08:00
|
|
|
rel_obj = None
|
2007-02-26 14:16:19 +08:00
|
|
|
if kwargs:
|
|
|
|
if isinstance(field.rel, ManyToOneRel):
|
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)
|
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:
|
2007-02-26 14:21:24 +08:00
|
|
|
# Object instance was passed in. Special case: You can
|
|
|
|
# pass in "None" for related objects if it's allowed.
|
2007-02-26 14:16:19 +08:00
|
|
|
if rel_obj is None and field.null:
|
|
|
|
val = None
|
|
|
|
else:
|
|
|
|
val = kwargs.pop(field.attname, field.get_default())
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2007-02-26 14:16:19 +08:00
|
|
|
val = field.get_default()
|
2008-08-02 07:16:59 +08:00
|
|
|
# If we got 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.
|
|
|
|
if rel_obj:
|
|
|
|
setattr(self, field.name, rel_obj)
|
|
|
|
else:
|
|
|
|
setattr(self, field.attname, val)
|
2007-02-26 14:16:19 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
if kwargs:
|
2007-02-26 14:16:19 +08:00
|
|
|
for prop in kwargs.keys():
|
|
|
|
try:
|
|
|
|
if isinstance(getattr(self.__class__, prop), property):
|
|
|
|
setattr(self, prop, kwargs.pop(prop))
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
if kwargs:
|
|
|
|
raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.post_init.send(sender=self.__class__, instance=self)
|
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 __repr__(self):
|
|
|
|
return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if hasattr(self, '__unicode__'):
|
|
|
|
return force_unicode(self).encode('utf-8')
|
|
|
|
return '%s object' % self.__class__.__name__
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return isinstance(other, self.__class__) and self._get_pk_val() == other._get_pk_val()
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self._get_pk_val())
|
|
|
|
|
|
|
|
def _get_pk_val(self, meta=None):
|
|
|
|
if not meta:
|
|
|
|
meta = self._meta
|
|
|
|
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):
|
|
|
|
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)
|
|
|
|
|
2008-08-10 01:19:23 +08:00
|
|
|
def save(self, force_insert=False, force_update=False):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Saves 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
|
|
|
"""
|
2008-08-10 01:19:23 +08:00
|
|
|
if force_insert and force_update:
|
|
|
|
raise ValueError("Cannot force both insert and updating in "
|
|
|
|
"model saving.")
|
|
|
|
self.save_base(force_insert=force_insert, force_update=force_update)
|
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
|
|
|
|
|
2008-08-10 01:19:23 +08:00
|
|
|
def save_base(self, raw=False, cls=None, force_insert=False,
|
|
|
|
force_update=False):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
|
|
|
Does the heavy-lifting involved in saving. Subclasses shouldn't need to
|
|
|
|
override this method. It's separate from save() in order to hide the
|
|
|
|
need for overrides of save() to pass around internal-only parameters
|
|
|
|
('raw' and 'cls').
|
|
|
|
"""
|
2008-08-10 01:19:23 +08:00
|
|
|
assert not (force_insert and force_update)
|
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 cls:
|
|
|
|
cls = self.__class__
|
|
|
|
meta = self._meta
|
|
|
|
signal = True
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.pre_save.send(sender=self.__class__, instance=self, raw=raw)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
else:
|
|
|
|
meta = cls._meta
|
|
|
|
signal = False
|
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
# If we are in a raw save, save the object exactly as presented.
|
|
|
|
# That means that we don't try to be smart about saving attributes
|
2008-07-09 05:53:38 +08:00
|
|
|
# that might have come from the parent class - we just save the
|
2008-06-09 22:03:35 +08:00
|
|
|
# attributes we have been given to the class we have been given.
|
|
|
|
if not raw:
|
|
|
|
for parent, field in meta.parents.items():
|
2008-07-22 09:10:06 +08:00
|
|
|
# At this point, parent's primary key field may be unknown
|
|
|
|
# (for example, from administration form which doesn't fill
|
|
|
|
# this field). If so, fill it.
|
|
|
|
if getattr(self, parent._meta.pk.attname) is None and getattr(self, field.attname) is not None:
|
|
|
|
setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
|
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
self.save_base(raw, parent)
|
|
|
|
setattr(self, field.attname, self._get_pk_val(parent._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
|
|
|
|
|
|
|
non_pks = [f for f in meta.local_fields if not f.primary_key]
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# First, try an UPDATE. If that doesn't update anything, do an INSERT.
|
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_val = self._get_pk_val(meta)
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
pk_set = pk_val is not None
|
2006-05-02 09:31:56 +08:00
|
|
|
record_exists = True
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
manager = cls._default_manager
|
2007-02-08 06:56:53 +08:00
|
|
|
if pk_set:
|
2006-05-02 09:31:56 +08:00
|
|
|
# Determine whether a record with the primary key already exists.
|
2008-08-10 01:19:23 +08:00
|
|
|
if (force_update or (not force_insert and
|
|
|
|
manager.filter(pk=pk_val).extra(select={'a': 1}).values('a').order_by())):
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# It does already exist, so do an UPDATE.
|
2008-08-10 01:19:23 +08:00
|
|
|
if force_update or non_pks:
|
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
|
|
|
values = [(f, None, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
|
2008-08-10 01:19:23 +08:00
|
|
|
rows = manager.filter(pk=pk_val)._update(values)
|
|
|
|
if force_update and not rows:
|
|
|
|
raise DatabaseError("Forced update did not affect any rows.")
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
record_exists = False
|
2007-02-08 06:56:53 +08:00
|
|
|
if not pk_set or not record_exists:
|
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 pk_set:
|
2008-08-10 01:19:23 +08:00
|
|
|
if force_update:
|
|
|
|
raise ValueError("Cannot force an update in save() with no primary key.")
|
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
|
|
|
values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields if not isinstance(f, AutoField)]
|
|
|
|
else:
|
|
|
|
values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields]
|
|
|
|
|
|
|
|
if meta.order_with_respect_to:
|
|
|
|
field = meta.order_with_respect_to
|
|
|
|
values.append((meta.get_field_by_name('_order')[0], manager.filter(**{field.name: getattr(self, field.attname)}).count()))
|
2007-09-15 17:14:51 +08:00
|
|
|
record_exists = False
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
|
|
|
update_pk = bool(meta.has_auto_field and not pk_set)
|
|
|
|
if values:
|
|
|
|
# Create a new record.
|
|
|
|
result = manager._insert(values, return_id=update_pk)
|
2006-06-08 23:14:06 +08:00
|
|
|
else:
|
|
|
|
# Create a new record with defaults for everything.
|
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
|
|
|
result = manager._insert([(meta.pk, connection.ops.pk_default_value())], return_id=update_pk, raw_values=True)
|
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
|
|
|
if update_pk:
|
|
|
|
setattr(self, meta.pk.attname, result)
|
|
|
|
transaction.commit_unless_managed()
|
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
|
|
|
if signal:
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.post_save.send(sender=self.__class__, instance=self,
|
|
|
|
created=(not record_exists), raw=raw)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-05-13 22:56:47 +08:00
|
|
|
save_base.alters_data = True
|
|
|
|
|
2008-06-22 04:57:05 +08:00
|
|
|
def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Recursively populates seen_objs with all objects related to this
|
|
|
|
object.
|
|
|
|
|
2008-06-22 04:57:05 +08:00
|
|
|
When done, seen_objs.items() will be in the format:
|
|
|
|
[(model_class, {pk_val: obj, pk_val: obj, ...}),
|
2008-07-09 05:53:38 +08:00
|
|
|
(model_class, {pk_val: obj, pk_val: obj, ...}), ...]
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
pk_val = self._get_pk_val()
|
2008-06-22 04:57:05 +08:00
|
|
|
if seen_objs.add(self.__class__, pk_val, self, parent, nullable):
|
2006-05-02 09:31:56 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
for related in self._meta.get_all_related_objects():
|
|
|
|
rel_opts_name = related.get_accessor_name()
|
|
|
|
if isinstance(related.field.rel, OneToOneRel):
|
|
|
|
try:
|
|
|
|
sub_obj = getattr(self, rel_opts_name)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
2008-06-22 04:57:05 +08:00
|
|
|
sub_obj._collect_sub_objects(seen_objs, self.__class__, related.field.null)
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
for sub_obj in getattr(self, rel_opts_name).all():
|
2008-06-22 04:57:05 +08:00
|
|
|
sub_obj._collect_sub_objects(seen_objs, self.__class__, related.field.null)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-06-29 17:41:35 +08:00
|
|
|
# Handle any ancestors (for the model-inheritance case). We do this by
|
|
|
|
# traversing to the most remote parent classes -- those with no parents
|
|
|
|
# themselves -- and then adding those instances to the collection. That
|
|
|
|
# will include all the child instances down to "self".
|
|
|
|
parent_stack = self._meta.parents.values()
|
|
|
|
while parent_stack:
|
|
|
|
link = parent_stack.pop()
|
|
|
|
parent_obj = getattr(self, link.name)
|
|
|
|
if parent_obj._meta.parents:
|
|
|
|
parent_stack.extend(parent_obj._meta.parents.values())
|
|
|
|
continue
|
|
|
|
# At this point, parent_obj is base class (no ancestor models). So
|
|
|
|
# delete it and all its descendents.
|
|
|
|
parent_obj._collect_sub_objects(seen_objs)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def delete(self):
|
|
|
|
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
# Find all the objects than need to be deleted.
|
2008-06-22 04:57:05 +08:00
|
|
|
seen_objs = CollectedObjects()
|
2006-05-02 09:31:56 +08:00
|
|
|
self._collect_sub_objects(seen_objs)
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
# Actually delete the objects.
|
2006-05-02 09:31:56 +08:00
|
|
|
delete_objects(seen_objs)
|
|
|
|
|
|
|
|
delete.alters_data = True
|
|
|
|
|
|
|
|
def _get_FIELD_display(self, field):
|
|
|
|
value = getattr(self, field.attname)
|
2008-07-19 15:53:02 +08:00
|
|
|
return force_unicode(dict(field.flatchoices).get(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):
|
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
|
|
|
op = is_next and 'gt' or 'lt'
|
|
|
|
order = not is_next and '-' or ''
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
param = smart_str(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})
|
|
|
|
q = q|Q(**{field.name: param, 'pk__%s' % op: self.pk})
|
|
|
|
qs = self.__class__._default_manager.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:
|
|
|
|
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):
|
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
|
|
|
qn = connection.ops.quote_name
|
2006-05-02 09:31:56 +08:00
|
|
|
op = is_next and '>' or '<'
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
order = not is_next and '-_order' or '_order'
|
2006-05-02 09:31:56 +08:00
|
|
|
order_field = self._meta.order_with_respect_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
|
|
|
# FIXME: When querysets support nested queries, this can be turned
|
|
|
|
# into a pure queryset operation.
|
2006-05-02 09:31:56 +08:00
|
|
|
where = ['%s %s (SELECT %s FROM %s WHERE %s=%%s)' % \
|
2007-08-20 09:03:33 +08:00
|
|
|
(qn('_order'), op, qn('_order'),
|
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
|
|
|
qn(self._meta.db_table), qn(self._meta.pk.column))]
|
|
|
|
params = [self.pk]
|
|
|
|
obj = self._default_manager.filter(**{order_field.name: getattr(self, order_field.attname)}).extra(where=where, params=params).order_by(order)[:1].get()
|
2006-05-02 09:31:56 +08:00
|
|
|
setattr(self, cachename, obj)
|
|
|
|
return getattr(self, cachename)
|
|
|
|
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
############################################
|
|
|
|
# HELPER FUNCTIONS (CURRIED MODEL METHODS) #
|
|
|
|
############################################
|
|
|
|
|
|
|
|
# ORDERING METHODS #########################
|
|
|
|
|
|
|
|
def method_set_order(ordered_obj, self, id_list):
|
2006-06-28 09:16:32 +08:00
|
|
|
rel_val = getattr(self, ordered_obj._meta.order_with_respect_to.rel.field_name)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
order_name = ordered_obj._meta.order_with_respect_to.name
|
|
|
|
# FIXME: It would be nice if there was an "update many" version of update
|
|
|
|
# for situations like this.
|
|
|
|
for i, j in enumerate(id_list):
|
|
|
|
ordered_obj.objects.filter(**{'pk': j, order_name: rel_val}).update(_order=i)
|
2006-05-02 09:31:56 +08:00
|
|
|
transaction.commit_unless_managed()
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def method_get_order(ordered_obj, self):
|
|
|
|
rel_val = getattr(self, ordered_obj._meta.order_with_respect_to.rel.field_name)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
order_name = ordered_obj._meta.order_with_respect_to.name
|
|
|
|
pk_name = ordered_obj._meta.pk.name
|
|
|
|
return [r[pk_name] for r in
|
|
|
|
ordered_obj.objects.filter(**{order_name: rel_val}).values(pk_name)]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
##############################################
|
|
|
|
# HELPER FUNCTIONS (CURRIED MODEL FUNCTIONS) #
|
|
|
|
##############################################
|
|
|
|
|
2007-11-30 00:32:09 +08:00
|
|
|
def get_absolute_url(opts, func, self, *args, **kwargs):
|
|
|
|
return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **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
|
|
|
|
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 #
|
|
|
|
########
|
|
|
|
|
|
|
|
class Empty(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
if sys.version_info < (2, 5):
|
|
|
|
# Prior to Python 2.5, Exception was an old-style class
|
|
|
|
def subclass_exception(name, parent, unused):
|
|
|
|
return types.ClassType(name, (parent,), {})
|
|
|
|
else:
|
|
|
|
def subclass_exception(name, parent, module):
|
|
|
|
return type(name, (parent,), {'__module__': module})
|