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:

['http://localhost:8000/api/v2/toppings/3/', 
'http://localhost:8000/api/v2/toppings/2/']

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.

The reason seems to be that the input is not properly parsed into a list of 
two elements but it ends up being a list with a single item which is the 
string from above ["[\'http://localhost:8000/api/v2/toppings/3/\', 
\'http://localhost:8000/api/v2/toppings/2/\']"]
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



-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/eacec7a8-fd49-460c-9568-dd489431acfbo%40googlegroups.com.

Reply via email to