Django выдает мне эту странную AttributeeError:
Объект 'Tour' не имеет атрибута 'check_if_requested_beds_are_avaliable'
Однако у меня есть метод check_if_requested_beds_are_avaliable () в моем классе Tour моего модель. Другой метод - set_new_number_of_beds () работает нормально. Я никогда не сталкивался с этим с Django, может ли кто-нибудь сказать мне, какое решение?
Это модель:
from django.db import models
from django.urls import reverse
class Tour(models.Model):
destination = models.CharField(max_length=256)
country = models.CharField(max_length=256)
date_of_arrival = models.DateField()
total_days = models.IntegerField()
price = models.DecimalField(decimal_places=2,max_digits=6)
available_beds = models.IntegerField()
photo = models.ImageField(upload_to='photos',default='photos/def.png')
description = models.TextField(blank=True)
def set_new_number_of_beds(self,number_of_guests):
self.available_beds -= number_of_guests
return self.available_beds
def check_if_requested_beds_are_available(self,numb_of_guests):
if self.available_beds < numb_of_guests:
return False
else:
return True
def __str__(self):
return f'{self.destination} , {self.country} , {self.total_days} days for $ {self.price}'
class Client(models.Model):
name = models.CharField(max_length=256)
surname = models.CharField(max_length=256)
email = models.CharField(max_length=256)
number_of_guests =models.IntegerField()
tour = models.ForeignKey(Tour,on_delete=models.CASCADE)
total_price = models.DecimalField(decimal_places=2,max_digits=6,default=0)
def get_total_price(self):
return self.number_of_guests * self.tour.price
def get_absolute_url(self):
return reverse('tour_list')
def __str__(self):
return f'{self.name} {self.surname}, {self.tour}'
и это вид:
from django.shortcuts import render
from aplikacija.models import Tour,Client,ContactMessage
from django.views.generic import ListView,DetailView,TemplateView,CreateView
from aplikacija.forms import ClientForm,ContactForm
def HomeView(request):
if request.method=="GET":
return render(request,'aplikacija/home.html')
else:
form= ClientForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
surname=form.cleaned_data['surname']
email=form.cleaned_data['email']
tour_id=form.cleaned_data['tour'].pk
number_of_guests=form.cleaned_data['number_of_guests']
total_price=number_of_guests*form.cleaned_data['tour'].price
tour_name=form.cleaned_data['tour'].destination
client = Client.objects.create(name=name,surname=surname,email=email,number_of_guests=number_of_guests,tour_id=tour_id,total_price=total_price)
client.save()
tour=Tour.objects.get(destination=tour_name)
beds_available = tour.check_if_requested_beds_are_avaliable(number_of_guests)
if beds_available:
tour.set_new_number_of_beds(number_of_guests)
tour.save()
message =f'thank you {name} ! Your reservation to {tour} for {number_of_guests} guests , with a total price of {total_price} has been booked successfully.'
return render(request,'aplikacija/home.html',{'msg':message})
else:
form = ClientForm()
return render(request,'aplikacija/client_form.html',{'infoMsg':f"sorry, there are less than {number_of_guests} beds available for {tour_name}",'form':form})
def thanksView(request):
form=ContactForm(request.POST)
if form.is_valid():
sender=form.cleaned_data['sender']
title=form.cleaned_data['title']
body=form.cleaned_data['body']
message=ContactMessage.objects.create(sender=sender,title=title,body=body)
message.save()
return render(request,'aplikacija/thanks.html')
class TourListView(ListView):
model = Tour
def get_queryset(self):
return Tour.objects.all()
class TourDetailView(DetailView):
model = Tour
class CreateClientView(CreateView):
redirect_field_name = "/aplikacija/home.html"
model = Client
form_class = ClientForm
class contactView(CreateView):
redirect_field_name="/aplikacija/thanks.html"
model = ContactMessage
form_class=ContactForm
Итак, довольно ясно, что у меня есть метод, который выдает AttributeError. И, как я уже сказал, другой метод в классе работает нормально. Что мне не хватает?