在django中提供多对多相关模型的模型知识

时间:2016-07-06 20:31:12

标签: python django django-models

编辑:鉴于评论和答案中的回答我尝试了建议,并且在尝试查询时遇到了一些错误,同时执行相关名称查询也没有得到正确的结果(如评论中所示)

BusinessLocations.objects.all()

Error: QuerySet object has no attribute 'objects' is the error.

在任何一种情况下,我都会对所有表进行转储并看到:

auth_business_permissions', u'auth_permission', u'auth_user', u'auth_user_businesss', u'auth_user_user_permissions', u'django_admin_log', 
u'django_content_type', u'django_migrations', u'django_session', u'ipaswdb_address', u'ipaswdb_billing', u'ipaswdb_billing_businesss', 
u'ipaswdb_designation', u'ipaswdb_business', u'ipaswdb_business_business_locations', u'ipaswdb_businessinsurances', u'ipaswdb_businessinvoices', 
'ipaswdb_businesslocations', u'ipaswdb_businessterm', u'ipaswdb_insurance', u'ipaswdb_insurance_businesss', u'ipaswdb_invoice', u'ipaswdb_employee',
u'ipaswdb_employeeinvoice', u'ipaswdb_employeelocations', u'ipaswdb_employeeterms', u'ipaswdb_specialty']

我有一个ipaswdb_business_business_locations和一个ipaswdb_businesslocations这对我来说很奇怪,我想知道我的数据库是不是只是笨拙了?

Original Question:

我有两个模型Business和一个员工。我希望他们两个都能彼此了解但不是直接相关,而是通过另一个名为' BusinessesLocation`的模型。我可以在我的模型中表达这一点,但它看起来或感觉不对。这就像只有员工了解业务,而不是相反。

我打开了另一个问题试图回答这个问题,但答案并非100%正确,因为它没有提供多对多的答案,更像是一对多。在这种情况下:员工可以在许多地方工作(可能是许多企业的员工),而企业可以拥有许多拥有许多员工的地点。

目前我的模型适用于此shell脚本的工作位置:

someEmployee.business_locations.all()[0].business.business_name 它工作正常,我可以获得一个员工工作的所有业务位置,并通过推断出员工可能为企业所在地工作的许多业务。

但我无法弄清楚如何走另一条路,找出一家公司为他们工作的所有员工以及在哪些地方

我目前的(错误的)模型是这样的:

class Employee(models.Model):
        first_name = models.CharField(max_length = 50)
        business_locations = models.ManyToManyField('BusinessLocations', through='EmployeeLocations')


class EmployeeLocations(models.Model):
    employee = models.ForeignKey('Employee', on_delete=models.CASCADE)
    business_location = models.ForeignKey('BusinessLocations', on_delete=models.CASCADE)
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)
    def __str__(self):
        return self.provider.first_name

class BusinessLocations(models.Model):
    address = models.ForeignKey('Address', on_delete= models.SET_NULL, null=True)
    business = models.ForeignKey('Business', on_delete=models.CASCADE)
    doing_business_as = models.CharField(max_length = 255)
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)
    def __str__(self):
        return self.doing_business_as

class Business(models.Model):
    business_name = models.CharField(max_length=50)
    business_locations = I need something here no idea how

Bellow是一些伪shell代码,演示了我希望我的模型如何工作:

#create a new business location assume business has been created
newLocation = Address(...)
business.business_locations.add(newLocation, doing_business_as='alternative name maybe')


#assume employee exists
#add a new business location to the employee
#when i say selected business the form would have current employee then in its locations
#you'd have to select a business first, and get a list of all that businesses locations and you
#you could add the business location and then select another business with all ITS locations
# and add one there too if you wish

employee.employee_locations.add(selectedBusiness.business_locations[0])
employee.employee_locations.add(anotherSelectedBusiness.business_locations[1])

Below is what I cannot figure out how to do, vice versa... 

#now lets see which businesses the employee works for.

for business in employee.business_locations
       business.business_name

#and lets see each businesses employees:
for employee in Employee.objects.all()
       employee.
?? No idea how to build the models to represent these relationships

我可以很好地获得员工的营业地点,但我无法获得上述获取企业员工列表的示例。不知道我需要调整什么(或者我可能需要的方法?),以便在我的shell示例中实现这一点。

1 个答案:

答案 0 :(得分:1)

你所缺少的是Django的related objects概念。

  

在模型中定义关系时(即ForeignKeyOneToOneFieldManyToManyField),该模型的实例将具有访问相关对象的便捷API。

您可以在查询中访问相关对象,也可以在模型上访问管理器属性。请参阅文档中的示例。在你的情况下,这看起来像:

# Now lets see which businesses the employee works for:
Business.objects.filter(businesslocations__employee=employee).distinct()

# And let's see each business's employees:
Employee.objects.filter(business_locations__business=business).distinct()