1

Story BackGround:

I want to store json object in django mysql database by using python programming language. But I got an error.

Configuration:

  • OS: Ubuntu 12.04 64bits
  • Program language: python
  • Framework: django
  • database: mysql
  • objects need to be store: json

Code Reference:

json_objects={ u'name': u'REF_1', u'text': u'it is apple', u'time': u'20131210T120039Z'}

Code:

--> models.py

from django.db import models
class Messages(models.Model):
        id           = models.AutoField(primary_key=True)
        received     = models.DateTimeField(null=False)
        name         = models.TextField()
        data         = models.TextField()
        text         = models.TextField()
        word         = models.TextField()

        def __unicode__(self):
            return self.text

--> views.py

from threechannels.models import Messages
data_send_to_db=Messages.objects.create(received=datetime.strptime(json_objects["time"], "%Y%m%dT%H%M%SZ"),name=json_objects_post["name"],data=json_objects_dumps,text=json_objects["text"],word=related_words_hits)

The error I got:

DatabaseError: (1054, "Unknown column 'data' in 'field list'")

If I change the json_objects as objects=json.dumps(json_objects), I got another error which is: string indices must be a integer not a string. I think json_objects_post["name"] will cause this problem.

Any Tips??? Thank you so much.

2
  • djangopackages.com/grids/g/json-fields Commented Jan 6, 2014 at 15:42
  • is your database schema up to date? (have you run syncdb since you added the data field) ? Commented Jan 6, 2014 at 15:46

2 Answers 2

1

To me it seems your json_objects is a simple python dictionary. So this is a valid operation:

received=datetime.strptime(json_objects["time"], "%Y%m%dT%H%M%SZ")

But this one is not:

name=json_objects_post["name"]

I am not sure what you are trying to achieve with the "_post" but you can use the python dictionary the way it is supposed to be used as:

name=json_objects["name"]

Also for the other fields:

data=json_objects

or

data=json.dumps(json_objects)

etc...

Sign up to request clarification or add additional context in comments.

Comments

0

The original code:

data_send_to_db=Messages.objects.create(received=datetime.strptime(json_objects["time"], "%Y%m%dT%H%M%SZ"),name=json_objects_post["name"],data=json_objects_dumps,text=json_objects["text"],word=related_words_hits)

Changes:

data_send_to_db=Messages(received=datetime.strptime(json_objects["time"], "%Y%m%dT%H%M%SZ"),name=json_objects_post["name"],data=json_objects_dumps,text=json_objects["text"],word=related_words_hits)

And It works......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.