How to map to request.post json key to model serialializer field in django?

主宰稳场 提交于 2021-02-08 09:21:48

问题


I am newbie in djnago-rest-framework. I am leaning about creating instance with serializer in DRF.

Let suppose I have models who look like this (models.py) :

from django.db import models

class Reporter(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()


class Article(models.Model):
    headline = models.CharField(max_length=100)
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

My serializer look like this (serializers.py) :

Class AricleSerializer(serializers.ModelSerializer):
    reporter = PkDictField(model_class=Reporter)
    class Meta:
        model = Article
        field = ('headline', 'reporter')

If I post the data like this:

$ curl -H "Content-Type: application/json" -X POST -d '{"headline":"NewHeadline", "reporter":1}' http://localhost:8000/create-article/

In this case everything work fine, It create new entry of article.

But actually I want to rename field reporter to article_reporter

so actual post request look like this

$ curl -H "Content-Type: application/json" -X POST -d '{"headline":"NewHeadline", "article_reporter":1}' http://localhost:8000/create-article/

Yes I understand I can rename key "article_reporter" to "reporter" in views.py before to send data to serializer and It will work fine.

But I want to handle this in serializer class, AricleSerialize. can someone tell me how can I solve this in serializer? Any help would be appreciater.

Thanks


回答1:


I am face the same problem but I handle this problem using Jquery the Serialize return raw (dict of list {[]} ) JSON dataset please have a look my project in Github enter link description here i hope here you can find your problem solution




回答2:


There will be lot of ways to do that,

  1. You can do that by front end javascript or jquery itself.
  2. You can map the value in the Custom function like below

    http://www.django-rest-framework.org/api-guide/viewsets/

  3. You can write custom serializer function to save that data

    http://www.django-rest-framework.org/tutorial/1-serialization/

  4. Orelse you can use Model alias name like this

    https://docs.djangoproject.com/en/2.0/ref/models/fields/#db-column




回答3:


You just need to do this in your serializer:

reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE, source='article_reporter')




回答4:


Although this is an old question, I had a similar issue (sans the differing name field being a foreign key) and this is what solved it for me.

  • First of all, the values in the field attribute of class Meta refer to the API fields and not your model fields.

  • You need to set the source attribute for the fields (whose request.data key differs from the model field name) to map them to your model fields.

  • One of the answers above explicitly declares the reporter field in the serializer but if you do not wish to do that, then a more concise alternative is to specify the source attribute using the extra_kwargs option of Model Serializer as below:

Class AricleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        field = ('headline', 'article_reporter')
        extra_kwargs = {
            "article_reporter": {"source": "reporter"}
        }



来源:https://stackoverflow.com/questions/48162727/how-to-map-to-request-post-json-key-to-model-serialializer-field-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!