On Sat, Aug 13, 2011 at 07:22:54AM -0700, Anssi Kääriäinen wrote: > I am trying to speed up Model.__init__, but it seems it is pretty well > optimized already. However, there are still some things that could be > done to speed up it even further. > > Deprecate pre_init and post_init signals. I wonder if these are > actually used in third-party code. These signals are not the easiest > to use, as they get the field values in *args, or in **kwargs, or part > in *args, part in **kwargs. Django core uses them in generic foreign > keys: > > django/contrib/contenttypes/generic.py: > > class GenericForeignKey(object): > def contribute_to_class(self, cls, name): > ... > # For some reason I don't totally understand, using weakrefs > here doesn't work. > signals.pre_init.connect(self.instance_pre_init, sender=cls, > weak=False) > ... > > def instance_pre_init(self, signal, sender, args, kwargs, > **_kwargs): > """ > Handles initializing an object with the generic FK instaed of > content-type/object-id fields. > """ > if self.name in kwargs: > value = kwargs.pop(self.name) > kwargs[self.ct_field] = self.get_content_type(obj=value) > kwargs[self.fk_field] = value._get_pk_val() > > > It is probably possible to fix that use case.
For the record, in my GSoC branch I already removed this particular piece of code and GenericForeignKeys are handled somewhat differently, though I'm still not convinced it is the best way. Michal
signature.asc
Description: Digital signature
