如何在django-rest中自定义Serializer

时间:2014-04-04 10:51:44

标签: python django django-rest-framework

我想自定义序列化程序,以便在创建(发布)质询时发布challenged_to字段的数据。

我有模特课:

class Challenge(models.Model):
    """
    """

    name = models.CharField(max_length=60)
    app = models.ForeignKey(App)
    challenged_to = models.ManyToManyField(Student, through='ChallengeStudents')
    challenged_by = models.ForeignKey(Student, related_name='challenge_created_by')
    points = models.PositiveSmallIntegerField()

    def __unicode__(self):
        return "%d - %s" % (self.id, self.name)

和ChallengeStudents课程:

class ChallengeStudents(models.Model):
    """
    """

    CHALLENGE_STATUS_CHOICES = (
            (1, 'Pending'),
            (2, 'Accepted'),
            (3, 'Rejected'),
    )
    challenge = models.ForeignKey(Challenge)
    student = models.ForeignKey(Student)
    status = models.PositiveSmallIntegerField(max_length=1, choices=CHALLENGE_STATUS_CHOICES, default=1)

    def __unicode__(self):
        return "%s - %s" % (self.challenge, self.student)

    class Meta:
        verbose_name_plural = "Challenged students"

序列化器:

class ChallengeSerializer(serializers.HyperlinkedModelSerializer):
    """
    """
    #challenged_to = want to custmize it

    class Meta:
        model = Challenge

在这种情况下,我获得了一个表格,用于发布挑战模型的数据,除了challenged_to字段。 如何自定义它以便我可以在表单中获得challenged_to字段。

0 个答案:

没有答案