简体   繁体   English

在Django中创建模型的分支

[英]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 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 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)

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. Clinic包含ForeignKeyClinic表示父子关系。 Because many different sub- Clinic s 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 . 因为许多不同的子Clinic可以具有相同的超级Clinic ,所以该子Clinic应该包含ForeignKey ,并且应该沿parent_clinic
  • Introduce another model along the lines of ClinicGroup and relate Clinic to them. 沿ClinicGroup引入另一种模型,并将Clinic与它们相关联。 That is, Clinic contains a ForeignKey to ClinicGroup named something along the lines of clinic_group . 也就是说, Clinic包含一个指向ClinicGroupForeignKeyClinicGroup名称类似于Clinicclinic_group

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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