I now sort of do what you proposed. I override the init. But for the
attributes that I want to keep track of I create create a property.
Then it's a matter of counting how often it has been accessed. Now I
just need inherit from both Model and DateTimeFieldHelper :)
-----------------------------------
_magic_dt_create_counter = {}
class DateTimeFieldHelper(object):
def __init__(self, *args, **kwargs):
global _magic_dt_create_counter
#print "init me!"
self._magic_dt_fields = {}
super(DateTimeFieldHelper, self).__init__(*args, **kwargs)
c = str(self.__class__)
if c not in _magic_dt_create_counter:
#print "Enhanceing %s", c
_magic_dt_create_counter[c] = 1
opts = self._meta
for f in opts.fields:
if isinstance(f, DateTimeField):
setattr(DateTimeFieldHelper , f.attname,
property(partial(_get_magic_dt_field, dt_field=f.attname),
partial(_set_magic_dt_field, dt_field=f.attname)))
# when supplied via kwargs, set to 1..
opts = self._meta
for f in opts.fields:
if isinstance(f, DateTimeField):
if f.attname in kwargs:
self._magic_dt_fields[f.attname] =
(kwargs[f.attname] ,1)
def _get_magic_dt_field(self, dt_field):
#print "get field: %s" %dt_field
return self._magic_dt_fields[dt_field][0] if dt_field in
self._magic_dt_fields else None
def _set_magic_dt_field(self, val, *args, **kwargs):
dt_field = kwargs["dt_field"]
#print "set field: %s" %dt_field
if dt_field in self._magic_dt_fields:
count = self._magic_dt_fields[dt_field][1] + 1
#print "%s has been set %s times now to %s" %
(dt_field,count,val)
else:
count = 0
self._magic_dt_fields[dt_field] = [val , count]
-----------------------------------
This is my new DateTimeField:
-----------------------------------
class DateTimeField(DateTimeField):
def __init__(self, verbose_name=None, name=None, auto_now=False,
auto_now_add=False, auto_override=False, **kwargs):
self.auto_now, self.auto_now_add, self.auto_override =
auto_now, auto_now_add, auto_override
if (auto_now or auto_now_add) and not auto_override:
kwargs['editable'] = False
kwargs['blank'] = True
Field.__init__(self, verbose_name, name, **kwargs)
def pre_save(self, model_instance, add):
if (not self.auto_override) and (self.auto_now or
(self.auto_now_add and add)):
value = datetime.datetime.now()
elif self.auto_override and (self.auto_now):
# somehow set currenttime if no time has explicitly been
set
# the somehow has been solved... be scared, very
hackish
if self.attname in model_instance._magic_dt_fields and
model_instance._magic_dt_fields[self.attname][1] >= 1:
# value has been explicitly set
#print "Value for %s has been set %s time" %
(self.attname, model_instance._magic_dt_fields[self.attname][1])
value = super(DateField,
self).pre_save(model_instance, add)
else:
# no value is set, use now
value = datetime.datetime.now()
else:
value = super(DateField, self).pre_save(model_instance,
add)
setattr(model_instance, self.attname, value)
return value
-----------------------------------
It all is very hackish, simply forcing myself to set the correct date
on an update would have been simpler :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---