Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Django. Filtering room search queries by room_type

pc510

Coder
I am trying to make a basic hotel reservation system with Django. I want the users to be able to input dates on an index page and see a list of available
Code:
room_types
for the selected dates. The page should then direct the users to a reservation page for that room type (make_reservation.html, make_resrevation view), where they can finalize the reservation.

I am able to run the search and filter individual rooms, but I am unable to do it by room type. Here is the code for the view

Code:
def search_results(request):
    check_in = request.POST.get('check_in_date')
    check_out = request.POST.get('check_out_date')

    check_in_date = datetime.strptime(check_in, '%Y-%m-%d').date()
    check_out_date = datetime.strptime(check_out, '%Y-%m-%d').date()

    nights = (check_out_date - check_in_date).days
    room_type = request.POST.get('room_type')  # Assuming you have a form input for selecting room type
    

    available_rooms = Room.objects.filter(
        ~Q(reservation_set__check_in_date__lte=check_out, reservation_set__check_out_date__gte=check_in)
    )             
    context = {
        'check_in': check_in,
        'check_out': check_out,
        'available_rooms': available_rooms,
        'nights': nights, 
    }

    return render(request, 'myapp/search_results.html', context)

Here is the Room and Reservation models


Code:
class Room(models.Model):
    room_number = models.CharField(max_length=10)
    room_type = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    reservations = models.ManyToManyField('Reservation', through='RoomReservation', related_name='room_set')

    def __str__(self):
        return self.room_number

class Reservation(models.Model):
    guest_name = models.CharField(max_length=100)
    check_in_date = models.DateField()
    check_out_date = models.DateField()
    room = models.ForeignKey(Room, on_delete=models.CASCADE, related_name='reservation_set')
    credit_card_number = models.CharField(max_length=16)
    security_code = models.CharField(max_length=3)
    name_on_card = models.CharField(max_length=100)
    expiration_date = models.CharField(max_length=100)
    total_cost = models.DecimalField(max_digits=10, decimal_places=2)  # Add this field


    def __str__(self):
        return self.guest_name
    
class RoomReservation(models.Model):
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE)
    date = models.DateField()


The line in question is line
available_rooms = Room.objects.filter(
~Q(reservation_set__check_in_date__lte=check_out, reservation_set__check_out_date__gte=check_in)
)

Can I set this up to the results by type of rooms rather than individual Room objects? Or what is the best way to achieve this?
 

Buy us a coffee!

Back
Top Bottom