[英]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_country
和dest_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.