我试图用postman 向django views.py文件发送POST请求.

class Article(models.Model):
    authorId=models.CharField(max_length=100)
    authorResidence=models.CharField(max_length=100)
    communtId=models.CharField(max_length=100)
    content=models.TextField()
    contentId=models.CharField(max_length=100)
    source=models.CharField(max_length=100)
    timestamp=models.IntegerField() 
    title=models.CharField(max_length=100)
    
class Interactions(models.Model):
    userId=models.CharField(max_length=100,unique=True)
    location=models.CharField(max_length=100)
    eventType=models.IntegerField(unique=True)
    articleId=models.ForeignKey(Article,on_delete=models.CASCADE)
    communityId=models.CharField(max_length=100)
    source=models.IntegerField()
    timestamp=models.IntegerField()

我试过这样做(in postman):

{
    "userId":"153344",
    "location":"Ethiopia",
    "eventType":"1",
    "articleId":"67353536",
    "communityId":"1234567",
    "source":"1",
    "timestamp":"123456"
    
}

正如你所看到的articleId is a foreignKey字段.

{
    "articleId": [
        "Invalid pk \"67353536\" - object does not exist."
    ]
}

推荐答案

您正在引用不存在的id.如果在Django中使用外键,它将引用所引用模型的主键

step 1: get the article with postman
step 2: copy one of the id of the article
step 3: paste the id to articleId
{
    "userId":"153344",
    "location":"Ethiopia",
    "eventType":"1",
    "articleId":"here",
    "communityId":"1234567",
    "source":"1",
    "timestamp":"123456"
    
}

Python相关问答推荐

通过Selenium从页面获取所有H2元素

将数据框架与导入的Excel文件一起使用

在线条上绘制表面

将JSON对象转换为Dataframe

根据列值添加时区

多处理队列在与Forking http.server一起使用时随机跳过项目

如何并行化/加速并行numba代码?

Django—cte给出:QuerySet对象没有属性with_cte''''

为什么\b在这个正则表达式中不解释为反斜杠

在Python中使用yaml渲染(多行字符串)

干燥化与列姆化的比较

Python pint将1/华氏度转换为1/摄氏度°°

使用嵌套对象字段的Qdrant过滤

jsonschema日期格式

如何使用加速广播主进程张量?

在一个数据帧中,我如何才能发现每个行号是否出现在一列列表中?

在使用ROLING()获得最大值时,是否可以排除每个窗口中的前n个值?

为什么任何一个HTML页面在保存到文件后都会变大6个字节?

突出显示两幅图像之间的变化或差异区域

在Matplotlib中通过特定的Y值而不是 colored颜色 来改变alpha/opacity