具有添加功能的只读表格内联 fk 字段
我有一个交集/直通表被用作 django 管理中的内联表。内联具有作为下拉菜单列出的其他模型的外键。我可以通过设置 readonly_fields 变量使字段只读,但希望允许通过添加添加新的外键对象。因此,在下面的示例中,我希望能够通过内联添加 Building_Room 行,只是无法在不单击“+”并通过 Room 弹出管理屏幕添加新房间键的情况下从任何现有 Room 键中进行选择。我必须通过自定义模板工作来完成此操作吗?非常感谢。
模型.py
class Building(models.Model):
rooms = models.ManyToManyField('Room', null=True, through="Building_Room")
...
class Room(models.Model):
...
class Building_Room(models.Model):
building = models.ForeignKey(Building)
room = models.ForeignKey(Room)
管理.py
class Building_Room_Inline(admin.TabularInline):
model = Building_Room
readonly_fields = ('building',)
...
class Building_Admin(admin.ModelAdmin):
inlines = (Building_Room_Inline,)
...
I have an intersection/through table being used as an inline in the django admin. The inline has the foreign key to the other model listed as a drop down menu. I am able to make the field read only by setting the readonly_fields variable, but wish to allow addition of new foreign key objects by adding. So with the example below I'd like to be able to add Building_Room rows via the inline, just not be able to choose from any existing Room keys without clicking the '+' and adding a new one via the Room pop up admin screen. Will I have to do this via custom template work? Thanks much.
models.py
class Building(models.Model):
rooms = models.ManyToManyField('Room', null=True, through="Building_Room")
...
class Room(models.Model):
...
class Building_Room(models.Model):
building = models.ForeignKey(Building)
room = models.ForeignKey(Room)
admin.py
class Building_Room_Inline(admin.TabularInline):
model = Building_Room
readonly_fields = ('building',)
...
class Building_Admin(admin.ModelAdmin):
inlines = (Building_Room_Inline,)
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论