在 Django Admin 中的主模型之前保存 StackedInline 外键模型

发布于 2024-09-24 08:57:32 字数 1387 浏览 3 评论 0原文

我有四个模型,其关系

Model PagetTemplate(models.Model):
    pass

Model TextKey(models.Model):
    page_template = models.ForeignKey(PageTemplate, related_name='text_keys')


Model Page(models.Model):
    page_template = models.ForeignKey(Pagetemplate, related_name='pages')

Model Text(models.Model):
    key = models.ForeignKey(TextKey, related_name='text_fields')
    page = models.ForeignKey(Page, related_name='text_fields')

如下:

                            PageTemplate
                                /\
                               /  \
                              /    \
                       TextKey      Page
                              \    /
                               \  /
                                \/
                               Text

在页面验证中(在 clean 方法中),我检查 [key for key in page.page_template.text_keys] 和 [text_field.key for text_field in page.text_fields] 匹配,以便所有文本键都填充在我的文本中。我遇到的问题是,在调用 clean 时,page.text_fields 为空。管理代码看起来像。

class TextInline(admin.StackedInline):
    model = Text
    extra = 0

class PageAdmin(DebugModelAdmin):
    inlines = [TextInline]

admin.site.register(Page, PageAdmin)

我将 admin.ModelAdmin 包装在日志记录类中,并且知道在调用 ModelAdmin.add_view 时我拥有所需的信息,但覆盖此信息是正确的做法或存在一些选项/方法可以更好地覆盖我所缺少的?

I have four models with the relationships

Model PagetTemplate(models.Model):
    pass

Model TextKey(models.Model):
    page_template = models.ForeignKey(PageTemplate, related_name='text_keys')


Model Page(models.Model):
    page_template = models.ForeignKey(Pagetemplate, related_name='pages')

Model Text(models.Model):
    key = models.ForeignKey(TextKey, related_name='text_fields')
    page = models.ForeignKey(Page, related_name='text_fields')

the relation is like this:

                            PageTemplate
                                /\
                               /  \
                              /    \
                       TextKey      Page
                              \    /
                               \  /
                                \/
                               Text

In the validation for page (In the clean method), I check that [key for key in page.page_template.text_keys] and [text_field.key for text_field in page.text_fields] match up so that all text keys are filled in my a text. The problem I am having is that at the time the clean is called, page.text_fields is empty. The admin code looks like.

class TextInline(admin.StackedInline):
    model = Text
    extra = 0

class PageAdmin(DebugModelAdmin):
    inlines = [TextInline]

admin.site.register(Page, PageAdmin)

I wrapped admin.ModelAdmin in a logging class and know that I have the information I need when ModelAdmin.add_view is called but is overriding this the right thing to do or is there some option/method that would be better to override that I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-10-01 08:57:32

看着 S. Lott 对各种 django 相关线程的明智建议,我决定自己编写一个应用程序来执行此操作,而不是强迫 django 管理员做一些不该做的事情。老实说,我只想为 django 下载一个像样的 CMS,但我能找到的那些要么很糟糕(他们的代码充满了类型检查、缩进制表符等),要么在 1.2 上不起作用,所以我正在滚动自己的。

Looking at S. Lott's sagacious advice on various django related threads, I decided to write an app to do this myself instead of forcing the django admin to do something that it wasn't meant to do. I would honestly just download a decent CMS for django but the ones that I can find either suck (their code is riddled with typechecking, indented with tabs, etc) or don't work on 1.2, so i'm rolling my own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文