我有三个模型:LineType、Service、DomainName、Record,它们之间有 m2m 或外键关系。这是代码:
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)
让我介绍一下模型。每个域名只能有一个其他域名可以拥有的服务。每个域名都有多条记录。服务定义可以应用于记录的 LineTypes。
我使用Django admin来管理这些模型,但是Record中“line”的选择都是LineType对象。所以问题是如何根据它的父(DomainName)的“服务”来过滤Record的LineType。