Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django创建具有多对多字段的对象时出错_Django_Django Models - Fatal编程技术网

Django创建具有多对多字段的对象时出错

Django创建具有多对多字段的对象时出错,django,django-models,Django,Django Models,有几个问题已经提到了这个错误,但我找不到任何一个问题的答案 File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/rest_framework/serializers.py", line 214, in save self.instance = self.create(validated_data) File "/home/joao/Code/projects/courty/courty-django/app

有几个问题已经提到了这个错误,但我找不到任何一个问题的答案

  File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/rest_framework/serializers.py", line 214, in save
self.instance = self.create(validated_data)
  File "/home/joao/Code/projects/courty/courty-django/apps/matching/serializers.py", line 19, in create
return MatchFinder.objects.create(**validated_data)
  File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/django/db/models/query.py", line 392, in create
obj = self.model(**kwargs)
  File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/django/db/models/base.py", line 568, in __init__
_setattr(self, prop, kwargs[prop])
  File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 536, in __set__
manager = self.__get__(instance)
  File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 513, in __get__
return self.related_manager_cls(instance)
  File "/home/joao/.virtualenvs/courty/lib64/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 830, in __init__
(instance, self.pk_field_names[self.source_field_name]))
 ValueError: "<MatchFinder: MatchFinder object>" needs to have a value for field "id" before this many-to-many relationship can be used.
查看

class MatchFinderView(views.APIView):

    def post(self, request, format=None):
        serializer = MatchFinderSerialzer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
序列化程序

class MatchFinderSerialzer(BaseModelSerializer):

    class Meta:
        model = MatchFinder
        fields = ('start_datetime', 'venues')

    def create(self, validated_data):
        return MatchFinder.objects.create(**validated_data)
请求

 c = Client()
 data = {
        'start_datetime': now,
        }
 response = c.post('/matches/findmatch/', data)

在post请求中传递
场馆中2个场馆对象的列表会导致相同的错误。

存在多个问题

首先,“player”是一个不可为空的字段-因此需要将其添加到字段中

其次,对于嵌套关系,您需要明确指定如何保存子关系。我已经相应地修改了序列化程序

class MatchFinderSerialzer(BaseModelSerializer):

     class Meta:
         model = MatchFinder
         fields = ('player', 'start_datetime', 'venues')

     def create(self, validated_data):
         venues_data = validated_data.pop('venues')
         match_finder = MatchFinder.objects.create(**validated_data)
         for venue_data in venues_data:
             Venue.objects.create(match_finder=match_finder, **venue_data)
         return match_finder

另外,请确保在发帖请求中提供“玩家id”。

非常感谢您的回答,现在无法尝试,但看起来这正是我所缺少的!
class MatchFinderSerialzer(BaseModelSerializer):

     class Meta:
         model = MatchFinder
         fields = ('player', 'start_datetime', 'venues')

     def create(self, validated_data):
         venues_data = validated_data.pop('venues')
         match_finder = MatchFinder.objects.create(**validated_data)
         for venue_data in venues_data:
             Venue.objects.create(match_finder=match_finder, **venue_data)
         return match_finder