Saving a StackedInline foreign keyed model before primary model in Django Admin
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?
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.
精彩评论