0

I'm experimenting with Djongo right now: Is there a possibility to reference the djongo nested models in another model as a foreign key? Or how would you deal with this structure in djongo? Clinic 1:n Wards - Ward 1:n Cases (The cases should still be in their own collection.)

class Clinic(models.Model):
...
    wards = models.ArrayModelField(model_container=Ward, null=True,)
...

class Case(models.Model):

 ward = models.ForeignKey(
        'clinic.ward', on_delete=models.PROTECT)
Gurkenkönig
  • 678
  • 15
  • 36

2 Answers2

0

I would do it this way

class Clinic(models.Model):
    ...

class Ward(models.Model):
    clinic = models.ForeignKey('Clinic', related_name='wards', on_delete=models.PROTECT)

class Case(models.Model):
    ward = models.ForeignKey('Ward', related_name='cases' on_delete=models.PROTECT)
Nour Wolf
  • 2,140
  • 25
  • 24
0

I think you should rewise in the way of looking NoSQL, its not necessary to keep relation between objects, may be its even better to duplicate data (embedding).

If you are sure to make relation, and you really need the relation here is how you can do it in django using djongo.

I guess you'll find out in the first steps to remove the relation and embed what is necessary.

Salehi
  • 362
  • 2
  • 13