具有添加功能的只读表格内联 fk 字段

发布于 2024-12-11 19:10:58 字数 774 浏览 0 评论 0原文

我有一个交集/直通表被用作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文