How to make a PUT/PATCH request with ListField field in Django-REST?

Viewed 195

I am trying to build a rest-api for a movie-review website. The movie model contains a cast-field which is a list-field, when using ModelViewSets one can't POST ListFields through HTML, so I set blank = true for all list-fields thinking that I'll make a raw PATCH request to update the blank fields, but I am unable to do so.

models.py

class Movie(models.Model):
    movie_name = models.CharField(max_length = 100, unique = True)
    release_date = models.DateField(blank = True)
    description = models.TextField(max_length = 500)
    movie_poster = models.ImageField(blank = True)
    directors = ListCharField(
        base_field = models.CharField(max_length = 500),
        max_length = 6 * 500,
        blank = True
        )
    trailer_url = models.URLField()
    cast = ListCharField(
        base_field = models.CharField(max_length = 225),
        max_length = 11 * 225,
        blank = True
        )
    genre = ListCharField(
        base_field = models.CharField(max_length = 225),
        max_length = 11 * 255,
        blank = True
        )
    avg_rating = models.FloatField(validators = [MinValueValidator(0), MaxValueValidator(5)])
    country = models.CharField(max_length = 100)
    language = models.CharField(max_length = 100)
    budget = models.BigIntegerField(blank = True)
    revenue = models.BigIntegerField(blank = True)
    runtime = models.DurationField(blank = True)

Serializer

class MovieSerializer(ModelSerializer):
   cast = ListField(
       child = CharField(required = False), required = False,
       min_length = 0
   )
   genre = ListField(
       child = CharField(required = False), required = False,
       min_length = 0
   )

   directors = ListField(
           child = CharField(required = False), required = False,
           min_length = 0
       )
   class Meta:
       model = Movie
       fields = '__all__'

I used djano-mysql for adding the ListCharField field-type.

1 Answers

Original put request which resulted in an error response

The request had some trailing commas, due to which the API expected more values.

Here's the correct request-content -

{
    "cast": [
        "aamir",
        "sakshi"
    ],
    "genre": [
        "biopic"
    ],
    "directors": [
        "nitesh tiwari"
    ]
}
Related