0

I have following django model

class CalculatedResults(models.Model):
 id = models.IntegerField(primary_key=True)
 tmstamp = models.DateTimeField()
 latitude = models.FloatField()
 longitude = models.FloatField()
 magnitude = models.FloatField()
 origin_time = models.DateTimeField()
 actioncode = models.IntegerField(db_column='actionCode', blank=True, null=True) # Field name made lowercase.
 class Meta:
     managed = False
     db_table = 'calculated_results'
     verbose_name = 'Calculated Result'
     verbose_name_plural = 'Calculated Results'

and relevant mysql table is :

CREATE TABLE IF NOT EXISTS `calculated_results` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tmstamp` timestamp(5) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `latitude` double NOT NULL,
  `longitude` double NOT NULL,
  `magnitude` double NOT NULL DEFAULT '0',
  `origin_time` timestamp(5) NOT NULL DEFAULT '0000-00-00 00:00:00.00000',
  `actionCode` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `tmstamp_index` (`tmstamp`),
  KEY `origin_time_index` (`origin_time`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=814 ;

When I try to retrieve data from the table using python manage.py shell using following code,

form eew_main.models import CalculatedResults
a = CalculatedResults.objects.all().order_by('-id')[:1]

I get None for tmstamp and origin_time but as far as I have investigated the issue, following sql query is sent to mysql

SELECT  `calculated_results`.`id` ,  `calculated_results`.`tmstamp` ,  `calculated_results`.`latitude` ,  `calculated_results`.`longitude` ,  `calculated_results`.`magnitude` , `calculated_results`.`origin_time` ,  `calculated_results`.`actionCode` 
FROM  `calculated_results` 
ORDER BY  `calculated_results`.`id` DESC 
LIMIT 1

When I try it on mysql with same mysql user that django uses I got all the values.

So please help me to somehow find a way to investigate and resolve this issue.

Thanks!

abyz
  • 156
  • 9
  • 1
    Is it returning `None` for all dates, or just for ones with the zero date `'0000-00-00 00:00:00.00000'`? – Alasdair May 16 '16 at 09:20
  • @Alasdair It returns null for all values not just zero date – abyz May 16 '16 at 10:58
  • @abyz why this model managed status is false ? it forbids django from creating migrations for this table [managed in model](https://docs.djangoproject.com/en/1.10/ref/models/options/) – shalbafzadeh Jan 06 '17 at 11:38
  • @shalbafzadeh because it is a table that is created by another c++ app and is managed by it so managed status is False – abyz Jan 07 '17 at 12:13

1 Answers1

1

Seems your tables are created outside of Django. Django's models.DateTimeField will synthesize into a datetime field in MySQL not timestamp.

Check this answer for an implementation similar to DateTimeField that uses timestamp at SQL level.

Community
  • 1
  • 1
Arman Ordookhani
  • 6,031
  • 28
  • 41
  • Thanks for your help, but dates are generated by other and just read by django. In production server it happens but in dev server nothing goes wrong – abyz May 16 '16 at 19:18