我正在努力寻找如何从表亲相关模型中获取数据的文档或示例。
所以如果模型看起来像这样:
class Part(models.Model):
name = models.CharField(max_length=550)
class Quantity(models.Model):
quantity = models.DecimalField(max_digits=10, decimal_places=2)
part = models.ForeignKey('Part', related_name='quantity_part')
stockarea = models.ForeignKey('StockArea', related_name='quantity_stockarea')
class Stock(models.Model):
name = models.CharField(max_length=550)
class StockArea(models.Model):
area = models.CharField(max_length=550)
stock = models.ManyToManyField(Stock, related_name='stockarea_stock')
def __str__(self):
return self.area
在视图中我得到了这样的部分:
def details(request, part_id):
part = get_object_or_404(Part, pk=part_id)
context = {
'part': part,
}
return render(request, 'part/details.html', context)
最后模板尝试显示数据:
{% for a in part.quantity_part.all %}
{{ a.quantity }} pcs
Find part in area: {{ a.stockarea }}
in stock: {{ part.stockarea.stock.name }}
{% endfor %}
你看我怎么试着得到股票的名称。我无法弄清楚如何能够掌握股票的名称。我从那里开始有一条路。
部分对名为related_name
的数量模型quantity_park
。在模型数量中,我与模型 StockArea 有关系。从那里我与模型股票有关系。
非常感谢指导=)
也许我完全倒退了。也许我开始定义错误的模型。我已经习惯了MySQL,所以这对我来说很新鲜。
答案 0 :(得分:0)
这样做的数据模型更好:
from django.db import models
class Unit(models.Model):
name = models.CharField(max_length=32)
description = models.TextField()
abbrev = models.CharField(max_length=7)
class Warehouse(models.Model):
name = models.CharField(max_length=100)
address = models.TextField()
class StockArea(models.Model):
warehouse = models.ForeignKey(Warehouse)
# Adjust type of these identifiers as necessary
aisle = models.PositiveIntegerField()
shelf = models.PositiveIntegerField()
class Part(models.Model):
name = models.CharField(max_length=550)
description = models.TextField()
class Stock(models.Model):
part = models.ForeignKey(Part, related_name='stock') # Adds a 'stock' attribute to 'Part'
quantity = models.PositiveIntegerField()
unit = models.ForeignKey(Unit)
location = models.ForeignKey(StockArea)
查看代码:
from django.views import generic
from .models import Part
class PartView(generic.DetailView):
# Pre-fetch related objects. This also illustrates the joins
queryset = Part.objects.prefetch_related(
'stock', 'stock__location', 'stock__location__warehouse'
)
template_name = 'yourapp/part/detail.html'
模板代码yourapp/part/detail.html
:
{% extends "base.html" %}
{% block content %}
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-8 col-md-offset-2">
<h1 class="title">{{ part.name }}</h1>
<p>{{ part.description }}</p>
<h2>Stock information</h2>
<div class="container-fluid">
{% for stock in part.stock.all %}
<div class="row">
<div class="col-xs-3">
Aisle {{ stock.location.aisle }}, shelf {{ stock.location.shelf }}
</div>
<div class="col-xs-3 label">
Warehouse:
</div>
<div class="col-xs-6">
{{ stock.location.warehouse.name }}
<address>
{{ stock.location.warehouse.address }}
</address>
</div>
</div>
<div class="row">
<div class="col-xs-3 label">
Available:
</div>
<div class="col-xs-8 numeric">
{{ stock.quantity }}
</div>
<div class="col-xs-1 unit">
{{ stock.unit.abbrev }} <sup><i class="fa fa-icon" title="{{ stock.unit.name }}"></i></sup>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</div>
{% endblock content %}
修改强>: