L
L
Legalas612021-02-08 17:06:56
Django
Legalas61, 2021-02-08 17:06:56

How to send a QuerySet from the front?

From the front, I pass category IDs for the user. {"specialty":[10,11]}

DRF apparently needs a QuerySet, swears like this

non_field_errors [ "Invalid data. Expected a dictionary, but got int." ]


This is in the serializer
class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = '__all__'

class CreateAccountSerializer(serializers.ModelSerializer):
    specialty = CategorySerializer(many=True)

    class Meta:
        model = Account
        fields = '__all__'


Model:
class Account (models.Model):
specialty = models.ManyToManyField(Catalog, default='')


If I create something like a QuerySet
"specialty":[{"name":"Rally","id":10}]
I am getting this error
Field 'id' expected a number but got {'name': 'Rally', 'id': 10}.

Points to this line
account.specialty.set(self.request.data.get('specialty'))


Changed to this
for i in self.request.data.get('specialty'):
                account.specialty.set(i.get('id')) // 'int' object is not iterable

I get an error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dooMoob, 2021-02-08
@dooMoob

DRF directly says what it needs and what it got instead

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question