Takes the current DateTime and adds either days, hours, minutes, or seconds to the datetime object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class DateTimeFluxCapacitor(models.DateTimeField):
def __init__(self,verbose_name = None, name = None, flux_day = 0, flux_hour = 0, flux_minute = 0, flux_second = 0 , **kwargs):
self.flux_day , self.flux_hour , self.flux_minute , self.flux_second = flux_day , flux_hour , flux_minute , flux_second
if self.flux_day or self.flux_hour or self.flux_minute or self.flux_second:
kwargs['blank'] = True
kwargs['editable'] = False
super(DateTimeFluxCapacitor,self).__init__(verbose_name,name,False,False)
def pre_save(self,model_instance,add):
if self.flux_day or self.flux_hour or self.flux_minute or self.flux_second:
value = datetime.datetime.now() + datetime.timedelta(days = self.flux_day , hours = self.flux_hour , minutes = self.flux_minute , seconds = self.flux_second )
setattr(model_instance,self.attname,value)
return value
else:
return super(DateTimeFluxCapacitor,self).pre_save(model_instance,add)
def get_internal_type(self):
return "DateTimeField"
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.