Code

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


Ignore:
Timestamp:
03/24/08 14:47:37 (6 years ago)
Author:
PhiR
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DjangoSpecifications/Core/SingleInstance

    v12 v13  
    108108 * the API for enabling/disabling the feature is very crude and consists only of two class methods. It should at the very least be possible to set the value as a member of the Meta class.  
    109109 * the Model dict should be set threadlocally. 
    110  * it should be possible to force instantiation, because the serializers want that (solved by next item). 
    111  * the internals of the patch should be changed to be less magic: overriding __call__ is a neat trick but I'd rather have something like Model.get_singleton(pk, kwargs = None) and use this where needed (the places are few!). If the user wants to do Model(id = something, kwargs) he should get a fresh instance. If he wanted to have the DB one he'd have used get or something like that.  
    112  * ^--- This is an opinion, and goes against the idea of this patch. 
    113  * For the sake of completeness, it should be possible to do Model.objects.get_fresh_instance(id=something) to bypass the singleton 
     110 * it should be possible to force instantiation, because the serializers want that. 
     111 * For the sake of completeness, it should be possible to do Model.objects.get_from_db(id=something) to bypass the singleton. 
     112 
     113=== Discussion === 
     114The current patch design overrides the __call__ method for the model base which means it magically returns singleton instances when available. This is convenient because XXX (David or Brian: feel free to fill this out). 
     115 
     116Another approach would be to skip the magic and have a simple API along the lines of: 
     117{{{ 
     118class ModelBase: 
     119    def get_singleton(pk, kwargs = None): 
     120        """ returns the current singleton for the instance corresponding to pk. If there isn't one in memory and kwargs is specified, it will be built from kwargs  """ 
     121     
     122}}} 
     123That 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.