匹配查询不存在 django V3.2.9
Posted
技术标签:
【中文标题】匹配查询不存在 django V3.2.9【英文标题】:matching query does not exist django V3.2.9 【发布时间】:2022-01-05 14:43:09 【问题描述】:我正在尝试从 POST 请求中检索 ID,当我确定该客户的特定 ID 存在时,该请求会引发匹配查询不存在,因为它的所有信息也使用 customer.id 显示在模板表中 在模板中给出一个(ID)以确保它确实存在
我的意见.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
)
我的模型.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 "
我的 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 %
追溯
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.
【问题讨论】:
请分享完整的错误回溯。 【参考方案1】:由于这两行代码,您遇到了错误
filtermaintenance_id = FilterMaintenance.objects.get(pk=request.POST.get('customer.id', False))
还有这个
<input type="submit" name="customer.id" value="Done">
试试这个
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
)
改变这个
% 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 %
到
% 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 %
【讨论】:
成功了!非常感谢以上是关于匹配查询不存在 django V3.2.9的主要内容,如果未能解决你的问题,请参考以下文章