我正从我的第二个模型LeaseExtra计算我的Extra模型中的属性。
但它对我没用。
为什么我收到此错误?:
LeaseExtra匹配查询不存在。
class Extra(CommonInfo):
version = IntegerVersionField( )
number = models.CharField(max_length=30,null=True, blank=True)
max_occupants = models.PositiveSmallIntegerField()
_lease = None
def _get_total(self):
from conditions.models import LeaseExtra
le_dict = LeaseExtra.objects.get(extra_id=self.id, is_active = True )
if le_dict:
lease = le_dict.lease
else:
lease = 0
self._lease = lease
class LeaseExtra(CommonInfo):
version = IntegerVersionField( )
extra = models.ForeignKey(Extra,on_delete=models.PROTECT)
lease = models.ForeignKey(Lease,on_delete=models.PROTECT)
is_included = models.BooleanField(default=True)
回溯:
文件 " C:\用户\ PAPA \ DEV \ rent_unit \ rent_unit_venv \ lib中\站点包\ django的\芯\处理程序\ base.py" 在get_response中 132. response = wrapped_callback(request,* callback_args,** callback_kwargs) 文件" C:\ Users \ PAPA \ DEV \ rent_unit \ rent_unit_venv \ lib \ site-packages \ django \ contrib \ auth \ decorators.py" 在_wrapped_view中 22. return view_func(request,* args,** kwargs) 文件" C:\ Users \ PAPA \ DEV \ rent_unit \ src \ unit \ views.py"在extra_available_list中 163. extra_list = [obj for obj in extra_list if((obj.lease)== 0)] 文件" C:\ Users \ PAPA \ DEV \ rent_unit \ src \ unit \ models.py"在租赁 166. self._get_total() 文件" C:\ Users \ PAPA \ DEV \ rent_unit \ src \ unit \ models.py"在_get_total中 151. le_dict = LeaseExtra.objects.get(extra_id = self.id,is_active = True) 文件" C:\ Users \ PAPA \ DEV \ rent_unit \ rent_unit_venv \ lib \ site-packages \ django \ db \ models \ manager.py" 在manager_method中 127. return getattr(self.get_queryset(),name)(* args,** kwargs) 文件" C:\ Users \ PAPA \ DEV \ rent_unit \ rent_unit_venv \ lib \ site-packages \ django \ db \ models \ query.py" 在得到 334. self.model._meta.object_name
Exception Type: DoesNotExist at /unit/list/extra/available/ Exception Value: LeaseExtra matching query does not exist.
更新:
视图
@login_required
def extra_available_list(request):
EXTEND = "rent_base.html"
extra_list = ExtraFilter(request.GET, queryset=Extra.objects.all().order_by('-created'))
extra_list = [obj for obj in extra_list if ((obj.lease)==0)]
paginator = Paginator(unit_list, 10) # Show 25 contacts per page
page = request.GET.get('page')
try:
extra_page = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
extra_page = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
extra_page = paginator.page(paginator.num_pages)
#return render(request, 'customer/company_list.html', {'company': company})
return render(request,'unit/extra_list.html', {'extra': extra_list,'page_extra':extra_page})
URL
url(r'^list/extra/available/$', views.extra_available_list, name="extra_available_list"),