Home > Enterprise >  matching query does not exist django V3.2.9
matching query does not exist django V3.2.9

Time:11-28

I am trying to retrieve the ID from a POST request which throws matching query does not exist when i am certain that specific ID for that customer exists because all it's information is displayed at the template table also using {{customer.id}} in the template gives an (ID) to make sure that it actually exists

My views.py

from .models import Customer,FilterMaintenance

def index(request):
    if request.method == "POST":
        filtermaintenance_id = FilterMaintenance.objects.get(pk=request.POST.get('{{customer.id}}', False))
        x = filtermaintenance_id.filter_last_due_date
    f = FilterMaintenance.objects.all()
    return render(request,'maintenance/index.html',{        
        "customers_maintenance":f,
        "test":x
        })

my models.py

class Filter(models.Model):
filter_brand= models.CharField(max_length=64)
filter_interval = models.IntegerField()

def __str__(self):
    return f"{self.id} , {self.filter_brand}"

class Customer(models.Model):
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
phone_number = models.IntegerField(blank=True)
home_address = models.CharField(max_length=64,blank=True)
email = models.EmailField(blank=True)
notes = models.TextField(blank=True)

def __str__(self):
    return f"{self.id}, {self.first_name}, {self.last_name}"

class FilterMaintenance(models.Model):
customer_info = models.ForeignKey(Customer,related_name="customer_set",on_delete = models.CASCADE)
customer_filter = models.ForeignKey(Filter,related_name="filter_set",on_delete = models.CASCADE)
filter_last_due_date = models.DateField()


def __str__(self):
    return f"{self.id}, {self.customer_info}, {self.customer_filter},{self.filter_last_due_date} "

my index.html

{% extends "maintenance/layout.html" %}

{% block body %}
 <div class="maintenance-container">
 <h1>Section for customers ...</h1>

<table class="maintenance-table"><thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Phone number</th>
            <th>Home address</th>       
            <th>Filter brand</th>
            <th>Last filter change</th>
            <th>Next filter due date</th>
            <th>Filter done</th>
        </tr>
    </thead>
    <tbody>
        {% for customer in customers_maintenance %}
        <tr>
            <td>{{customer.customer_info.first_name}}</td>
            <td>{{customer.customer_info.last_name}}</td>
            <td>{{customer.customer_info.phone_number }}</td>
            <td>{{customer.customer_info.home_address}}</td>
            <td>{{customer.customer_filter.filter_brand}}</td>
            <td>{{customer.filter_last_due_date}}</td>
            <td>{{test}}</td>
            <td><form action="" method="post">
                {% csrf_token %}
                <input type="submit" name="{{customer.id}}" value="Done">
            </form></td>                
        </tr>
        {% endfor %}
    </tbody>
</table></div>{% endblock %}

                             

Traceback

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/maintenance/

Django Version: 3.2.9
Python Version: 3.10.0
Installed Applications:
['maintenance',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\PC\Desktop\yamama\maintenance\views.py", line 8, in index
    filtermaintenance_id = FilterMaintenance.objects.get(pk=request.POST.get('{{customer.customer_info.id}}', False))
  File "C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 435, in get
    raise self.model.DoesNotExist(

Exception Type: DoesNotExist at /maintenance/
Exception Value: FilterMaintenance matching query does not exist.

CodePudding user response:

you were getting an error because of these two lines of code

filtermaintenance_id = FilterMaintenance.objects.get(pk=request.POST.get('{{customer.id}}', False))

and this

<input type="submit" name="{{customer.id}}" value="Done">

try this

from .models import Customer,FilterMaintenance

def index(request):
    x = []
    if request.method == "POST":
        filtermaintenance_id = FilterMaintenance.objects.get(pk=request.POST.get('custome_id',1))
        x = filtermaintenance_id.filter_last_due_date
        f = FilterMaintenance.objects.all()
        return render(request,'maintenance/index.html',{        
        "customers_maintenance":f,
        "test":x
        })

    f = FilterMaintenance.objects.all()
    return render(request,'maintenance/index.html',{        
        "customers_maintenance":f,
        "test":x
        })

change this

{% for customer in customers_maintenance %}
<tr>
    <td>{{customer.customer_info.first_name}}</td>
    <td>{{customer.customer_info.last_name}}</td>
    <td>{{customer.customer_info.phone_number }}</td>
    <td>{{customer.customer_info.home_address}}</td>
    <td>{{customer.customer_filter.filter_brand}}</td>
    <td>{{customer.filter_last_due_date}}</td>
    <td>{{test}}</td>
    <td><form action="" method="post">
        {% csrf_token %}
        <input type="submit" name="{{customer.id}}" value="Done">
    </form></td>                
</tr>
{% endfor %}

to

{% for customer in customers_maintenance %}
        <tr>
            <td>{{customer.customer_info.first_name}}</td>
            <td>{{customer.customer_info.last_name}}</td>
            <td>{{customer.customer_info.phone_number }}</td>
            <td>{{customer.customer_info.home_address}}</td>
            <td>{{customer.customer_filter.filter_brand}}</td>
            <td>{{customer.filter_last_due_date}}</td>
            <td>{{test}}</td>
            <td><form action="" method="post">
                {% csrf_token %}
               <input type="text" name="custome_id" type="hidden" value="{{ customer.pk }}"/>
                <input type="submit" value="Done">
            </form></td>                
        </tr>
        {% endfor %}
  • Related