有谁能告诉我如何限制我在以下代码中继承的Page模型的选择?
class CaseStudy(Page):
"""
An entry in a fancy picture flow widget for a case study page
"""
image = models.ForeignKey(Image, limit_choices_to={'is_active': True, 'category__code':'RP'})
def __unicode__(self):
return u"%s" % self.title
django管理员成功限制了下拉列表中的图像选择,但我想限制页面模型中的字段(“父页面字段”),即:
class Page(models.Model):
parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
答案 0 :(得分:0)
我设法解决了这个问题 - 通过覆盖管理模型表单。我意识到这可能会收紧,但认为它可能会被用于那里的人。这是admin.py
的摘录class CaseStudyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(CaseStudyForm, self).__init__(*args, **kwargs)
recent_project_page = Page.objects.get(title="Recent Projects")
parent_widget = self.fields['parent'].widget
choices = []
for key, value in parent_widget.choices:
if key in [recent_project_page.id,]:
choices.append((key, value))
parent_widget.choices = choices
class CaseStudyAdmin(admin.ModelAdmin):
form = CaseStudyForm
admin.site.register(CaseStudy, CaseStudyAdmin)