NOT NULL constraint failed: baseapi_movie.admin_id without using django forms

huangapple go评论76阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2020年1月3日 15:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定