gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:02 26 4
gpt4 key购买 nike

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

BusinessLocations.objects.all()

错误:QuerySet 对象没有属性“objects”。

无论哪种情况,我都会转储所有表并看到以下内容:

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,这对我来说似乎很奇怪,我想知道我的数据库是否只是陷入困境?

原始问题:

我有两个模型:Business 和 Employee。我希望他们都能互相了解,但不是直接了解,而是通过另一个称为“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

下面是一些伪 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 示例中想要的那样工作。

最佳答案

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

When you define a relationship in a model (i.e., a ForeignKey, OneToOneField, or ManyToManyField), instances of that model will have a convenient API to access the related objects.

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

# 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()

关于python - 在 django 中提供多对多相关模型的模型知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38233245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com