ViewSet中的SlugField不会在JSON Webform中流行

时间:2019-02-19 13:51:55

标签: django-rest-framework

我有一个带有课程和部门模型的Django REST框架应用程序。我有以下模型,序列化器和视图集来表示课程目录。


## MODELS ##

class Department(models.Model):
    """Academic Department""" 
    name = models.CharField(max_length=30, unique=True)

class Course(models.Model):
    """Courses in the course catalog"""

    course_name = models.CharField(max_length=30, unique=True)
    department = models.ForeignKey(Department, on_delete=models.PROTECT)
    course_number = models.DecimalField(decimal_places=0, max_digits=3, unique=True)
    credits = models.IntegerField()

    class Meta:
        unique_together = (("department", "course_number"),)

## SERIALIZERS ##

class DepartmentSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Department
        #fields = ('id', 'last_name', 'first_name')

class CourseSerializer(serializers.ModelSerializer):
    department = serializers.SlugRelatedField(
        many = False,
        read_only = False,
        slug_field = 'name',
        queryset = models.Department.objects.all()
     )

    class Meta:
        model = models.Course
        fields = (
            'id', 
            'course_name', 
            'department',
            'course_number', 
            'credits')

## VIEWSETS ##

class CourseViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows courses to be viewed or edited.
    """
    queryset = models.Course.objects.all().order_by('department__name','course_number')
    serializer_class = serializers.CourseSerializer

课程具有指向部门的外键,并使用SlugRelatedField将课程部门列在结果JSON中作为部门名称:

{
    "count": 5,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 5,
            "course_name": "History of Justin",
            "department": "English",
            "course_number": "344",
            "credits": 3
        },
        {
            "id": 6,
            "course_name": "34",
            "department": "Math",
            "course_number": "4",
            "credits": 4
        },
        {
            "id": 1,
            "course_name": "Pre Calc",
            "department": "Math",
            "course_number": "101",
            "credits": 3
        },
        {
            "id": 2,
            "course_name": "Calc I",
            "department": "Math",
            "course_number": "200",
            "credits": 3
        },
        {
            "id": 3,
            "course_name": "Calc II",
            "department": "Math",
            "course_number": "201",
            "credits": 3
        }
    ]
}

我还可以在POST请求中使用以下正文填充新课程:

{
    "course_name": "Chemistry I",
    "department": "Science",
    "course_number": "200",
    "credits": 3
}

但是,自动生成的表单不会呈现子弹字段文本。

Form

如何更改此表单呈现的内容?

1 个答案:

答案 0 :(得分:1)

您需要向__str__添加Department方法,以告知Django如何表示Department实例:

class Department(models.Model):
    """Academic Department""" 
    name = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return self.name