1👍
when you used ManyToManyField
in your Django model I think you don’t need to implement any nested serializer class or overwriting action at the create. If you change your serializer to this:
class GroupsSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = "__all__"
Django-Rest-Framework add some PrimaryKeyRelatedField
fields for each many-many-field in your model to this serializer therefore you repr()
object of this serializer at the console probably sew like this:
class GroupsSerializer(serializers.ModelSerializer):
.....
groupmodels = PrimaryKeyRelatedField(allow_empty=False, many=True, queryset=GroupModels.objects.all())
sender_clients = PrimaryKeyRelatedField(allow_empty=False, many=True, queryset=Client.objects.all())
receiver_clients = PrimaryKeyRelatedField(allow_empty=False, many=True, queryset=ReceiverClient.objects.all())
warehouses = PrimaryKeyRelatedField(allow_empty=False, many=True, queryset=Warehouse.objects.all())
.....
therefore you request body for those fields change to this:
{
.....
groupmodels: [1,2,3,...],
sender_clients: [1,2,3,...],
receiver_clients: [1,2,3,...],
warehouses: [1,2,3,...],
.....
}
those ids must be created before, If you want to create at the same time when you was created Group
with the duplicated rows at the destination models, should be overwrite with some change in the serializer. But for avoiding to problem and best practice better to implement create action for destination models at the other place like your views or other API for create them. Better to avoiding to implement logic at the serializer. At the future you can control on access on create API for each user role or responsive implementation for each of them.
0👍
Convert the order dict to dict like this:
data = validated_data.dict()
in your case for list of sc_objects:
objs = [obj.dict() for obj in sc_objects]
- [Answered ]-Django project and SVN loads older code versions
- [Answered ]-Many to many vs new object with compound foreign keys in django
- [Answered ]-Django-scarface not working in python 2.7