1

I am trying to save json data as django models instances, I am new to djano-rest-framework

here is my model:


   class objective(models.Model):
      description = models.CharField(max_length=200)
      profile_name = models.CharField(max_length=100)
      pid = models.ForeignKey('personal_info')

serializer.py


   class objective_Serilaizer(serializers.Serializer):
      description = serializers.CharField(max_length=200)
      profile_name = serializers.CharField(max_length=100)
      pid = serializers.IntegerField()

     def restore_object(self, attrs, instance=None):

        if instance:
           instance.description = attrs.get('description', instance.description)
           instance.profile_name = attrs.get('profile_name', instance.profile_name)
           instance.pid = attrs.get('pid', instance.pid)
           return instance
        return objective(**attrs)

json


     {
"objective": {
    "description": "To obtain job focusing in information technology.",
    "profile_name": "Default",
    "id": 1
    }
}

I tried


   >>> stream = StringIO(json) 
   >>> data = JSONParser().parse(stream)

I am getting following error


        raise ParseError('JSON parse error - %s' % six.text_type(exc))
     ParseError: JSON parse error - No JSON object could be decoded

2 Answers 2

1

Use:

objective_Serilaizer(data=json)

or probably because your json is data on the request object:

objective_Serilaizer(data=request.DATA)

Here's a good walk through from the Django Rest-framework docs.

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

3 Comments

` >>> serializer = objective_Serilaizer(data=json)` >>> serializer.is_valid() False >>> serializer.errors {'profile_name': [u'This field is required.'], 'pid': [u'This field is required.'], 'description': [u'This field is required.']} i got this error
its because your have those fields as a nested object within objective. You can certainly have this structure if you want, but it requires a refactor of your code.
currently it expects json like this: { "description": "To obtain job focusing in information technology.", "profile_name": "Default", "id": 1 }. If you use this format with the code above, everything will work fine and dandy.
0

If you are sure your JSON as a string is correct then it should be easy to parse without going to the lengths you currently are:

>>> import json
>>> stream = """\
...      {
... "objective": {
...     "description": "To obtain job focusing in information technology.",
...     "profile_name": "Default",
...     "id": 1
...     }
... }"""
>>> json.loads(stream)
{u'objective': {u'profile_name': u'Default', u'description': u'To obtain job focusing in information technology.', u'id': 1}}

So surely the question is how come you aren't able to parse it. Where is that JSON string you quote actually coming from? Once your JSON object is parsed, you need to address the top-level "objective" key to access the individual data elements in the record you want to create.

2 Comments

He's already using Django rest-framework and it already provides this functionality and even instantiates the django model instance afterwards. Why would be the benefit of just parsing this into a dict?
I was mostly trying to focus on the fact that the quoted JSON is valid, in a way that demonstrates it most graphically.

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.