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 === |
| 114 | The 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 | |
| 116 | Another approach would be to skip the magic and have a simple API along the lines of: |
| 117 | {{{ |
| 118 | class 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 | }}} |
| 123 | That 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. |