简体   繁体   English

具有可编辑外键关系的Django Inlineformset

[英]Django Inlineformset with editable Foreign Key Relation

I'm trying to create a formset of related objects, where the relation itself is also an editable field. 我正在尝试创建相关对象的表单集,其中关系本身也是可编辑字段。

Let's say we have a models.py like the one below. 假设我们有一个model.py,如下所示。

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=45)

class Book(models.Model):
    name = models.CharField(max_length=45)
    author = models.ForeignKey(Author)

Is there a good, pythonic way to create a formset of all Books by a certain Author, and be able to change the Author of any of those books? 有没有一种好的Python方法,可以由某个作者创建所有书籍的表单集,并能够更改其中任何书籍的作者? This would be a particularly useful way to fix any errors in the foreign key relations. 这将是修复外键关系中任何错误的特别有用的方法。

My first instinct was that inlineformset would solve this for me, but I can't seem to get the Author of each book to be an editable field, even when I use custom forms. 我的第一个直觉是inlineformset可以为我解决这个问题,但是即使我使用自定义表单,我似乎也无法使每本书的作者成为可编辑字段。

using modelformset_factory will do the trick. 使用modelformset_factory可以解决问题。 (pulled from my comments here for completeness of the systems question/answer model) (出于系统问题/答案模型的完整性,从我的评论中删除)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM