Django-Rest-Framework可浏览的API未显示' editable = False' POST框中的字段

时间:2016-11-10 00:39:17

标签: python django django-rest-framework

我希望能够通过可浏览的API(django-rest-framework)实例化我的Job实体,但是我的雇主字段包含字段editable=False。因此,雇主输入框不会出现。

这是我在editable = False时看到的内容(下面的代码):
(Image) POST form without employer entry box

这是我删除editable = False时所看到的内容(下面的代码):
(Image) POST form with employer entry box

我怎样才能让雇主框显示,而不允许雇主字段在首次设置后不被更改? (这样看起来就像是第二张图片)

models.py

from django.db import models
from users.models import Proof_User

class Job(models.Model):
    title = models.CharField(max_length=254, blank=False, null=False)
    description = models.TextField(blank=True)
    employer = models.ForeignKey(Proof_User, editable=False, related_name='jobs', blank=False, null=False)

serializers.py

class Job_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Job
        fields = ('id', 'title', 'description', 'employer',)

views.py

class Job_Viewset(viewsets.ModelViewSet):
    queryset = Job.objects.all()
    serializer_class = Job_Serializer

0 个答案:

没有答案