django.db.utils.integrityerror:不是零约束失败:pages_add_music.upload错误
我编写了一个代码来上传音乐的图像,它有效,但是当我尝试编辑旧帖子(我还没有添加IMG字段)时,它显示了以下错误:
django.db.utils.IntegrityError: NOT NULL constraint failed: pages_add_music.upload
models.py.py.py
class add_music(models.Model):
name = models.CharField(max_length=100)
artist = models.CharField(max_length=100)
nation = models.CharField(max_length=100)
language = models.CharField(max_length=100)
year = models.DecimalField(max_digits=4,decimal_places=0,null=True)
genre = models.TextField()
duration = models.DecimalField(max_digits=3,decimal_places=2,null=True)
upload = models.DateTimeField(auto_now_add=True)
img = models.ImageField(blank=True,null=True,upload_to='images/')
def __str__(self):
return self.name.title()+" by "+self.artist
视图:
def edit_music_view(request,music_id):
my_object = add_music.objects.get(id=music_id)
if request.method != 'POST':
form = add_music_form(instance=my_object)
else:
form = add_music_form(instance=my_object,data=request.POST)
if form.is_valid():
form.save()
context={'form':form,'my_object':my_object}
return render(request,'pages/edit_music.html',context)
模板文件:
{% extends 'pages/base.html' %}
{% block content %}
<p>{{ my_object }}</p>
<form action="{% url 'pages:edit_music' my_object.id %}" method='post' enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value="yes">save</button>
</form>
{% endblock %}
它出了什么问题?
I wrote a code to upload images of music and it works ,but when I try to edit older posts (I hadn't add img field yet) It shows following error:
django.db.utils.IntegrityError: NOT NULL constraint failed: pages_add_music.upload
Models.py
class add_music(models.Model):
name = models.CharField(max_length=100)
artist = models.CharField(max_length=100)
nation = models.CharField(max_length=100)
language = models.CharField(max_length=100)
year = models.DecimalField(max_digits=4,decimal_places=0,null=True)
genre = models.TextField()
duration = models.DecimalField(max_digits=3,decimal_places=2,null=True)
upload = models.DateTimeField(auto_now_add=True)
img = models.ImageField(blank=True,null=True,upload_to='images/')
def __str__(self):
return self.name.title()+" by "+self.artist
views:
def edit_music_view(request,music_id):
my_object = add_music.objects.get(id=music_id)
if request.method != 'POST':
form = add_music_form(instance=my_object)
else:
form = add_music_form(instance=my_object,data=request.POST)
if form.is_valid():
form.save()
context={'form':form,'my_object':my_object}
return render(request,'pages/edit_music.html',context)
Template file:
{% extends 'pages/base.html' %}
{% block content %}
<p>{{ my_object }}</p>
<form action="{% url 'pages:edit_music' my_object.id %}" method='post' enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value="yes">save</button>
</form>
{% endblock %}
What's wrong with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以将该字段更改为:
应该解决该问题。
PS您不应使用
snake_case
命名类。这是坏习惯。You can always change that field to:
that should fix that problem.
PS you should not name classes with
snake_case
. It's bad habit.