0

My models.py is

class Genre(models.Model):
    genre = models.CharField(max_length=200)
    def __str__(self):
        return self.genre


class Movies(models.Model):
    popularity=models.DecimalField(max_digits=5, decimal_places=2)
    director = models.CharField(max_length=200)
    genre = models.ManyToManyField(Genre, blank=True, null=True)
    score= models.DecimalField(max_digits=5, decimal_places=2)
    name = models.CharField(max_length=200)
    def __str__(self):
        return self.name

Here is my Serializer and viewset

# Serializers define the API representation.
class MoviesSerializer(serializers.ModelSerializer):
    #genre =  serializers.StringRelatedField(many=True)
    genre =  serializers.SlugRelatedField(queryset=Genre.objects.all(),many=True,slug_field='genre' )
    class Meta:
        model = Movies
        fields = ('popularity', 'director', 'genre', 'score','name')

# ViewSets define the view behavior.
class MovieViewSet(viewsets.ModelViewSet):
    queryset = Movies.objects.all()
    serializer_class = MoviesSerializer

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'movies', MovieViewSet)

I am able to see the HTML POST form while logged in as Superuser. But while submitting HTML POST form getting error message This field is required. What is the error here in the code. Thanks for help in Advance.

enter image description hereenter image description here

5
  • are you sure that your Movie object has Genre object? Nov 22, 2015 at 10:29
  • How to check that. I am able to select one or many Genre object while creating a new Movie in django admin section
    – curiousguy
    Nov 22, 2015 at 13:56
  • Also for rest api call on a movie I am getting the Genre objects that I have selected. [ { "popularity": "1.89", "director": "Dib", "genre": [ "test1", "test2" ], "score": "2.80", "name": "Test Movie" } ]
    – curiousguy
    Nov 22, 2015 at 14:00
  • could you add a screenshots or the complete error message. I don't understand "But for Genre dropdown I cant see any options. Getting msg No items to select. " where is this dropdown? add template, screenshots to help us to understand please Nov 22, 2015 at 14:02
  • I have added screenshots . For the second screen HTML POST form see the Genre dropdown No Item to Select
    – curiousguy
    Nov 22, 2015 at 14:15

1 Answer 1

1

You should use a SlugRelatedField instead of the StringRelatedField for that.

4
  • I have already tried that genre = serializers.SlugRelatedField(many=True,read_only=True,slug_field='genre' ) But for this even the Genre drop down field is not coming in the HTML POST Form
    – curiousguy
    Nov 22, 2015 at 13:53
  • 1
    Your line won't work with read_only=True Set the query set instead
    – Linovia
    Nov 22, 2015 at 21:37
  • Also now I am not able to create a new movie using POST FORM . Getting validation error This field is required. for all fields after submitting. Though I have provided values for all the filed .
    – curiousguy
    Nov 23, 2015 at 18:30
  • You likely are using DRF 3.3.0 which has a bug. If this is you case, upgrade DRF to 3.3.1
    – Linovia
    Nov 24, 2015 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.