Django 一对多模型和管理内联
Django One-To-Many Model and Admin inlines
我正在尝试在 Django 中定义 2 个模型,如下所示:
class Selector(models.Model):
# A Beautiful Soup selector
selector = models.CharField(max_length=ELEMENT_SELECTOR_MAX_LENGTH, null=True, blank=True)
def __str__(self):
return self.selector
class Provider(models.Model):
# Articles' parent container selector
articles_parent_container_selector = models.ForeignKey(Selector, related_name="articles_parent_container",
help_text=_("Beautiful Soup selector for all articles' "
"parent container"))
# Article's parent container selector
article_parent_container_selector = models.ForeignKey(Selector, related_name="article_parent_container_selector",
help_text=_("Beautiful Soup selector for each article"))
等等等
我们的想法是为 Provider 模型的每个字段设置多个选择器。
我试图在管理应用程序中实现的是为提供程序模型的每个字段提供 charField 内联。
所以我的admin.py就像
from django.contrib import admin
from .models import Provider, Selector
class SelectorInline(admin.StackedInline):
model = Selector
class ProviderAdmin(admin.ModelAdmin):
inlines = [
SelectorInline,
]
admin.site.register(Provider, ProviderAdmin)
我收到错误
<class 'news_providers.admin.SelectorInline'>: (admin.E202) 'news_providers.Selector' has no ForeignKey to 'news_providers.Provider'.
我也试过了
class SelectorInline(admin.StackedInline):
model = Selector
fk_name = 'articles_parent_container'
如此处所述:Django inline forms with multiple foreign keys
但现在的错误是:
<class 'news_providers.admin.SelectorInline'>: (admin.E202) 'news_providers.Selector' has no field named 'articles_parent_container'.
还尝试更改我与 ManyToMany 的关系(这似乎也与我的用例更相关)并应用此处找到的技巧:http://www.mc706.com/tip_trick_snippets/18/django-manytomany-inline-admin/,但运气不好:/
这应该很简单,但恐怕 django 开发人员没有注意到这个用例?
谢谢!
显然,没有内置功能可以在另一个模型页面中显示内联多对多模型。
你能做的最好的事情就是像这样定义模型
models.py
class Selector(models.Model):
# A Beautiful Soup selector
selector = models.CharField(max_length=70, null=True, blank=True)
class Provider(models.Model):
# Articles' parent container selector
articles_parent_container_selector = models.ManyToManyField(Selector, blank=True,
help_text=_("Beautiful Soup selector for all articles' "
"parent container."),
related_name='articles_parent_container')
admin.py
class ArticlesParentContainerSelectorInline(admin.TabularInline):
model = Provider.articles_parent_container_selector.through
verbose_name = "Articles' parent container selector"
class ProviderAdmin(admin.ModelAdmin):
inlines = [
ArticlesParentContainerSelectorInline,
]
exclude = ('articles_parent_container_selector',)
admin.site.register(Provider, ProviderAdmin)
您将得到如下所示:
这有点令人失望,因为我希望获得文本输入而不是下拉菜单(甚至两者都有),因此我可以添加选择器而无需单击加号...
我倾向于为管理应用程序创建我自己的小部件。
总之,感谢大家耐心阅读!
我正在尝试在 Django 中定义 2 个模型,如下所示:
class Selector(models.Model):
# A Beautiful Soup selector
selector = models.CharField(max_length=ELEMENT_SELECTOR_MAX_LENGTH, null=True, blank=True)
def __str__(self):
return self.selector
class Provider(models.Model):
# Articles' parent container selector
articles_parent_container_selector = models.ForeignKey(Selector, related_name="articles_parent_container",
help_text=_("Beautiful Soup selector for all articles' "
"parent container"))
# Article's parent container selector
article_parent_container_selector = models.ForeignKey(Selector, related_name="article_parent_container_selector",
help_text=_("Beautiful Soup selector for each article"))
等等等 我们的想法是为 Provider 模型的每个字段设置多个选择器。
我试图在管理应用程序中实现的是为提供程序模型的每个字段提供 charField 内联。
所以我的admin.py就像
from django.contrib import admin
from .models import Provider, Selector
class SelectorInline(admin.StackedInline):
model = Selector
class ProviderAdmin(admin.ModelAdmin):
inlines = [
SelectorInline,
]
admin.site.register(Provider, ProviderAdmin)
我收到错误
<class 'news_providers.admin.SelectorInline'>: (admin.E202) 'news_providers.Selector' has no ForeignKey to 'news_providers.Provider'.
我也试过了
class SelectorInline(admin.StackedInline):
model = Selector
fk_name = 'articles_parent_container'
如此处所述:Django inline forms with multiple foreign keys
但现在的错误是:
<class 'news_providers.admin.SelectorInline'>: (admin.E202) 'news_providers.Selector' has no field named 'articles_parent_container'.
还尝试更改我与 ManyToMany 的关系(这似乎也与我的用例更相关)并应用此处找到的技巧:http://www.mc706.com/tip_trick_snippets/18/django-manytomany-inline-admin/,但运气不好:/
这应该很简单,但恐怕 django 开发人员没有注意到这个用例?
谢谢!
显然,没有内置功能可以在另一个模型页面中显示内联多对多模型。
你能做的最好的事情就是像这样定义模型
models.py
class Selector(models.Model):
# A Beautiful Soup selector
selector = models.CharField(max_length=70, null=True, blank=True)
class Provider(models.Model):
# Articles' parent container selector
articles_parent_container_selector = models.ManyToManyField(Selector, blank=True,
help_text=_("Beautiful Soup selector for all articles' "
"parent container."),
related_name='articles_parent_container')
admin.py
class ArticlesParentContainerSelectorInline(admin.TabularInline):
model = Provider.articles_parent_container_selector.through
verbose_name = "Articles' parent container selector"
class ProviderAdmin(admin.ModelAdmin):
inlines = [
ArticlesParentContainerSelectorInline,
]
exclude = ('articles_parent_container_selector',)
admin.site.register(Provider, ProviderAdmin)
您将得到如下所示:
这有点令人失望,因为我希望获得文本输入而不是下拉菜单(甚至两者都有),因此我可以添加选择器而无需单击加号...
我倾向于为管理应用程序创建我自己的小部件。
总之,感谢大家耐心阅读!