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

Python Django:如何向ListView添加查找字段?,python,django,Python,Django,我下面有一个模型列表,其中包含有关船舶航行的信息。开航港口由xxxx_国家和xxxx_港口的两个代表给出 我使用基于ListView的类ListingView列出航行。我想将端口名传递给模板,而不是xxxx\u国家/地区和xxxx\u端口,并且我可以从端口模型获取名称。代码如下: # from listing/models.py class Listing(models.Model): # Port Codes - see https://www.worldnetlogistics.c

我下面有一个模型列表,其中包含有关船舶航行的信息。开航港口由xxxx_国家和xxxx_港口的两个代表给出

我使用基于ListView的类ListingView列出航行。我想将端口名传递给模板,而不是xxxx\u国家/地区和xxxx\u端口,并且我可以从端口模型获取名称。代码如下:

# 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

我需要在视图中输入什么代码,以便将orrig\u port\u名称和dest\u port\u名称传递给模板

致以最诚挚的问候…Paul

这里您需要的是引用端口对象的ForeignKey,而不是在模型中写入dest_country和dest_post等字段。这会导致重复的数据。重复数据是一种反模式,因为它使数据更难维护、存储、检索和聚合

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'
    )
这样你就可以拿到

class ListingView(ListView):
    model = Listing
    queryset = Listing.objects.select_related('orig', 'dest')
    template_name = 'listing/home.html'
    paginate_by = 16
例如,在模板中,您可以将其列在表格中,如下所示:

<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>

我想你需要用一把外国钥匙来改造它。Django模型是关于有效地存储、检索和聚合数据的。通过使用ForeignKey,Django的ORM也可以更有效地聚合:为了点i,关闭i@威廉·瓦农森-谢谢。这看起来很有希望。我输入了清单模型更改,运行了makemigrations和migrate,并得到一个请求默认值的提示。我在每个ForeignKey实例化中添加了default=1。我得到了结果,但始终是端口模型中的第一个默认条目。我不需要指定指定连接条件的内容吗?在SQL中,我会说JOIN ON listing.country\u code=port.country\u code,listing.port\u code=port.location\u code。抱歉,如果这听起来很愚蠢-我是Django新手。@PaulOfford:不,与.select\u相关的。。将进行连接并选择数据,但将其包装到附加到列表对象的orig和dest属性的端口对象中。因此,Django ORM使它看起来像是您获取了对象。@WillemVanOnsem-谢谢。我现在明白你的意思了。我从错误的角度看它。我们所做的是在创建每个列表项时存储对正确端口项的引用,从而连接表。这比我想的要好。