For example, imagine a simple model, consisting of a person class, which may have more than one email.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person(models.Model): | |
name=models.CharField(max_length=20); | |
def __unicode__(self): | |
return self.name; | |
class Phone(models.Model): | |
person=models.ForeignKey(Person); | |
area_code=models.CharField(max_length=3); | |
number=models.CharField(max_length=7); | |
def __unicode__(self): | |
return "("+self.area_code+")"+self.number +" for " +self.person.name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from joins.models import *; | |
p1=Person.objects.create(name='Orlando'); | |
ph1=Phone.objects.create(person=p1,area_ code='770',number='1234567'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person.objects.filter(phone_set__area='770'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT Person.* | |
FROM Person JOIN Phone ON (Person.id=Phone.person_id) | |
WHERE Phone.area_code='770' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * | |
FROM Person | |
WHERE id in ( | |
SELECT person_id | |
FROM Phone | |
WHERE area_code='770' | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person.objects.filter(phone_set__area='770').distinct(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [8]: Person.objects.filter(phone__area_code='770') | |
Out[8]: [<Person: Orlando>, <Person: Orlando>] | |
In [9]: Person.objects.filter(phone__area_code='770').distinct() | |
Out[9]: [<Person: Orlando>] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ph2=Phone.objects.create(person=p1,area_code='770',number='1234567'); |
No comments:
Post a Comment