Вот модель для заголовка,
class Header(models.Model):
id = models.AutoField(primary_key=True)
features = models.ForeignKey(Project, null=True, blank=True, related_name= 'features')
def __str__(self):
return str(self.name)
и эта модель для Project (внешний ключ функций в заголовке):
class Project(models.Model):
id = models.AutoField(primary_key=True)
project_info = models.OneToOneField(ProjectInfo, related_name='project_info')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
dataset_associated = models.ForeignKey(Dataset)
ratio_test_training = models.IntegerField()
random_seed = models.IntegerField(blank=True, null=True)
url = models.URLField(blank=True, null=True)
def __str__(self):
return str(self.project_info.name)
И, когда я пытаюсьчтобы сохранить сериализатор заголовка, передав None объектам, он показывает ошибку:
{features: ["This field may not be null."]}
, даже если я установил его свойство null = True.
Это сериализатор для заголовка:
class HeaderSerializer(serializers.ModelSerializer):
features = ProjectSerializer()
class Meta:
model = models.Header
fields = ('id', 'features')
def create(self, validated_data):
features_data = validated_data.pop("features")
if(features_data):
features = models.Project.objects.get(pk =
(features_data.get("id")))
else:
features = None
obj=models.Header.objects.create(features = features)
obj.save()
return obj
А это код в views.py,
@api_view(['GET', 'POST', 'DELETE'])
def header_detail_pk(request):
if request.method == 'POST':
features = request.POST.get('features')
if(features):
features = project_serializer.data
print("Not none")
else:
features = None
serializer = HeaderSerializer('features':features)
if serializer.is_valid():
serializer.save(features= features)
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Это сериализатор проекта:
class ProjectSerializer(serializers.ModelSerializer):
project_info = ProjectInfoSerializer()
dataset_associated = DatasetSerializer()
class Meta:
model = models.Project
fields = ('id', 'project_info','created_at', 'updated_at', 'dataset_associated', 'ratio_test_training', 'random_seed', 'url')
def create(self, validated_data):
project_info_data = validated_data.pop("project_info")
dataset_associated_data = validated_data.pop("dataset_associated")
project_info_id = project_info_data.get("id")
dataset_associated_id = dataset_associated_data.get("dataset_id")
project_info = models.ProjectInfo.objects.get(pk=project_info_id)
dataset_associated = models.Dataset.objects.get(dataset_id=dataset_associated_id)
obj = models.Project.objects.create(url = validated_data.get("url") ,ratio_test_training=validated_data.get("ratio_test_training"), random_seed=validated_data.get("random_seed"), project_info=project_info, dataset_associated=dataset_associated)
obj.save()
return obj