A cheat sheet for Django ORM relationships — version 2.1 I want to start this story with saying thanks to , who published a really useful article “ ” about a year ago and also gave me permission to use the same structure/images/examples in my article. Mahmoud Zalt Eloquent Relationships Cheat Sheet One to One Relationship One to Many Relationship Many to Many Relationship Polymorphic One to Many Relationship — Generic Relations Polymorphic One to Many Relationship — django-polymorphic package Polymorphic Many to Many Relationship — django-polymorphic package One to One Relationship Demo details: In this demo we have 2 models ( and ), and 2 tables ( and ). Owner Car owners cars Business Rules: The can own one .The can be owned by one . Owner Car Car Owner Relations Diagram: Relationship Details: The table should store the . Cars Owner ID Models: Owner(models.Model): class #... name = models.CharField(max\_length=255) Car(models.Model): class #... name = models.CharField(max\_length=255) owner = models.OneToOneField( Owner, on\_delete=models.CASCADE, related\_name='car' ) Store Records: car = Car.objects.get(id=1)owner = Owner.objects.get(id=1) # Create relation between Owner and Car. owner.car = carowner.car.save() # Create relation between Car and Owner. car.owner = ownercar.save() Retrieve Records: # Get Owner Car owner.car # Get Car Owner car.owner; One to Many Relationship Demo details: In this demo we have 2 models ( and ), and 2 tables ( and ). Thief Car thieves cars Business Rules: The can steal many .The can be stolen by one . Thief Cars Car Thief Relations Diagram: Relationship Details: The table should store the . Cars Thief ID Models: Thief(models.Model): class # ... name = models.CharField(max\_length=255) Car(models.Model): class # ... name = models.CharField(max\_length=255) thief = models.ForeignKey( Thief, on\_delete=models.CASCADE, related\_name='cars' ) Store Records: thief = Thief.objects.get(id=1)car1 = Car.objects.get(id=1)... # Create relation between Thief and Car. thief.cars.add(car1,car2, car3) # Create relation between Car and Thief. car.thief = thiefcar.save() # When we creating new car :car = Car(name = 'test name', thief=thief)car.save() Retrieve Records: # Get Thief Car thief.cars.all() # Get Car Thief car.thief Many to Many Relationship Demo details: In this demo we have 2 models ( and and 3 tables ( , and a pivot table named ). Driver Car), drivers cars car_drivers Business Rules: The can drive many .The can be driven by many . Driver Cars Car Drivers Relations Diagram: Relationship Details: The table “car_driver” should store the and the . Pivot Driver ID Car ID Models: Driver(models.Model): class # ... name = models.CharField(max\_length=255) Car(models.Model): class # ... name = models.CharField(max\_length=255) drivers = models.ManyToManyField( Driver, related\_name='cars' ) Store Records: # Create relation between Driver and Car. driver = Driver.objects.get(id=1)car1 = Car.objects.get(id=1)car2 = Car.objects.get(id=2) driver.cars.add(car1,car2) # Create relation between Car and Driver.car = Car.objects.get(id=1)driver1 = Driver.objects.get(id=2)driver2 = Driver.objects.get(id=3) car.drivers.add(driver1, driver2) Retrieve Records: # Get Driver Car driver.cars.all() # Get Car Drivers car.drivers.all() Polymorphic One to Many Relationship With Django’s Generic Relations Demo details: In this demo we have 3 models ( and and 3 tables ( , and ). Man, Woman Car), men women cars Business Rules: The (buyer) can buy many . The (buyer) can buy many .The can be bought by one buyer ( or ). Man Cars Woman Cars Car Man Woman Relations Diagram: Relationship Details: The table should store the and the Buyer table should store the relation between and . Car Buyer ID ID Type Models: django.contrib.contenttypes.fields GenericForeignKey django.contrib.contenttypes.fields GenericRelation django.contrib.contenttypes.models ContentType from import from import from import Car(models.Model): class # ... name = models.CharField(max\_length=255) content\_type = models.ForeignKey(ContentType, on\_delete=models.CASCADE) object\_id = models.PositiveIntegerField() content\_object = GenericForeignKey() Woman(models.Model): class # ... name = models.CharField(max\_length=255) cars = GenericRelation(Car) Man(models.Model): class # ... name = models.CharField(max\_length=255) cars = GenericRelation(Car) Store Records: man = Man.objects.get(id=1)woman = Woman.objects.get(id=1) # Create relation between buyer (Man/Woman) and Car. car = Car.objects.get(id=1)woman.cars.add(car) # Create relation between Car and buyer (Men/Women). man = Man.objects.get(id=1)woman = Woman.objects.get(id=1) c = Car(name = 'test name',content_object=man)c.save() c = Car(name = 'test name',content_object=woman)c.save() Retrieve Records: # Get buyer (Man/Woman) Cars man.cars.all()woman.cars.all() # Get Car buyer (Man and Woman) car.content_object Polymorphic One to Many Relationship With package — django-polymorphic . Demo details: In this demo we have 4 models ( , and and 4 tables ( , , and ). Buyer Man, Woman Car), buyer men women cars Business Rules: The (buyer) can buy many . The (buyer) can buy many .The can be bought by one buyer ( or ). Man Cars Woman Cars Car Man Woman Relations Diagram: Relationship Details: The table should store the and the Buyer table should store the relation between and . Car Buyer ID ID Type Models: polymorphic.models PolymorphicModel from import Buyer(PolymorphicModel):_pass class class_ Woman(Buyer): # ... name = models.CharField(max\_length=255) Man(Buyer): class # ... name = models.CharField(max\_length=255) Car(models.Model): class # Fields name = models.CharField(max\_length=255) buyer = models.ForeignKey( Buyer, on\_delete=models.CASCADE, related\_name='cars' ) Store Records: man = Buyer.objects.get(id=1) # or Man.objecets.get(id=1)woman = Buyer.objects.get(id=2) # or Woman.objecets.get(id=2) # Create relation between buyer (Man/Woman) and Car. man.cars.add(car1, car2) woman.cars.add(car1, car2) # Create relation between Car and buyer (Men/Women). c = Car(name = 'test name', buyer = man)c.save() c = Car(name = 'test name', buyer = woman)c.save() Retrieve Records: # Get buyer (Man/Woman) Cars man.cars.all()woman.cars.all() # Get Car buyer (Man and Woman) car.buyer Polymorphic Many to Many Relationship With package — django-polymorphic . Demo details: In this demo we have 3 models ( and and 4 tables ( , and ). Valet, Owner Car), valets owners, cars drivers Business Rules: The (driver) can drive many . The (driver) can drive many .The can be driven by many drivers ( or/and ). Valet Cars Owner Cars Car Valet Owner Relations Diagram: Relationship Details: The table “drivers” should store the , and the . Pivot Driver ID Driver Type CarID “driver” is a name given to a group of models (Valet and Owner). And it’s not limited to two. The driver type is the real name of the model. Models: polymorphic.models PolymorphicModel from import Driver(PolymorphicModel): class pass Owner(Driver): class # ... name = models.CharField(max\_length=255) Valet(Driver): class # ... name = models.CharField(max\_length=255) Car(models.Model): class # ... name = models.CharField(max\_length=255) drivers = models.ManyToManyField( Driver, related\_name='cars' ) Store Records: # Create relation between driver (Valet/Owner) and Car. owner.cars.add(car1, car2) # Create relation between Car and driver (Valet/Owner). car.drivers.add(owner, valet) Retrieve Records: # Get driver (Valet/Owner) Cars valet.cars.all()owner.cars.all() # Get Car drivers (Valet and Owner) car.drivers.all()car1.drivers.instance_of(Valet)car1.drivers.instance_of(Owner) I also highly recommend to read the , that describes polymorphic many to many relationships with another django package (django-gm2m). article Thanks for reading and stay in . touch
Share Your Thoughts