我没有找到关于所有命名参数**kwargs如何在视图调用中的重写“get_context_data”方法中传递的信息。
有3种型号:
class User(models.Model):
email = models.CharField(unique=True)
company_id = models.IntegerField(blank=True, null=True)
first_name = models.CharField(blank=True, null=True)
last_name = models.CharField(blank=True, null=True)
class Company(models.Model):
logo = models.CharField(blank=True, null=True)
name = models.CharField(blank=True, null=True)
full_name = models.CharField(blank=True, null=True)
address = models.CharField(blank=True, null=True)
phone = models.CharField(blank=True, null=True)
email = models.CharField(blank=True, null=True)
kpp = models.CharField(blank=True, null=True)
okved = models.CharField(blank=True, null=True)
bik = models.CharField(blank=True, null=True)
class Order(models.Model):
user_id = models.IntegerField(blank=True, null=True)
amount_total_cents = models.BigIntegerField()
amount_mean_cents = models.BigIntegerField()
created_at = models.DateTimeField()
模型之间的通信:
公司-用户1至M,用户-订单1至M
在视图中直接输出数据而不覆盖“get_context_data”方法没有问题。但是,在重写该方法并通过主键链接两个表时没有问题:
**1.**公司/urls.py
urlpatterns = [
path('company/<int:pk>', CompanyShow.as_view(), name='company_show')
]
公司/views.py
class CompanyShow(DetailView):
model = Company
template_name = 'company/company_show.html'
context_object_name = 'company'
def get_context_data(self, **kwargs):
context = super(CompanyShow, self).get_context_data(**kwargs)
context['users'] = User.objects.filter(company_id = self.kwargs['pk'])
return context
company_show.html
<p>ID: {{ company.id }}</p>
<p>Name: {{ company.name }}</p>
<p>Members:
{% for user in users %}
<li>
<a href="{% url 'user:user_show' user.id %}">{{ user }}</a>
</li>
{% endfor %}
</p>
**2.**user/urls.py
urlpatterns = [
path('user/<int:pk>', UserShow.as_view(), name='user_show')
]
用户/views.py
class UserShow(DetailView):
model = User
template_name = 'user/user_show.html'
context_object_name = 'user'
user_show.html
<p>ID: {{ user.id}}</p>
<p>Name: {{ user.first_name }}</p>
<p>Surname: {{ user.last_name }}</p>
{% if user.company_id %}
<a href="{% url 'company:company_show' user.company_id %}">
Company profile (id {{ user.company_id }})
</a>
{% else %}
No company
{% endif %}
问题如下:
应该从**kwargs向order/ www.example.com传递什么匹配参数views.py以便根据“user_id”字段的“order”表中的“user”表“id”过滤所需对象的列表?根据结果,需要在.html中显示公司-下一步如何连接“用户”和“公司”表?
- 然而,困难在于当绑定按顺序进行时 *:
订购/urls.py
urlpatterns = [
path('order/<int:pk>', OrderShow.as_view(), name='order_show')
]
订购/views.py
class OrderShow(DetailView):
model = Order
template_name = 'order/order_show.html'
context_object_name = 'order'
def get_context_data(self, **kwargs):
context = super(OrderShow, self).get_context_data(**kwargs)
context['users'] = User.objects.filter(id = self.kwargs['user_id'])
return context
order_show.html
<p> Member:
{% for user in users %}
<li>
<a href="{% url 'user:user_show' user.id %}">{{ user }}</a>
</li>
{% endfor %}
</p>
在所描述的实现中,错误:
Exception Type: KeyError
Exception Value: 'user_id'
1条答案
按热度按时间0qx6xfy61#
这是一个简单的解决方案,如果不附加到**kwargs:
context['users'] = User.objects.filter(id = self.object.user_id)
最终视图: