Django模型-如何建立多个外键关系
发布时间:2022-05-03 13:33:18 288
相关标签: # django
我正在尝试创建一个模型,
一个人可以在多家公司担任多个角色。
就像一家公司可以将一个人分配给董事和MD角色一样,董事角色也可以分配给两个独立的个人。
一个人可以在多家公司担任多个角色,如公司MD和董事会;菲尔姆博士。
我有三个模型,商行,个人和坚定的角色.
我怎样才能做到这一点?
模型.py
from django.db import models
from django.urls import reverse
from django.utils import timezone
class Firm(models.Model):
name = models.CharField(blank=False, max_length=50, null=False)
street_address = models.TextField(blank=False, max_length=100, null=False)
country = models.CharField(blank=False, max_length=25, null=False)
state = models.CharField(blank=False, max_length=25, null=False)
city = models.CharField(blank=False, max_length=25, null=False)
pin_code = models.CharField(blank=False, max_length=25, null=False)
contact = models.CharField(blank=False, max_length=15, null=False, unique=True)
email = models.EmailField(blank=False, null=False, unique=True)
pan = models.CharField(blank=True, max_length=25, null=True)
cin = models.CharField(blank=True, max_length=17, null=True)
gstin = models.CharField(blank=True, max_length=25, null=True)
is_active = models.BooleanField(default=False)
created_by = models.ForeignKey(to='user.User', on_delete=models.CASCADE, blank=False, null=False, related_name="customer_firm", related_query_name="customer_firms")
date_of_creation = models.DateTimeField(blank=False, default=timezone.now, null=False)
last_modified = models.DateTimeField(blank=False, default=timezone.now, null=False)
company = models.ForeignKey(to='company.Company', on_delete=models.CASCADE, blank=True, null=True, related_name="company_company", related_query_name="company_companies")
def __str__(self) :
return self.email
def get_absolute_url(self):
return reverse('cust_firm_detail', kwargs={'pk': self.pk})
class Meta:
ordering = ['id']
class Individual(models.Model):
first_name = models.CharField(blank=False, max_length=25, null=False)
last_name = models.CharField(blank=False, max_length=25, null=False)
street_address = models.TextField(blank=False, max_length=100, null=False)
country = models.CharField(blank=False, max_length=25, null=False)
state = models.CharField(blank=False, max_length=25, null=False)
city = models.CharField(blank=False, max_length=25, null=False)
pin_code = models.CharField(blank=False, max_length=25, null=False)
contact = models.CharField(max_length=10, null=False, blank=False, unique=True)
email = models.EmailField(blank=False, null=False, unique=True)
password = models.CharField(blank=False, editable=False, max_length=256)
pan = models.CharField(blank=True, max_length=25, null=True)
aadhar_no = models.CharField(blank=True, max_length=16, null=True)
is_active = models.BooleanField(default=False)
created_by = models.ForeignKey(to='user.User', on_delete=models.CASCADE, blank=False, null=False, related_name="customer_individual", related_query_name="customer_individuals")
date_of_creation = models.DateTimeField(blank=False, default=timezone.now, null=False)
last_modified = models.DateTimeField(blank=False, default=timezone.now, null=False)
company = models.ForeignKey(to='company.Company', on_delete=models.CASCADE, blank=True, null=True, related_name="company_individual", related_query_name="company_individuals")
def __str__(self) :
return self.email
def get_absolute_url(self):
return reverse('cust_individual_detail', kwargs={'pk': self.pk})
class Meta:
ordering = ['id']
class FirmRole(models.Model):
name = models.CharField(blank=False, max_length=50, null=False)
def __str__(self):
return self.name
class IndividualFirmRole(models.Model):
individual = models.ForeignKey(blank=False, related_name='individual_set', related_query_name='individual', to='Individual', on_delete=models.CASCADE, null=False)
role = models.ForeignKey(blank=True, related_name='role_set', related_query_name='role', to='FirmRole', on_delete=models.CASCADE, null=True)
firm = models.ForeignKey(blank=True, related_name='firm_set', related_query_name='firm', to='Firm', on_delete=models.CASCADE, null=True)
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报