我尝试覆盖我的文章模型的save()方法。我做了几次同样的事情,没关系。但是,这次价值误差提升了。
我的课程:
class Article(models.Model):
title = models.CharField(max_length=500)
body = models.TextField(max_length=50000)
url = models.CharField(max_length=500, null=True)
def save(self, *args, **kwargs):
self.url = self.title
self.url.lower()
self.url.replace(' ', '_')
super(Article, self).save(*args, **kwargs)
错误日志:
>>> a = Article("Title 1", "text")
>>> a.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/user/django/project/apps/articles/models.py", line 19, in save
super(Article, self).save(*args, **kwargs)
File "/home/user/.virtualenvs/dj18/local/lib/python2.7/site-packages/django/db/models/base.py", line 710, in save
force_update=force_update, update_fields=update_fields)
...
File "/home/user/.virtualenvs/dj18/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Title 1'
答案 0 :(得分:1)
您创建的对象不是推荐的方法。将值指定为
时,您必须为specifiying the parameter namesa = Article(title = "Title 1", body = "text")
a.save()
这里我已经指定新对象的标题必须设置为Title 1
,
并且body
将设置为text