2009-07-07 8 views
0

다음 코드에서 상속받은 페이지 모델의 선택을 어떻게 제한 할 수 있습니까? 즉,Django 모델 상속 및 limit_choices_to

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 

은 장고 관리자는 아래 성공적으로 드롭 이미지 선택을 제한하고,하지만 난뿐만 아니라 (A '상위 페이지 필드') 페이지 모델에서 필드를 제한하려는 :

class Page(models.Model): 
    parent    = models.ForeignKey('self', blank=True, null=True, related_name='children') 

답변

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)