简体   繁体   English

如何显示Django中设置的相关对象?

[英]How do I display a related object set in django?

I know the question seems simple to answer, and that the answer would be in the API ref somewhere, but I'm a bit new to django and have poured through the API for days trying to get this done. 我知道这个问题似乎很容易回答,并且答案会在API ref的某个地方,但是我对django有点陌生,并且已经尝试了好几天才能完成此工作。 Any help would be highly appreciated! 任何帮助将不胜感激!

I'm creating a basic blogging app for my University's computer labs, and in this app you have two relevant pages. 我正在为大学的计算机实验室创建一个基本的博客应用程序,在此应用程序中,您有两个相关页面。 A locations page which displays information about each lab on campus (hours, location/building), and a resources page which displays information about the equipment in each lab. 位置页面显示有关校园中每个实验室的信息(时间,位置/建筑物),资源页面显示有关每个实验室中设备的信息。

I have my models.py file set up as shown (there are other models but these are the only relevant ones: 如图所示,我已经设置了models.py文件(还有其他模型,但是这些是唯一相关的模型:

class Location(models.Model):
    name = models.CharField(max_length=100)
    computer = models.ForeignKey('Computer')
    printer = models.ForeignKey('Printer')

class Computer(models.Model):
    computer_location = models.ForeignKey('Location', related_name='lab_computers')
    computer_model = models.ForeignKey('HardwareModel')

class Printer(models.Model):
    printer_location = models.ForeignKey('Location', related_name='lab_printers')
    printer_model = models.ForeignKey('HardwareModel')

class HardwareModel(models.Model):
    brand = models.CharField(max_length = 100)
    model_num = models.CharField(max_length = 30)

My views.py file: 我的views.py文件:

class LocationsView(generic.ListView): #This isn't the view to be concerned with
    template_name = 'blog/location_list.html'
    context_object_name = 'locations'
    queryset = Location.objects.all()

#This is commented out because i was trying to modify the view to allow extra context
#I'm aware that you can't have 2 views with the same name. Just assume only one is active
#class ResourcesView(generic.ListView):
#    context_object_name = 'locations'
#    queryset = Location.objects.all()

def ResourcesView(request):
    locations = Location.objects.prefetch_related('lab_printers').all()
    return render_to_response('blog/resource_list.html',
          {'locations': locations})

The template that I need to display this in is like this: 我需要在其中显示的模板是这样的:

<table>
    <tr>
        <th>Lab</th>
        <th>Device</th>
        <th>Type</th>
        <th>Model</th>
    </tr>
    {% for location in locations %}
    <tr>
        <td rowspan="{{ location.lab_printers.count }}">{{ location.name }}</td>
        <td>Computer</td>
        <td>{{ location.computer.computer_model.brand }}</td>
        <td>{{ location.computer.computer_model.model_num }}</td>
    </tr>
    <tr>
        <td>Printer</td>
        <td>{{ location.printer.printer_model.brand }}</td>
        <td>{{ location.printer.printer_model.model_num }}</td>
    </tr>
{% endfor %}
</table>

The point of displaying it like this is the the University has strict guidelines on how information on the web pages are presented and they want everything in neat organized tables. 像这样显示它的重点是大学对如何显示网页上的信息有严格的指导,并且他们希望所有内容都以整齐有序的表格显示。 The table needs to have a dynamic rowspan on the first column to support people adding or deleting computer/printer types through django admin. 该表的第一列需要有一个动态行跨度,以支持人们通过django admin添加或删除计算机/打印机类型。

I searched through StackOverflow and found a few issues similar to mine and they said to use prefetch_related to get the related objects in one batch query, however when I did this I began to get a TypeError "RelatedManager" is not an iterable when trying to access the printers in the template. 我搜索了StackOverflow,发现了一些与我类似的问题,他们说它们使用prefetch_related在一个批处理查询中获取相关对象,但是当我这样做时,我开始遇到一个TypeError“ RelatedManager”在尝试访问时不是可迭代的模板中的打印机。 I understand why this is happening. 我了解为什么会这样。 It's because the RelatedManager class handling the query does not return a list/array/tuple. 这是因为处理查询的RelatedManager类不会返回列表/数组/元组。 I just don't know how in the heck I can access the information I need in the way I need it. 我只是不知道该如何以所需方式访问所需信息。

I did have it partially working with the commented-out Generic View, however when I use location.lab_printers.count The values came back inconsistent and didn't reflect the actual objects in the database, messing up the formatting, and resulting in missing td elements and inaccurate display. 我确实有部分使用注释掉的通用视图,但是当我使用location.lab_printers.count ,值返回不一致,并且没有反映数据库中的实际对象,弄乱了格式,并导致丢失了td元素和不准确的显示。

Sorry if I'm rambling, this is just a complex problem and I have absolutely run out of ideas on how I should go about doing this. 很抱歉,如果我在四处闲逛,这只是一个复杂的问题,我对于如何执行此操作绝对没有想法。

Any and all help would be so very greatly appreciated. 任何和所有帮助将非常感激。 Thank you for your time! 感谢您的时间!

Not the answer, but use ManyToManyField for Location-Computer and Location-Printer relations. 不是答案,而是将ManyToManyField用于Location-ComputerLocation-Printer关系。 Docs link1 , link2 . 文件link1link2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM