如何在django中获取相关集合(多对多字段)
我有一个员工模型如何在django中获取相关集合(多对多字段),django,django-models,django-templates,django-views,Django,Django Models,Django Templates,Django Views,我有一个员工模型 class Employees(models.Model): name = models.CharField(max_length=200, help_text="Enter the name of the employee") location = models.ManyToManyField('Coverage') def __str__(self): return self.name' 和一个位置模型 class Coverage(models.Model):
class Employees(models.Model):
name = models.CharField(max_length=200, help_text="Enter the name of the employee")
location = models.ManyToManyField('Coverage')
def __str__(self):
return self.name'
和一个位置模型
class Coverage(models.Model):
name = models.CharField(max_length=300, help_text="Enter employee location")
time_needed = models.IntegerField(help_text="Enter time needed to get to work. eg (40 = > 40 minutes)")
def __str__(self):
return self.name
如何使用(employees=employees.objects.all())从模板中访问所需的覆盖率时间
我试过{{employees.location.time}}
,但不起作用。任何好的答复都将不胜感激
ps:这只是一个更大模型类的一个片段,employees=employees.objects.all()是一个查询集,您不能用它访问字段。但是,如果您对它进行迭代,您将可以访问每个实例,然后您将能够拥有任何实例的
location
。因为location
是一个ManyToManyFieldlocation=models.ManyToManyField('location')
。您还需要对其进行迭代
{% for employee in employees %}
{{ employee.location.all }} // Location queryset
{% for location in employee.location.all %}
{{ location.time_needed }}
{% endfor %}
{% endfor %}
如果您真的不需要遍历这些字段。您可以使用slice
选择一个实例
{{employees.0}
将根据查询集顺序选择第一个employees实例
{{employee.0.location.all.0}
根据查询集顺序选择第一个位置实例
{{employee.0.location.all.0.time_needed}
将允许您访问该值
或者更清楚:
{% with employees.0 as employee %}
{% with location as employee.location.all.0 %}
{{ location.time_needed }}
{% endwith %}
{% endwith %}
employees=employees.objects.all()。但是,如果您对它进行迭代,您将可以访问每个实例,然后您将能够拥有任何实例的location
。因为location
是一个ManyToManyFieldlocation=models.ManyToManyField('location')
。您还需要对其进行迭代
{% for employee in employees %}
{{ employee.location.all }} // Location queryset
{% for location in employee.location.all %}
{{ location.time_needed }}
{% endfor %}
{% endfor %}
如果您真的不需要遍历这些字段。您可以使用slice
选择一个实例
{{employees.0}
将根据查询集顺序选择第一个employees实例
{{employee.0.location.all.0}
根据查询集顺序选择第一个位置实例
{{employee.0.location.all.0.time_needed}
将允许您访问该值
或者更清楚:
{% with employees.0 as employee %}
{% with location as employee.location.all.0 %}
{{ location.time_needed }}
{% endwith %}
{% endwith %}
我想,你可以这样做:
employees = Employees.objects.all()
for employee in employees:
for location in employee.location.all():
print(location.time_needed)
Coverage.objects.filter(id__in=employee.location.all().values('id')).values('time_needed')
# This will return time_needed of all the Coverage whose id matched the location ids of employee
如果您想访问特定的覆盖范围,则可以执行以下操作:
employees = Employees.objects.all()
for employee in employees:
for location in employee.location.all():
print(location.time_needed)
Coverage.objects.filter(id__in=employee.location.all().values('id')).values('time_needed')
# This will return time_needed of all the Coverage whose id matched the location ids of employee
我想,你可以这样做:
employees = Employees.objects.all()
for employee in employees:
for location in employee.location.all():
print(location.time_needed)
Coverage.objects.filter(id__in=employee.location.all().values('id')).values('time_needed')
# This will return time_needed of all the Coverage whose id matched the location ids of employee
如果您想访问特定的覆盖范围,则可以执行以下操作:
employees = Employees.objects.all()
for employee in employees:
for location in employee.location.all():
print(location.time_needed)
Coverage.objects.filter(id__in=employee.location.all().values('id')).values('time_needed')
# This will return time_needed of all the Coverage whose id matched the location ids of employee
我看不出这两个模型之间有任何联系location=models.ManyToManyField('location')
location
model在哪里?感谢您指出这一点。我会更新模型。我看不到这两个模型之间有任何联系location=models.ManyToManyField('location')
locationmodel在哪里?感谢您指出这一点。我会更新模型