Hello everybody,
I am struggling getting the following to work. I have a model with a ManyToMany realationship and I want it to be updateable through the Web API of the DjangoRestFramework.
class Topping(models.Model):
name = models.CharField(max_length=255)
class Pizza(models.Model):
name = models.CharField(max_length=255)
creator = models.ForeignKey(User)
toppings = ManyToManyField(Topping, related_name='pizzas', blank=True)
My Pizza serializer looks like this:
class PizzaSerializer(ExpanderSerializerMixin, serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField(read_only=True)
topping_queryset = models.Topping.objects.all()
toppings = serializers.HyperlinkedRelatedField(queryset=topping_queryset, view_name='topping-detail', style={'base_template': 'input.html'}, many=True)
"""
Create and return a new `Pizza` instance
"""
def create(self, validated_data):
return models.Pizza.objects.create(**validated_data, creator=self.context['request'].user)
class Meta:
model = models.Pizza
fields = '__all__'
read_only_fields = ('creator',)
expandable_fields = {
'creator': UserSerializer,
'toppings': (ToppingSerializer, (), {'many': True},),
}
Displaying the pizzas and their toppings works fine but when I e.g. want to update an existing pizza the toppings field in the browser is displayed like this:
Which in my opinion looks fine. But it ends up with the error message ""Invalid hyperlink - No URL match."" when I send a PUT request.
It seems that the request parsing happens in django/http/requests.py and it would expect multiple toppings entries instead of a single one as list.
What would I need to change so that the list is properly parsed?
The versions i am currently running are:
Django==1.11.29
djangorestframework==3.11.0
djangorestframework-expander==0.2.3
Thanks a million, I am really stuck by now.
Fabian