我希望能够通过可浏览的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