简体   繁体   中英

How create identifying and non identifying relationship in Django

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM