Value error. model field must be an instance django
I want to save a list of the relational objects using IDs but unfortunately, I'm getting an error from Django which I attached below.
error:
Cannot assign "[<Tag: Tag object (189)>, <Tag: Tag object (190)>]":
"PackageRoom.tag" must be a "Tag" instance.
models.py
class Tag(models.Model):
name = models.CharField(max_length=255, default='')
description = models.CharField(max_length=255, default='')
singleline = models.ManyToManyField(Singleline)
class Meta:
db_table = 'tags'
class PackageRoom(models.Model):
name = models.CharField(max_length=255, default='')
tag = models.ForeignKey(Tag, on_delete=models.PROTECT)
class Meta:
db_table = 'package_rooms'
serializers.py
class PackageRoomSerializer(serializers.ModelSerializer):
tag = serializers.PrimaryKeyRelatedField(queryset=Tag.objects.all(), many=True)
class Meta:
model = PackageRoom
fields = ['id', 'name', 'description', 'tag']
views.py
serializer = PackageRoomSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
JSON object which is sent from Frontend:
{
"name": "example room",
"description": "lorem lipsum",
"tag": [189, 190]
}