Django:如何通过自定义Django_Admin动态过滤外键选择

时间:2012-10-26 14:46:48

标签: django django-admin

我有三种模式:LineType,Service,DomainName,Record,它们之间有m2m或foreignkey关系。这是代码:

class LineType(models.Model):
    linename = models.CharField(max_length=30,primary_key=True)

    def __unicode__(self):
        return self.linename

class Service(models.Model):
    servicename = models.CharField(max_length=30,primary_key=True)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    line = models.ManyToManyField(LineType)

    def __unicode__(self):
        return self.servicename

class DomainName(models.Model):
    domainname = models.CharField(max_length=30,primary_key=True)
    user = models.ForeignKey(SiteProfile)
    service = models.ForeignKey(Service)

    def __str__(self):
        return self.domainname

class Record(models.Model):
    TYPE_CHOICES = (
                ('A','A'),
                ('CNAME','CNAME'),
                ('MX','MX'),
               )
    domainname = models.ForeignKey(DomainName)   

    def get_line(self):
        domain = DomainName.objects.get(pk=self.domainname_id)
        service = Service.objects.get(pk=domain.service_id)
        lines = service.line.all()
        choices = []
        for x in lines:
           choices.append((x.linename,x.linename))
        return choices

    host = models.CharField(max_length=30)
    type = models.CharField(max_length=5,choices=TYPE_CHOICES,default='A')
    line = models.ForeignKey(LineType)
    destaddress = models.CharField(max_length=30)

让我介绍一下这个模型。每个DomainName只能有一个其他DomainNames可以拥有的服务。每个DomainName都有多个记录。服务定义可应用于记录的LineType。

我使用Django admin来管理这些模型,但Record中“line”的选择都是LineType对象。所以问题是如何根据它的父级的“服务”过滤RecordType的LineType(DomainName) )。

1 个答案:

答案 0 :(得分:0)

取决于您的确切要求和您的Django版本,答案可能是设置limit_choices_to attribute of the foreign key,或自定义ModelAdmin表单:

使用limit_choices_to

只需将行选项限制为匹配linename == 'foo'

的行
class Record(models.Model):
    # ...
    line = models.ForeignKey(LineType, limit_choices_to={'linename': 'foo'}))

使用自定义form

如果在实例化表单时存在实例,我们可以选择它的域的服务行作为查询集选项

class RecordAdminForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(RecordAdminForm, self).__init__(*args, **kwargs)

        # access object through self.instance...
        if self.instance:
            valid_lines = self.instance.domainname.service.line.objects.all()
            self.fields['line'].queryset = valid_lines

class RecordAdmin(admin.ModelAdmin):
    form = RecordAdminForm
    # ...

参考SO问题: