我的 Complete_Book 模型有一个 Book 的外键。书籍是我从已安装的应用程序中使用的东西(因此,“外部应用程序”)我希望能够直接从 Complete_Book 管理员编辑/创建“书籍”。这可能吗?我无法使用内联,因为我的外键关系与内联允许的关系“向后”。
我的问题与这个问题中解释的相同(Django admin内联显示但外键具有相反的关系 https://stackoverflow.com/questions/15339809/django-admin-inline-display-but-foreign-key-has-opposite-relation)但我无法按照该答案中的建议重构我的模型。还有其他办法吗?感谢您的帮助!
模型.py
class Complete_Book(models.Model):
topic = models.ForeignKey('topic')
book = models.ForeignKey(Book)
is_primary = models.BooleanField()
subprogram_name = models.CharField(max_length = 256, blank=True)
class Book(models.Model):
title = models.CharField(max_length=512)
authors = models.CharField(max_length=2048)
year = models.PositiveIntegerField(max_length=4)
django_reverse_admin https://github.com/daniyalzade/django_reverse_admin是一个 django 模块,用于解决需要内联的问题,其中关系的方向是“错误”的。
您需要将其添加到您的requirements.txt中,然后导入它:
admin.py
from django_reverse_admin import ReverseModelAdmin
class Complete_Book(ReverseModelAdmin):
# usual admin stuff goes here
inline_type = 'tabular' # or could be 'stacked'
inline_reverse = ['book'] # could do [('book', {'fields': ['title', 'authors'})] if you only wanted to show certain fields in the create admin
admin.site.register(Book, Complete_Book)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)