Login

Custom model field for mysql time type.

Author:
xuqingkuang
Posted:
October 23, 2009
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

Django does not have a suitable model field can process time type in mysql, the DateTimeField built-in django can not process more than 24 hours. That's why the code born.

Simply usage `from myapp.models import TimeAsTimeDeltaField, SECONDS_PER_MIN, SECONDS_PER_HOUR, SECONDS_PER_DAY

class EstimatedTime: days = None hours = None minutes = None seconds = None

class Case(models.Model): estimated_time = TimeAsTimeDeltaField(null=True, blank=True)

def get_estimated_time(self):
    estimated_time = EstimatedTime()
    if self.estimated_time:
        total_seconds = self.estimated_time.seconds + (self.estimated_time.days * SECONDS_PER_DAY)

        days = total_seconds / SECONDS_PER_DAY
        hours = total_seconds / SECONDS_PER_HOUR - days * 24
        minutes = total_seconds / SECONDS_PER_MIN - hours * 60 - days * 24 * 60
        seconds = total_seconds % SECONDS_PER_MIN

        estimated_time.days = days
        estimated_time.hours = hours
        estimated_time.minutes = minutes
        estimated_time.seconds = seconds

        return estimated_time
    else:
        return estimated_time
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import datetime

from django.db import models
from django.db.backends.mysql.base import django_conversions
from django.conf import settings
from MySQLdb.constants import FIELD_TYPE

django_conversions.update({FIELD_TYPE.TIME: None})

SECONDS_PER_MIN = 60
SECONDS_PER_HOUR = SECONDS_PER_MIN * 60
SECONDS_PER_DAY = SECONDS_PER_HOUR * 24

class TimeAsTimeDeltaField(models.Field):
    """
    Custom field for mapping TIME columns to timedelta values, not times,
    so that we can store values greater than 24 hours.
    See ticket #354 and http://docs.djangoproject.com/en/1.0/howto/custom-model-fields/#howto-custom-model-fields
    """
    __metaclass__ = models.SubfieldBase
    
    def db_type(self):
        return 'time'
    
    def to_python(self, value):
        if value is None:
            return None
        
        if isinstance(value, datetime.timedelta):
            return value
        
        hours, minutes, seconds = map(int, value.split(':'))
        total_seconds = seconds + (60 * (minutes + (60 * hours)))
        result = datetime.timedelta(seconds=total_seconds)
        return result
    
    def get_db_prep_value(self, value):
        if not value:
            return None
        
        total_seconds = value.seconds + (value.days * SECONDS_PER_DAY)
        return '%02i:%02i:%02i' % (
            total_seconds / SECONDS_PER_HOUR, # hours
            total_seconds / SECONDS_PER_MIN - total_seconds / SECONDS_PER_HOUR * 60, # minutes - Total seconds subtract the used hours
            total_seconds % SECONDS_PER_MIN # seconds
        )

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.