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

03/25/2008 04:44:00 AM (7 years ago)



  • DjangoSpecifications/Core/SingleInstance

    v13 v14  
    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.
     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.
     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.
     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):
     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')
     140    def __init__(self, instance, attributes={}):
     141        object.__setattr__(self, '__instance', instance)
     142        object.__setattr__(self, '_sphinx', SphinxAttributes(attributes))
     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)
     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)
     155The proposal would also change how extra is used, it should probably be instance._extra or something similar.
     157(The code above is taken from an unpublished update to the django-sphinx package.)
Back to Top