Changes between Version 13 and Version 14 of DjangoSpecifications/Core/SingleInstance


Ignore:
Timestamp:
Mar 25, 2008, 4:44:00 AM (16 years ago)
Author:
David Cramer
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DjangoSpecifications/Core/SingleInstance

    v13 v14  
    122122}}}
    123123That means writing Model(kwargs) would always return a fresh instance and bypass the instance. Since Django's ORM would use the API internally, doing Model.objects.get would (by default, overridable in the manager) return the singleton. The default related manager would also have a new get_from_db method to force retrieving and instantiating from the DB only.
     124
     125== Proposal #2 (The Original Proposal) ==
     126This would always be enabled. You cannot turn it off. If stuff is broken by having this on then it should be broken.
     127
     128This can cause problems with serializers, and things that modify querysets and shouldn't (e.g. .extra). That is the only reason it should be turned off. If those problems are addressed there is no reason to use #17 and to have it enabled 100% of the time.
     129
     130A simple workaround querysets, and this would branch into being able to store .values() as partials as well (this code is untested, but should give a general idea):
     131
     132{{{
     133class SphinxObjectInstance(object):
     134    """
     135    Acts exactly like a normal instance of an object except that
     136    it will handle any special sphinx attributes in a __sphinx class.
     137    """
     138    _reserved_attributes = ('_sphinx', '__instance')
     139   
     140    def __init__(self, instance, attributes={}):
     141        object.__setattr__(self, '__instance', instance)
     142        object.__setattr__(self, '_sphinx', SphinxAttributes(attributes))
     143   
     144    def __setattr__(self, name, value):
     145        if name in _reserved_attributes:
     146            return object.__setattr__(self, name, value)
     147        return setattr(self.instance, name, value)
     148
     149    def __getattr__(self, name, value=None):
     150        if name in _reserved_attributes:
     151            return object.__getattr__(self, name, value)
     152        return getattr(self.instance, name, value)
     153}}}
     154
     155The proposal would also change how extra is used, it should probably be instance._extra or something similar.
     156
     157(The code above is taken from an unpublished update to the django-sphinx package.)
Back to Top