On 5/18/07, Robert Coup <[EMAIL PROTECTED]> wrote:
> What about having a _first_set attribute on the proxy object that only
> allows laziness for the first time the value is set (ie. when it is set
> from the DB row)? Then any later set()s get live validation and coercion
> to geos objects.
I submitted a new patch on #3982 that does just this, incorporating a
bit of your work on lazy instantiation, and making it more generic.
Basically, fields would have a simple way to create and manage lazy
attributes. I've already adapted DurationField to use this new
technique (#2443), and I've started work on getting FileField and
ImageField to use it as well. It'll be a bit of time before I can say
whether or not those will work properly, but it's looking promising.
The generic system I put in on #3982 allows fields to specify a class
that would be instantiated, as well as a separate function that would
handle the instantiation, and a function to be called after the object
is instantiated. The separate "creation" function is necessary in
order to have a helper funciton perform some extra trickery, but still
return an instance of the provided class. DurationField uses this
helper function to pass the value as seconds to timedelta, since
seconds are the second positional argument, not the first.
For your GeometryField, both the creation function and the init
function would be specified, since you need special handling for each
case. From what I read in your most recent patch, here's the basics of
how it could work for you.
class GeometryField(Field):
...
def contribute_to_class(self, cls, name):
super(GeometryField, self).contribute_to_class(cls, name)
setattr(cls, name, self.lazy_attribute(GEOSGeometry,
self.create, self.set_srid)
def create(self, value):
try:
return GEOSGeometry(value, "hex")
except GEOSException:
return GEOSGeometry(value, "wkt")
def set_srid(self, geom):
if geom.srid is None and self.srid is not None:
geom.set_srid(self.srid)
As you can see, it would greatly simplify the code necessary to
implement lazy instantiation of your field attributes.
I'm not tied to the self.lazy_attribute(cls, ...) syntax. It would be
just as easy to use AttributeProxy(self, cls, ...), it would just mean
that modules that will be using it would have to import AttributeProxy
as well as Field. Let the discussion ensue.
-Gul
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---