0
created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
updated_at = models.DateTimeField(auto_now_add=False, auto_now=True)

My table contains the following fields:

| id | created_at | updated_at | Longitude | Latitude |

I am manually inserting some values in Longitude and Latitude column and hoping created_date and updated_date fields get updated whenever i insert new record. These record are inserted from excel sheet. Simply, I am taking the value from excel and putting into the the table using mysqldb I am able to do this without django database with the following code.

import MySQLdb    #For database operations
import xlrd       #For reading excel file
import time
book = xlrd.open_workbook('my_data.xlsx')  
s1 = book.sheet_by_name('prasad')         
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = 'lmtech123',
                     db = 'server_db')        
cur = db.cursor()                          
query = "insert into eyeway_devicedata(Longitude, Latitude) values(%s, %s)"
for r in range(1,s1.nrows):
    ln = str(s1.cell(r,8).value)
    lt = str(s1.cell(r,7).value)


    values = ln, lt
    cur.execute(query, values)          #insert the data into the table
    db.commit()
    time.sleep(5)    

db.close()

But i want to load the data in django database with its datetime field facility. Is it possible? Please help.

Prasad
  • 1,028
  • 1
  • 11
  • 22
  • Could you please show your models? – Thiago Rossener Jul 24 '17 at 11:41
  • 'class DeviceData(models.Model): created_at = models.DateTimeField(auto_now_add=True, auto_now=False) updated_at = models.DateTimeField(auto_now_add=False, auto_now=True) Longitude = models.CharField(max_length=20) Latitude = models.CharField(max_length=20)' – Prasad Jul 24 '17 at 11:45

1 Answers1

0

Well, all you have to do is creating an instance of your model, fill with data from your sheet and save it.

import xlrd       #For reading excel file
import time

from eyeway.models import DeviceData


book = xlrd.open_workbook('my_data.xlsx')  
s1 = book.sheet_by_name('prasad')     

for r in range(1,s1.nrows):
    device_data = DeviceData(Latitude=str(s1.cell(r,7).value),
                             Longitude=str(s1.cell(r,8).value))
    device_data.save()

Don't forget to set up your MySQL database correctly. You need to put into your settings.py something like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'server_db',
        'USER': 'root',
        'PASSWORD': 'lmtech123',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

I hope it's what you've been looking for.

Thiago Rossener
  • 934
  • 1
  • 6
  • 17
  • when i try to add some data without created_at and updated_at field. i get following error: raise errorclass, errorvalue OperationalError: (1364, "Field 'created_at' doesn't have a default value") – Prasad Jul 24 '17 at 12:40
  • I want it to get saved with correct information of created_at and updated_at fields. and other fields will not get affected. – Prasad Jul 24 '17 at 12:41
  • @Prasad you don't need to be rude if the answer is not what you are expecting. Look, apparently if you put an empty string as the default value of your 'created_at' field you could get over this error. See this question here, it seems to be similar to the error you are getting: https://stackoverflow.com/questions/15438840/mysql-error-1364-field-doesnt-have-a-default-values – Thiago Rossener Jul 24 '17 at 12:50
  • @ Thiago Rossener Hey man. apologies. but i was not getting rude. word might be looking. Please don't be misunderstood. Will try to improve the communication. – Prasad Jul 24 '17 at 13:23
  • I really appreciate you people. Whether my problem got solved or not. That's the community of humanity and coders – Prasad Jul 24 '17 at 13:28
  • @Prasad no problems man, misunderstandings happen. We are all here to help you. Unfortunately, I can't help you more than that, I don't have a MySQL database here to imitate the issue you are dealing with, but I hope you got a little closer to solve it. Good lucky! – Thiago Rossener Jul 24 '17 at 13:34