简体   繁体   English

DRF 创建方法引发'“project_id_id”列中的空值违反非空约束'django rest 框架

[英]DRF create method raise 'null value in column "project_id_id" violates not-null constraint' django rest framework

I got an integrity error while assigning new sites to the existing project id's I followed the same solution from this answer Please find there models & serializers & views while trying with below script I got IntegrityError: null value in column "project_id" violates not-null constraint DETAIL: Failing row contains (18, , null, 2022-06-14 16:43:12.319679+00, 2022-06-14 16:43:12.319679+00, t, 3, 3, null).我在将新站点分配给现有项目 ID 时遇到完整性错误我从这个答案中遵循了相同的解决方案请在尝试使用下面的脚本时找到模型和序列化程序和视图我得到IntegrityError: null value in column "project_id" violates not-null constraint DETAIL: Failing row contains (18, , null, 2022-06-14 16:43:12.319679+00, 2022-06-14 16:43:12.319679+00, t, 3, 3, null).

class CreateNewProjetSerial(serializers.ModelSerializer):
    site_name = ProjectSiteSerializer(many=True, read_only = True)
    assigned_to_id = AssignedUserSerializer(many=True, read_only = True)
    site_names = serializers.ListField(
        child = serializers.CharField(), write_only = True)
    user_ids = serializers.ListField(
        child = serializers.IntegerField(), write_only = True)
    project_ids = serializers.ListField(
        child = serializers.IntegerField(), write_only=True
    )

    class Meta:
        model = Project
        fields = ['site_name','project_name','assigned_to_id', 'project_ids','site_names', 'user_ids']
   
    def create(self, validated_data):
        site_names = validated_data.pop('site_names')
        user_ids = validated_data.pop('user_ids')
        project_ids = validated_data.pop('project_ids')
        new_project = ProjectSite.objects.create(**validated_data)
  
        for project in project_ids :
            new_project_site = Project.objects.create(project_id_id=project, **validated_data)            
            
            for user_id in user_ids:    
                Assignment.objects.create(assigned_to_id__id=user_id, site_id=new_project_site)
  
        return new_project
class Project(models.Model):
    project_id = models.AutoField(primary_key=True, unique=True)
    project_name = models.CharField(max_length=100)


json object for post 
{
    "site_names": ["site1106", "site2106"],
    "user_ids": [1],
    "project_ids": [16]
}

I think the code for saving part is wrong.我认为保存部分的代码是错误的。 It should be project_id , not the project_id_id它应该是project_id ,而不是project_id_id

...
for project in project_ids :
    new_project_site = Project.objects.create(project_id=project, **validated_data)

...

在您的 json 对象中,您将密钥用作“project_id”,而在创建方法中,您将密钥用作“project_ids”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在DRF(django-rest-framework)中,“author_id”列中的空值违反了非空约束。 我该怎么办? - In DRF(django-rest-framework), null value in column “author_id” violates not-null constraint. What should i do? Django Rest Framework POST 失败:“cat_id”列中的空值违反了非空约束 - Django Rest Framework POST fails: null value in column "cat_id" violates not-null constraint Django:关系的“id”列中的 null 值违反非空约束 - Django : null value in column "id" of relation violates not-null constraint “user_id”列中的 Django REST POST null 值违反非空约束 - Django REST POST null value in column “user_id” violates not-null constraint Django 表单提交'“project_id”列中的空值违反非空约束',但表单有效 - Django Form Submit 'null value in column “project_id” violates not-null constraint', but Form is valid DRF:无法创建 object null。 错误:“network_from_id”列中的值违反非空约束 - DRF: Can't create object null. Error: value in column "network_from_id" violates not-null constraint null “user_id”列中的值违反了非空约束 DRF - null value in column "user_id" violates not-null constraint DRF “ job_id”列中的空值违反了非空约束 - null value in column “job_id” violates not-null constraint “ user_id”列中的空值违反了非空约束 - null value in column “user_id” violates not-null constraint “ city_id”列中的空值违反了非空约束 - null value in column “city_id” violates not-null constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM