我有两个基本模型SiteData和Showoroom服务,它们的模型结构如下所示。在
我需要SiteData信息,但我也想从showroomservice模型中获取link_类型,如果有匹配的ID
到目前为止,我尝试了一些事情,但都没有得到我所需要的,实现这一点的最佳方法是什么?在
谢谢
选择相关
>>> nd = ShowroomService.objects.select_related('site').all()
>>> nd
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 229, in __repr__
return '<%s %r>' % (self.__class__.__name__, data)
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 590, in __repr__
u = six.text_type(self)
TypeError: __str__ returned non-string (type SiteData)
组合:
^{pr2}$连锁
>>> final_data = chain(monitoring_data, site_data)
>>> for i in final_data:
... '{} {}'.format(i.location,i.link_Type)
...
Traceback (most recent call last):
File "<console>", line 2, in <module>
AttributeError: 'ShowroomService' object has no attribute 'location'
在sites.models.py在
class SiteData(models.Model):
location = models.CharField(max_length=50)
site_type = models.ForeignKey(SiteTypes, verbose_name="Site Type", \
on_delete=models.PROTECT)
subnet = models.GenericIPAddressField(protocol='IPv4')
routed_subnet = models.GenericIPAddressField(protocol='IPv4', \
verbose_name="Routed Link Subnet", blank=True, null=True)
bgp_as = models.CharField(max_length=6, verbose_name="BGP AS Number")
opening_date = models.DateField(verbose_name="Showroom opening date")
last_hw_refresh_date = models.DateField(verbose_name="Date of latest hardware refresh", \
blank=True, null=True)
is_live = models.BooleanField(default=False, verbose_name="Is this a live site?")
tel = models.CharField(max_length=20, blank=True, null=True)
address = models.CharField(max_length=255, blank=True, null=True)
town = models.CharField(max_length=255, blank=True, null=True)
...
class Meta:
verbose_name = "Site Data"
verbose_name_plural = "Site Data"
ordering = ('location',)
permissions = (
("can_view", "Can View"),
("can_view_mgmt", "Can View Management"),
)
def __str__(self):
return self.location
在监控.models.py在
from sites.models import SiteData
class ShowroomService(models.Model):
site = models.ForeignKey(SiteData, verbose_name="Site", \
on_delete=models.PROTECT)
link_type = models.CharField(max_length=200, blank=True, null=True)
preference = models.CharField(max_length=200, blank=True, null=True)
timestamp = models.DateTimeField(auto_now_add=True, blank=True, null=True)
dashboard = models.BooleanField(default=True, verbose_name="display on monitoring dashboard?")
class Meta:
verbose_name = "Showroom Service Data"
verbose_name_plural = "Showroom Service Data"
def __str__(self):
return self.site
您可以使用django的“related manager”拉取所有相关对象。此处提供文档:https://docs.djangoproject.com/en/2.0/ref/models/relations/#related-objects-reference
通过对
SiteData
模型调用showroom_service_set
,您可以获得每个SiteData
的子记录集。在只是为了解释为什么列出的尝试也失败了:
__str__
方法需要返回字符串。如果他们不这样做,你会得到例外。在|
用于OR
在django中组合查询。这段代码试图组合两种不同类型模型的查询。在location
属性。在下面是一段代码,用于获取附加到SiteData模型的所有ShowroomService模型的链接类型:
我不确定django如何处理带有相关对象的camel大小写,所以
showroom_service_set
是我的最佳猜测。你可能需要做一点腿部的工作来找出真正的集合是什么。在编辑:有一种叫做预取相关的东西。{我来找你的答案是什么
相关问题 更多 >
编程相关推荐