简体   繁体   English

Django:如何向 ListView 添加查找字段?

[英]Django: How can I add a looked up field to ListView?

I have a model (Listing below) that contains information about ship sailings.我有一个 model(下面的清单),其中包含有关船舶航行的信息。 The ports for the sailings are given by the duple of xxxx_country and xxxx_port.航行的港口由 xxxx_country 和 xxxx_port 的双倍给出。

I list the sailings with a class ListingView based on ListView.我使用基于 ListView 的 class ListingView 列出航行。 I want to pass the port name to the template rather than xxxx_country and xxxx_port, and I can get the name from a Port model.我想将端口名称传递给模板,而不是 xxxx_country 和 xxxx_port,我可以从端口 model 获取名称。 Here's the code:这是代码:

# from listing/models.py

class Listing(models.Model):
    # Port Codes - see https://www.worldnetlogistics.com/seaport-codes/
    orig_country = models.CharField(max_length=2, blank=True)
    orig_port = models.CharField(max_length=3, blank=True)
    dest_country = models.CharField(max_length=2, blank=True)
    dest_port = models.CharField(max_length=3, blank=True)


# from codes/models.py

class Port(models.Model):
    country_code = models.CharField(max_length=2, blank=True)
    location_code = models.CharField(max_length=3, blank=True)
    name = models.TextField()


# from listing/views.py

class ListingView(ListView):
    model = Listing

    # need code here that looks up the port name by combination of country_code and location_code
    # one Port object will be returned
    orig_port_name = ?
    dest_port_name = ?

    template_name = 'listing/home.html'  # change from default template name
    ordering =['-ship_sailing']  # reordering the list in reverse order
    paginate_by = 16

What code do I need to put into the view so that I can pass the orrig_port_name and dest_port_name to the template?我需要将什么代码放入视图中,以便可以将 orrig_port_name 和 dest_port_name 传递给模板?

Best regards...Paul最好的问候......保罗

What you need here is a ForeignKey that refers to Port objects instead of writing the fields like dest_country and dest_post in the model.您需要的是一个引用Port对象的ForeignKey ,而不是在 model 中写入诸如dest_countrydest_post之类的字段。 This results in duplicated data .这会导致重复数据 Duplicated data is an antipattern since it makes data harder to maintain, store, retrieve, and aggregate.重复数据是一种反模式,因为它使数据更难维护、存储、检索和聚合。

class Listing(models.Model):
    # Port Codes - see https://www.worldnetlogistics.com/seaport-codes/
    orig = models.ForeignKey( 'codes.Port',
        on_delete=models.PROTECT,
        related_name='listings_as_orig'
    )
    dest = models.ForeignKey( 'codes.Port',
        on_delete=models.PROTECT,
        related_name='listings_as_dest'
    )

Then you can thus fetch the然后你就可以获取

class ListingView(ListView):
    model = Listing
    queryset = Listing.objects.select_related('orig', 'dest')
    template_name = 'listing/home.html'
    paginate_by = 16

In the template you then can for example list it in a table as:例如,在模板中,您可以在表格中将其列为:

<table>
  {% for object in object_list %}
    <tr>
        <td>{{ object.orig.country_code }}</td>
        <td>{{ object.orig.location_code }}</td>
        <td>{{ object.orig.name }}</td>
        <td>{{ object.dest.country_code }}</td>
        <td>{{ object.dest.location_code }}</td>
        <td>{{ object.dest.name }}</td>
    </tr>
  {% endfor %}
</table>

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

相关问题 如何在Python中查找变量? - How are variables looked up in Python? 如何在Django模型中添加动态字段? - How can I add dynamic field in the model in django? 如何向 Django 中的代理模型添加额外的字段? - How can I add an extra field to a proxied model in Django? 如何在我的Django模型中添加一个字段来动态过滤另一个字段的下拉列表? - How can i add a field to my Django model to dynamically filter a dropdown on another field? 在django中注册时如何使用此功能更新用户字段? - How can I update a user field using this function on sign up in django? 如何选择在 django 表单字段中显示的选项? - How can I choose which choice to show up in django form field? 如何在 django 中制作像谷歌表单一样的表单? 哪里可以根据需要添加或删除字段?用户最多可以添加10-20个字段? - How to make a form in django like google form? where user can add or delete field according to their need?User can add up to 10-20 field? Django 表单不添加文本框 &amp; 不发送用户查找的内容 - Django forms not adding textbox & not sending what user looked up 当我已经在Django中进行数据迁移时,如何在模型中添加新字段? - How can I add a new field in model when I already have a datamigration in Django? 如何加快Django查询的速度? - How can I speed up this Django query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM