简体   繁体   English

Django Rest Framework POST 失败:“cat_id”列中的空值违反了非空约束

[英]Django Rest Framework POST fails: null value in column "cat_id" violates not-null constraint

I've recently converted my views to generic class-based views, however I've just noticed that POST requests fail on classes that have foreign-keys.我最近将我的视图转换为基于类的通用视图,但是我刚刚注意到 POST 请求在具有外键的类上失败。 The following is my code, followed by the error message.以下是我的代码,后面是错误信息。

models.py模型.py

class Category(models.Model):
    name = models.CharField(max_length=25, blank=False)

    class Meta:
        ordering = ('id',)


class Task(models.Model):
    name = models.CharField(max_length=25, blank=False)
    cat = models.ForeignKey(Category, on_delete=models.CASCADE)

    class Meta:
        ordering = ('id',)

serializers.py序列化程序.py

class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = ('id', 'name', 'cat_id')


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ('id', 'name')

views.py视图.py

class TaskList(generics.ListCreateAPIView):
    """
    List all Tasks (OR for specified cat_id)
    """
    queryset = Task.objects.all()
    serializer_class = TaskSerializer
    filter_fields = ('cat_id',)

urls.py网址.py

path('tasks/', views.TaskList.as_view()),

Error returned返回错误

django.db.utils.IntegrityError: null value in column "cat_id" violates not-null constraint
DETAIL:  Failing row contains (51, buy-some, null).

REQUEST content: JSON Object请求内容:JSON 对象

{
    "name": "buy-some",
    "cat_id": 1
}

Additionally, Content-Type, Accept headers are set to application/json .此外, Content-Type、Accept标头设置为application/json

Category with id=1 exists存在 id=1 的类别

Probably what you want is to define the field cat in your TaskSerializer to be a PrimaryKeyRelatedField (documentation here) , in your case would be:可能您想要的是将TaskSerializer的字段cat定义为PrimaryKeyRelatedField (documentation here) ,在您的情况下是:

class TaskSerializer(serializers.ModelSerializer):
    cat = PrimaryKeyRelatedField(queryset=Category.objects.all())
    class Meta:
        model = Task
        fields = ('id', 'name', 'cat')

Then in your request just send the pk in the "cat" field like so:然后在您的请求中,只需在"cat"字段中发送 pk,如下所示:

{
    "name": "buy-some",
    "cat": 1
}

This should do the trick.这应该可以解决问题。

暂无
暂无

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

相关问题 “user_id”列中的 Django REST POST null 值违反非空约束 - Django REST POST null value in column “user_id” violates not-null constraint DRF 创建方法引发'“project_id_id”列中的空值违反非空约束'django rest 框架 - DRF create method raise 'null value in column "project_id_id" violates not-null constraint' django rest framework Django:关系的“id”列中的 null 值违反非空约束 - Django : null value in column "id" of relation violates not-null constraint 在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 - 列中的空值违反了 Django 管理中的非空约束 - Django - null value in column violates not-null constraint in Django Admin “ user_id”列中的空值违反了非空约束 - null value in column “user_id” violates not-null constraint “ city_id”列中的空值违反了非空约束 - null value in column “city_id” violates not-null constraint Flask SQLAlchemy null “id”列中的值违反非空约束 - Flask SQLAlchemy null value in column "id" violates not-null constraint “ job_id”列中的空值违反了非空约束 - null value in column “job_id” violates not-null constraint Django:“is_admin”列中的 null 值违反了非空约束 - Django: null value in column "is_admin" violates not-null constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM