英文:
NOT NULL constraint failed: baseapi_movie.admin_id without using django forms
问题
我创建了Theimdb电影API,并将所有电影数据存储到数据库中,但在使用外键时出现了错误。一些用户喜欢不同的电影,所以我想展示不同的用户可以看到不同的数据,所以我使用了模型关系,可以有人建议我如何做到这一点。
views.py
@login_required(redirect_field_name="log")
def trending(request):
movie_list = []
try:
if request.method == "POST":
if "taskAdd" in request.POST:
title = request.POST["title"]
release = request.POST["release"]
poster = request.POST["poster"]
vote_average = request.POST["vote_average"]
movie_id = request.POST["movie_id"]
task = Movie(title=title, release=release, poster=poster, vote_average=vote_average, movie_id=movie_id)
task.save()
return redirect('/')
models.py
class Movie(models.Model):
title = models.CharField(verbose_name='电影名称', max_length=30)
release = models.DateField()
poster = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100)
vote_average = models.CharField(max_length=30, default=True)
movie_id = models.IntegerField(default=True)
admin = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
英文:
I created Theimdb movie API and I store all the movies' data into database but I got error when I used foreign key.some users liked different film so I want show different user can see different data, so I used model relationship, can anyone please suggest me how to do it.
views.py
@login_required(redirect_field_name="log")
def trending(request):
movie_list=[]
try:
if request.method=="POST":
if "taskAdd" in request.POST:
title=request.POST["title"]
release=request.POST["release"]
poster=request.POST["poster"]
vote_average=request.POST["vote_average"]
movie_id=request.POST["movie_id"]
task=Movie(title=title,release=release,poster=poster,vote_average=vote_average,movie_id=movie_id)
task.save()
return redirect('/')
models.py
class Movie(models.Model):
title=models.CharField(verbose_name='movie_name',max_length=30)
#overview=models.TextField(blank=True, null=True)
release=models.DateField()
poster=models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100)
vote_average=models.CharField(max_length=30,default=True)
movie_id=models.IntegerField(default=True)
admin=models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
答案1
得分: 0
你可以在创建电影时像下面这样添加管理员:
task=Movie(
title=title,
release=release,
poster=poster,
vote_average=vote_average,
movie_id=movie_id,
admin=request.user
)
task.save()
英文:
you can add admin in while creating a movie like below
task=Movie(
title=title,
release=release,
poster=poster,
vote_average=vote_average,
movie_id=movie_id,
admin=request.user
)
task.save()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论