django - 无法在 Django App 中建立自我加入
问题描述
我想显示赞助商的名称,这些赞助商也是客户(移动领域)。我们可以从数据模型中理解这一点。在视图页面中,我想显示赞助商的名称,但我无法在赞助商及其名称之间建立连接。这里的手机是客户号码。
Model.py
-----------
class customer(models.Model):
company = models.CharField(max_length=3)
mobile = models.CharField(max_length=10)
name = models.CharField(max_length=100)
sponsor = models.CharField(max_length=10) # Sponsor is also a customer like employee and manager
address1 = models.CharField(max_length=200)
country = models.CharField(max_length=101)
state = models.CharField(max_length=100)
city = models.CharField(max_length=100)
zip = models.CharField(max_length=6)
email = models.EmailField(max_length=100)
status = models.CharField(max_length=1)
creator = models.CharField(max_length=20)
cretime = models.DateTimeField(default=datetime.now)
updator = models.CharField(max_length=20)
updtime = models.DateTimeField(default=datetime.now, blank = True )
views.py
--------
from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
import datetime
from .models import customer
from django.db import transaction
from django.contrib import messages
import re
def customer_view(request):
name1 = str(request.GET.get('nam'))
mobile1 = str(request.GET.get('mob'))
if (name1 == 'None' and mobile1 == 'None'):
customers_list = customer.objects.all().order_by('-cretime')
elif (name1 == '' and mobile1 == ''):
customers_list = customer.objects.all().order_by('-cretime')
elif (name1 != '' and mobile1 == ''):
customers_list = customer.objects.filter(name=name1).order_by('-cretime')
elif (name1 == '' and mobile1 != ''):
customers_list = customer.objects.filter(mobile=mobile1).order_by('-cretime')
else:
customers_list = customer.objects.filter(mobile=mobile1) & customer.objects.filter(name=name1).order_by('-cretime')
sponsors = customer.objects.all().distinct('mobile')
ctx = { 'customer': customers_list, 'sponsor': sponsors }
return render(request, 'pages/customer_view.html', ctx)
customer_view.html (In place of i.sponsor i want to show the corresponding name)
------------------------------------------------------------------------------------
{% if customer %}
{% for i in customer %}
<tbody>
<tr>
<th scope="row">1</th>
<td>{{ i.id }} </td>
<td>{{ i.mobile }} </td>
<td>{{ i.name }} </td>
<td>{{ i.sponsor }} </td>
</tr>
</tbody>
{% endfor %}
{% endif %}
解决方案
推荐阅读
- c# - dotnet-trace 不解析 linux-arm 下的符号
- linux - 如何找到使用 _do_fork() 创建新进程的基本例程?
- javascript - 为什么在向上和向下缩放容器后将指针向下事件传递给错误的 PIXI.Container?附上测试用例
- javascript - 如何根据值使猫鼬模式动态化?
- python - 为什么 QTableView 有空白边距,我该如何删除它们?
- php - Wordpress 自定义小部件显示在错误的位置
- python - 使用机器人阅读 Discord 频道消息
- python - 访问列表元素的while循环问题
- reactjs - 如何获取输入字段的值并在 React 的 URL 中使用它
- arrays - 将 JSON 对象转换为新数组