Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#12276 closed (wontfix)

Adding a UnixDateTimeField to Models

Reported by: nemesys Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: Keywords: unix timestamp datetime
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Hi there,

Beside the normal DateTime fields in databases, there are often legacy systems working with unix timestamps as DateTime. Having a UnixDateTimeField helps setting the Unix Timestamps over the comfortable Calender and Date choosing helper in the Django Admin. This is a preview what a UnixDateTimeField might look like:

### Definition:

class UnixDateTimeField(models.DateTimeField):

    __metaclass__ = models.SubfieldBase

    def get_internal_type(self):
        return 'PositiveIntegerField'

    def to_python(self, value):
        if value is None or isinstance(value, datetime):
            return value
        if isinstance(value, date):
            return datetime(value.year, value.month,
        return datetime.fromtimestamp( float(value) )

    def get_db_prep_value(self, value):
        return int( time.mktime( value.timetuple() ) )

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.to_python(value).strftime('%Y-%m-%d %H:%M:%S')

### Usage:
class Downtime(models.Model):
    id = models.AutoField(primary_key=True, db_column='id') 
    service = models.ForeignKey(Service, db_column='id') 
    begin = UnixDateTimeField(db_column='begin')

Change History (4)

comment:1 Changed 7 years ago by nemesys

  • milestone 1.2 deleted
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from new to closed

This really doesn't feel like something that needs to be in core, and it's easy enough to write and package up your own field for distribution.

comment:3 Changed 7 years ago by nemesys

Probably the same reason why USStateField shouldnt be in core, hm? Actually the UnixTimestampField has even a better reason to be in there as for the USStateField, given that lots of legacy databases and mysql perform faster on these fields than DateTime crap.

comment:4 Changed 7 years ago by SmileyChris

You're speaking to a brick wall here, nemesys. Bring it up in the django-dev google group if you're really passionate about it.

Note: See TracTickets for help on using tickets.
Back to Top