0

Please somebody can teachme how create identifying and non identifying relationship in Django, similar the image for reference. Thank you.

Image for reference.

https://i.stack.imgur.com/ai8HP.jpg

  • See [how to ask a good question](https://stackoverflow.com/help/how-to-ask). That way, you will get a faster response specific to your need. – Frankline Aug 19 '22 at 06:51

1 Answers1

0

I guess ForeignKey is what you are looking for as "identifying relationship". Then

# models.py
from django.db import models

class District(models.Model):
    # django will automatically create id field
    # nombre = models.SOMEFIELD()
    pass

class Headquarter(models.Model):
    district = models.ForeignKey(District, on_delete=models.CASCADE)
    # nombre = models.SOMEFIELD()
    # activo = models.SOMEFIELD()

class Building(models.Model):
    # This "district" field looks pointless, as we can simply 
    # follow relation like a_building.headquarter.district
    district = models.ForeignKey(District, on_delete=models.CASCADE)
    headquarter = models.ForeignKey(Headquarter, on_delete=models.CASCADE)
    # nombre = models.SOMEFIELD()

I believe the pattern is obvious enough for you, and I leave Department to your own.

I have no idea what you mean by "non identifying relationship". You might want to give implementation details, so that I can be of more help.

PS: Django is well documented. You can find most Django features there.

PS 2: Please follow @Frankline and re-write your question. Welcome to Stack Overflow.

Ezon Zhao
  • 577
  • 2
  • 14