如何使外键显示为其自己的形式而不是下拉菜单?
我正在尝试为电影列表创建一个表单集,如下所示:
class MovieList(models.Model):
user = models.ForeignKey(User)
movie = models.ForeignKey(Movie)
class Movie(models.Model):
genre = models.ForeignKey(Genre)
name = models.CharField(max_length=300)
MovieListFormSet = inlineformset_factory(User, MovieList)
movielist_formset = MovieListFormSet(instance=currentUser)
现在“电影”显示为下拉菜单。但是,我希望“电影”显示为“Movie.name”的空白文本框和“Movie.genre”的下拉菜单。用户可以在文本框中输入电影名称并选择类型,如果该电影已存在,则 MovieList.movie 引用现有的表行,否则将创建一个新的 MovieList 行,并且 MovieList.movie 引用新的表行。
我该怎么做?
I'm trying to create a formset for a list of movies like so:
class MovieList(models.Model):
user = models.ForeignKey(User)
movie = models.ForeignKey(Movie)
class Movie(models.Model):
genre = models.ForeignKey(Genre)
name = models.CharField(max_length=300)
MovieListFormSet = inlineformset_factory(User, MovieList)
movielist_formset = MovieListFormSet(instance=currentUser)
Right now "Movie" is appearing as a dropdown menu. However, I'd like it so that "Movie" appears as a blank textbox for 'Movie.name', and a dropdown menu for 'Movie.genre'. The user can enter in the movie name in the textbox and select the genre, and if that movie already exists, MovieList.movie references the existing table row, otherwise a new MovieList row is created and MovieList.movie references the new table row.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要更复杂的逻辑,为什么不从头开始创建一个新的表单集。代码应该不多。
If you want more complex logic, why not to create a new formset from scratch. It shouldn't be much code.