当从 models.py 获取来自同一字段的 2 个字段(以字符串和整数表示形式)时无法发出 POST 请求

问题描述 投票:0回答:0

我需要通过邮递员进行 POST 请求以获取 url http://127.0.0.1:8000/api/v1/posts/1/comments/

models.py 文件中的相同字段我想在 POST 输出中以字符串和整数表示形式执行。

虽然有顺序问题。呈现的models.py、serializers.py和errors内容会更好的呈现我的问题,请参考这些。

# This my models.py
from django.contrib.auth import get_user_model
from django.db import models

User = get_user_model()


class Group(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    description = models.TextField()

    def __str__(self):
        return self.title


class Post(models.Model):
    text = models.TextField()
    pub_date = models.DateTimeField(
        'Дата публикации', auto_now_add=True
    )
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='posts'
    )
    image = models.ImageField(
        upload_to='posts/', null=True, blank=True
    )  # поле для картинки
    group = models.ForeignKey(
        Group, on_delete=models.SET_NULL,
        related_name='posts', blank=True, null=True
    )

    def __str__(self):
        return self.text


class Comment(models.Model):
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='comments'
    )
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE, related_name='comments'
    )
    post_text = models.ForeignKey(
        Post, on_delete=models.CASCADE, related_name='comments'
# In case I'm adding this above new same field past_text the program gives below error and suggest me to change related_name, but if I do so there will be an issue during the actual POST to http://127.0.0.1:8000/api/v1/posts/1/comments/ (it's shown at the very bottom)

    text = models.TextField()
    created = models.DateTimeField(
        'Дата добавления', auto_now_add=True, db_index=True
    )

GitBash 错误以防万一

错误: posts.Comment.post:(fields.E304)“posts.Comment.post”的反向访问器与“posts.Comment.post_text”的反向访问器冲突。 提示:在“posts.Comment.post”或“posts.Comment.post_text”的定义中添加或更改 related_name 参数。 posts.Comment.post:(fields.E305)“posts.Comment.post”的反向查询名称与“posts.Comment.post_text”的反向查询名称冲突。 提示:在“posts.Comment.post”或“posts.Comment.post_text”的定义中添加或更改 related_name 参数。 posts.Comment.post_text:(fields.E304)“posts.Comment.post_text”的反向访问器与“posts.Comment.post”的反向访问器冲突。 提示:在“posts.Comment.post_text”或“posts.Comment.post”的定义中添加或更改 related_name 参数。 posts.Comment.post_text:(fields.E305)“posts.Comment.post_text”的反向查询名称与“posts.Comment.post”的反向查询名称冲突。 提示:在“posts.Comment.post_text”或“posts.Comment.post”的定义中添加或更改 related_name 参数。 (venv)

# serializers.py
from rest_framework import serializers

from posts.models import Group, Post, Comment


class GroupSerializer(serializers.ModelSerializer):

    class Meta:
        model = Group
        fields = ('id', 'title', 'slug', 'description')


class PostSerializer(serializers.ModelSerializer):
    author = serializers.SlugRelatedField(slug_field='username', read_only=True)

    class Meta:
        model = Post
        fields = ('id', 'text', 'author', 'image', 'group', 'pub_date')


class CommentSerializer(serializers.ModelSerializer):
    author = serializers.SlugRelatedField(read_only=True, slug_field='username')

    post_text = serializers.StringRelatedField(read_only=True) 
# adding this above line to be able for additional output of the same in string format

    class Meta:
        model = Comment
        fields = ('id', 'author', 'post', 'post_text' ,'text', 'created')
# adding 'post_text' to fields for get reply in string as well 'post' will get reply by #id
        read_only_fields = ['post']

Postman 错误一旦 POST 到 http://127.0.0.1:8000/api/v1/posts/1/comments/

IntegrityError 在 /api/v1/posts/1/comments/ NOT NULL 约束失败:posts_comment.post_text_id 请求方式:POST 请求网址:http://127.0.0.1:8000/api/v1/posts/1/comments/ 姜戈版本:3.2 异常类型:IntegrityError 异常值:
NOT NULL 约束失败:posts_comment.post_text_id

GitBash 错误

return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError:NOT NULL 约束失败:posts_comment.post_text_id [03/Mar/2023 11:19:12] “POST /api/v1/posts/1/comments/HTTP/1.1” 500 202961

PUT 和 PATCH 请求仍然正常工作,没有问题,而 POST 请求出错,请建议修复此问题。提前致谢!

python-3.x django-models postman http-post
© www.soinside.com 2019 - 2024. All rights reserved.