gpt4 book ai didi

python - 无法创建条目

转载 作者:行者123 更新时间:2023-11-28 22:34:46 24 4
gpt4 key购买 nike

Django 版本 1.10,python 版本 3.4

我在 manage.py shell 中键入并执行此代码:

from tweet.models import Tweet
tweet = Tweet("Parse JSON like a boss", "Admin")
tweet.save()

并收到错误消息:

invalid literal for int() with base 10: 'Parse JSON like a Boss'

模型.py:

class Tweet(models.Model):
text = models.TextField()
pub_time = models.DateTimeField(auto_now_add=True)
author = models.CharField(max_length=100)

class Meta:
ordering = ['-pub_time']

最佳答案

当您没有为模型中的所有字段指定值时,您应该将您确实拥有的值作为名称值对发送。

Tweet(text="Parse JSON like a boss", author="admin")

事实上,最好的做法是一直这样做,这样以后对模型的更改就不会在其他地方破坏您的代码。这也是 manual 中推荐的方式:

To create a new instance of a model, just instantiate it like any other Python class:

class Model(**kwargs)[source]¶ The keyword arguments are simply the names of the fields you’ve defined on your model. Note that instantiating a model in no way touches your database; for that, you need to save().

关于python - 无法创建条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38760988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com