我有以下两个模型: models.py
class Genre(models.Model):
name = models.CharField(max_length=50, blank=True, unique=True)
def __unicode__(self):
return self.name
class Movies(models.Model):
title = models.CharField(max_length=50, blank=False)
plot = models.CharField(max_length=500, blank=True)
actors = models.CharField(max_length=200, blank=True)
director = models.CharField(max_length=100, blank=True)
genre = models.ManyToManyField(Genre)
def __unicode__(self):
return self.title
表格.py
class MovieForm(forms.ModelForm):
genre = forms.CharField(widget=forms.TextInput)
class Meta:
model = Movies
当我运行 syncdbmovies , genre , movies_genre
表时创建
movies_genre table has (id, movies_id , genre_id)
但是在电影表中,没有为流派字段创建任何 cloumn,它应该类似于genre_id
为什么电影表中缺少genre_id 字段?
编辑 :.................我如何添加数据:
def genre_save(request):
genre_name = request.POST['genre']
genre_obj = Genre(name=genre_name)
genre_obj.save()
movie_obj = Movies(genre=genre_obj.id)
movie_obj.save() // error
TypeError 'genre' 是此函数的无效关键字参数
def add_movie(request):
if request.method=="POST":
genre_save(request)
form_post = MovieForm(request.POST)
form_post.save()
/// other stuff....