James L.
James L.

Reputation: 1153

create branches of a model in django

I'm creating a clinic directory and some clinics have multiple branches. Each branch is also a proper clinic entity. There are no head office or parent branch. How do I change my models to create branch and also show branches of a clinic, if it exists, in templates?

models.py

class Clinic(models.Model):
    name = models.CharField(max_length=500)
    slug = models.CharField(max_length=200, blank = True, null = True, unique = True)
    contact_no = models.IntegerField() 
    area = models.ForeignKey(Area, blank = True, null = True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Clinic, self).save(*args, **kwargs)

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('m1.views.clinicProfile', args=[str(self.slug)])

views.py

def clinicProfile(request, slug):
    clinic = get_object_or_404(Clinic, slug=slug)
    doctors = Doctor.objects.filter(clinic=clinic)

    d.update({'clinic': clinic, 'doctors': doctors, 'carouselImages':ClinicImage.objects.all() })

    return render(request, 'm1/clinicprofile.html', d)

Upvotes: 0

Views: 228

Answers (1)

Ming
Ming

Reputation: 1693

Here are two common approaches data modeling for this. Pick whichever one better fits your business domain.

  • Clinic contains a ForeignKey to Clinic representing the parent-child relationship. Because many different sub-Clinics can have the same super-Clinic, the sub-Clinic should contain the ForeignKey and it should be named something along the lines of parent_clinic.
  • Introduce another model along the lines of ClinicGroup and relate Clinic to them. That is, Clinic contains a ForeignKey to ClinicGroup named something along the lines of clinic_group.

Upvotes: 1

Related Questions