Home  >  Q&A  >  body text

python - Writing Django restful api, there is a field in the model table that contains a foreign key, how can the frontend POST submit data get the value from this foreign key?

Dear seniors, hello everyone.
I am writing an interface for a Django project. I want to release a table called info through the interface, so that other platforms can pass data to the info table through this interface.
But the org field in the info table has a primary and foreign key relationship with another table Org. It can only get values ​​from the Org table and cannot create it by itself. At present, I can access the interface through the URL and view all the information in the info table. However, when submitting the test via POST, the org field cannot be submitted and an error is always reported.
I would like to ask you how to handle this field and how to define the creation method of InfoSerializer in serializer.py.
This is my first time writing restful api. I modified it after learning from it online. Please give me your advice.

The following is my code and api interface display

1.model design

class Org(models.Model):
    name = models.CharField(verbose_name=u"组织单元", max_length=50)
    other = models.CharField(verbose_name=u"备注", max_length=30)
    
class Info(models.Model):
    datetime = models.DateTimeField(verbose_name=u"时间",default=datetime.now)
    name = models.CharField(verbose_name=u"主机名称", max_length=50)
    ip = models.CharField(verbose_name=u"主机地址", max_length=20)
    org = models.ForeignKey(Org, verbose_name=u"所属单元")
    desc = models.CharField(verbose_name=u"故障描述", max_length=200)
    type = models.CharField(verbose_name=u"故障等级", choices=((u"严重",u"严重"),(u"告警",u"告警"),(u"提醒",u"提醒")), max_length=2)

2.serializers.py

class OrgSerializer(serializers.ModelSerializer):
    class Meta:
        model = Org
        fields = ('id','name')


class InfoSerializer(serializers.ModelSerializer):
    org = OrgSerializer()

    class Meta:
        model = Info
        fields = ('id','name','ip','org','desc','type')
        read_only_fields = ('org',)

    def create(self, validated_data):
        validated_data['org'] = self.context['request'].org
        return Info.objects.create(**validated_data)

3.views.py

class OrgViewSet(viewsets.ModelViewSet):
    queryset = Org.objects.all()
    serializer_class = OrgSerializer
    permission_classes = (permissions.IsAuthenticated,)

class InfoViewSet(viewsets.ModelViewSet):
    queryset = Info.objects.all()
    serializer_class = InfoSerializer
    permission_classes = (permissions.IsAuthenticated,)

    @detail_route(renderer_classes=[renderers.StaticHTMLRenderer])
    def plaintext(self, request, *args, **kwargs):
        model = self.get_object()
        return Response(repr(model))

PHP中文网PHP中文网2681 days ago705

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:59:29

    In class Meta中添加depth = 1, and then specify the corresponding field name.

    See official documentation for details

    reply
    0
  • Cancelreply