Не удается установить само присоединение в приложении Django - PullRequest
0 голосов
/ 24 октября 2019

Я хочу отобразить имя спонсоров, которые также являются клиентами (мобильное поле). Мы можем понять это из модели данных. На странице просмотра я хочу показать имя спонсора, но не могу установить соединение между спонсором и его именем. Здесь мобильный номер клиента.

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 %}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...