如何在DjangoRestFramework [Python3.6]中的模型表上接受数组有效载荷

时间:2018-08-27 18:29:58

标签: django python-3.x django-rest-framework

运行以下代码时,出现一个'create'方法未实现的错误。。如何在没有申请人字段的模型上创建create方法。处理此问题的最佳方法是什么?

有效载荷数据(如POSTMAN中一样):

    {
      "applicants": [{"id": 1, "status": 2}, {"id": 12, "status": 3}, 
      {"id": 11, "status": 2}]
    }

我的serializers.py文件:

class StringListField(serializers.ListField):
    applicants = serializers.CharField

class UpdateApplicantSerializer(serializers.Serializer):
    applicants = StringListField()

Models.py文件:

class Applicant(models.Model):

    APPLICATION_STATUS = (
      (1, 'Pending'),
      (2, 'Accept'),
      (3, 'Reject'),
    )

    first_name = models.CharField(max_length=200, blank=False, 
    null=False)

    last_name = models.CharField(max_length=200, blank=False, 
    null=False)

    email = models.EmailField(max_length=200, blank=False, null=False, 
    unique=True)

    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', 
    message="Phone number must be entered in the format: '+999999999'. 
    Up to 15 digits allowed.")

    phone_number = models.CharField(validators=[phone_regex], 
    max_length=17, blank=True, null=False, unique=True) # validators 
    should be a list

    linkedin_url = models.URLField(max_length=255, unique=True) #make 
    sure diff users cant use two same profile

    twitter_url = models.URLField(max_length=255, unique=True) #make 
    sure diff users cant use two same profile

    articles = ArrayField(models.URLField(), blank=False, null=False, 
    unique=True, size=3)

    country = models.ForeignKey(Country, on_delete=models.CASCADE, 
    blank=False, related_name="applicant")

    category = models.ForeignKey(Category, on_delete=models.CASCADE, 
    related_name="applicant", blank=False)

    status = models.CharField(max_length=200, 
      choices=APPLICATION_STATUS, default=1)

0 个答案:

没有答案