#How to save ManyToMany field? getting a ` Field 'id' expected a number but got ','.` error

9 messages · Page 1 of 1 (latest)

lethal kiln
#

Hi. I'm trying to save a data to a many-to-many field, but I'm getting this error Direct assignment to the forward side of a many-to-many set is prohibited. Use can_be_used_in_airport.set() instead.
I'm using Django REST Framework and a Vue frontend. From the frontend, I send only the airport and card IDs and then get their instances and save them in the backend.

class CardProgramme(models.Model):
  ...
    can_be_used_in_airport = models.ManyToManyField(to=Airport)
    card = models.ManyToManyField(Card)

This is my CardProgrammeSerializer class:

class CardProgrammeSerializer(serializers.ModelSerializer):
    can_be_used_in_airport = AirportSerializer(read_only=True, many=True)
    card = CardSerializer(read_only=True, many=True)


    def create(self, validated_data):
        airport_ids = self.context['request'].data['can_be_used_in_airport']
        card_ids = self.context['request'].data['card']
        cardProgramme = CardProgramme.objects.create(
            programme_name=validated_data['programme_name'],
            monthly_lounges=validated_data['monthly_lounges'],
            quarterly_lounges=validated_data['quarterly_lounges'],
            half_yearly_lounges=validated_data['half_yearly_lounges'],
            yearly_lounges=validated_data['yearly_lounges'],
            programme_start_date=validated_data['programme_start_date'],
            programme_end_date=validated_data['programme_end_date']
        )

        for airport in airport_ids:
            print(airport)
            airportModelSet = Airport.objects.filter(pk=airport)
            print(airportModelSet)
            cardProgramme.can_be_used_in_airport=(airportModelSet)


        # cardProgramme.save()

        return True

but when I use that, it throws another error
Field 'id' expected a number but got ','.

short junco
#

Can you show the full traceback?

#

You also shouldn't use request data directly, everything should go through a serializer, may be part of the problem

lethal kiln
#

Hi.

#

I tried changing the method

#

instead of directly assigning the values, I'm getting the primary key and then trying to set it

#
    def update(self, instance, validated_data):
        airport_ids = list(self.initial_data['can_be_used_in_airport'].rsplit(","))
        card_ids = list(self.initial_data['card'].rsplit(","))
        instance.programme_name = validated_data['programme_name']
        instance.monthly_lounges = validated_data['monthly_lounges']
        instance.quarterly_lounges = validated_data['quarterly_lounges']
        instance.half_yearly_lounges = validated_data['half_yearly_lounges']
        instance.yearly_lounges = validated_data['yearly_lounges']
        instance.programme_start_date = validated_data['programme_start_date']
        instance.programme_end_date = validated_data['programme_end_date']
        instance.save()

        print(card_ids)
        print(airport_ids)

        if airport_ids:
            print("inside Airport")
            for airport in airport_ids:
                print(airport)
                airportModel = Airport.objects.filter(pk=airport)
                print(airportModel)
                instance.can_be_used_in_airport.set(airportModel)

        instance.save()
#

This works, but it is saving only the last instance pk I set

#

for example, if I'm sending airport ids 2, 3, 4; then it only saves 4