0

谁能告诉我如何限制我在以下代码中继承的 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')
4

1 回答 1

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)
于 2009-07-08T14:49:52.763 回答