首页 > 解决方案 > 无法在 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 %}

标签: django

解决方案


推荐阅读