Raju Ahmed Shetu
Raju Ahmed Shetu

Reputation: 134

Django Table Join

I am going over the django join on to get some query set. I have declared two model like this

class PortService(models.Model):
    port=models.IntegerField()
    service=models.TextField()
    class Meta:
        unique_together=(('port','service'),)

class ServiceDefects(models.Model):
    port=models.ForeignKey('PortService')
    defect_id=models.TextField()
    defect_description=models.TextField()
    class Meta:
        unique_together=(('port','defect_id'),)

I want to get all the entry of ServiceDefects class which has a port number of 80 and service named 'Apache'. For this I wrote the query like this:

ServiceDefects.objects.filter(portservice__port=80).filter(portservice__service='Apache') 

Any Help?? Thanks in advance.

Upvotes: 0

Views: 563

Answers (2)

Selcuk
Selcuk

Reputation: 59184

You should write both conditions in a single filter:

ServiceDefects.objects.filter(port__port=80, port__service='Apache')

Upvotes: 1

Alasdair
Alasdair

Reputation: 308789

The foreign key field is called port, not portservice, so you should do:

ServiceDefects.objects.filter(port__port=80).filter(port__service='Apache')

You might prefer to put both arguments in a single filter():

ServiceDefects.objects.filter(port__port=80, port__service='Apache')

Upvotes: 2

Related Questions