Ver Mensaje Individual
  #6 (permalink)  
Antiguo 26/10/2012, 11:13
PoLiZe
 
Fecha de Ingreso: marzo-2008
Ubicación: Santa Cruz, Argentina
Mensajes: 433
Antigüedad: 16 años, 1 mes
Puntos: 5
Respuesta: Usar Unix Timestamp con DateField

asi es la estructura de la db

Código:
CREATE TABLE IF NOT EXISTS `bandas` (
  `id` int(11) NOT NULL auto_increment,
  `nombre` varchar(64) NOT NULL,
  `bio` mediumtext NOT NULL,
  `urltag` varchar(64) NOT NULL,
  `fecha_creado` text NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `urltag` (`urltag`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=50 ;
y este es mi models.py

Código:
from django.db import models
from datetime import datetime
from time import strftime

class UnixTimestampField(models.DateTimeField):

    def __init__(self, null=False, blank=False, **kwargs):
        super(UnixTimestampField, self).__init__(**kwargs)
        # default for TIMESTAMP is NOT NULL unlike most fields, so we have to
        # cheat a little:
        self.blank, self.isnull = blank, null
        self.null = True # To prevent the framework from shoving in "not null".
        
    def db_type(self):
        typ=['TIMESTAMP']
        # See above!
        if self.isnull:
            typ += ['NULL']
        if self.auto_created:
            typ += ['default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP']
        return ' '.join(typ)
    
    def to_python(self, value):
        return datetime.from_timestamp(value)
    
    def get_db_prep_value(self, value):
        if value==None:
            return None
        return strftime('%Y%m%d%H%M%S',value.timetuple())




class Artista(models.Model):
	nombre = models.CharField(max_length=50)
	bio = models.TextField()
	fecha_creado = UnixTimestampField(null=True, blank=True)
	urltag = models.CharField(max_length=255)

	class Meta:
		db_table = 'bandas'

	def __unicode__(self):
		return self.nombre
Muchas gracias desde ya !