I have three models: LineType, Service, DomainName, Record, they have m2m or foreignkey relation between each other. here is the code:
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)
Let me introduce the model. Each DomainName could only have one Service which other DomainNames can have. Each DomainName have multi Records. Service define LineTypes which can be applied to the Record.
I use Django admin to manage these models, but the choice of the "line" in Record are all the LineType object.So the question is how to filter the LineType of Record according to the "service" of it's parent(DomainName).