Home > Software engineering >  How do I make a Django form that display's model objects to chose from
How do I make a Django form that display's model objects to chose from

Time:08-01

Ok, I am building a hotel application and in the booking section of the various hotels, I want there to be a dropdown in the booking section and that dropdown would contain the number of rooms the hotel has so a user can book a specific room. Now each hotel has different number of rooms, so I want it to be that, based on the number of rooms the hotel has, that number would be the number of rooms in the drop down. here is my code...

registration 'models.py'

class Hotel(models.Model):
    name = models.CharField(max_length=120, null=True)
    hotel_location = models.CharField(max_length=3000, null=True)
    number_of_rooms = models.IntegerField(null=True)
    number_of_booked_rooms = models.IntegerField(null=True, default=0)

and here is the booking 'models.py'

class RoomBooking(TrackingModel):
    hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE)
    room_number = models.ForeignKey('Room', on_delete=models.CASCADE)
 
class Room(TrackingModel):
    hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE)
    room_information = models.ForeignKey(RoomBooking, on_delete=models.CASCADE)
    room_number = models.IntegerField()

here is my 'views.py'

def book_a_room(request, *args, **kwargs): hotel_slug = kwargs['slug'] hotel = Hotel.objects.get(slug=hotel_slug)

form = BookingARoomForm()

if request.method == 'POST':
    form = BookingARoomForm(request.POST)
    if form.is_valid():

        if hotel.number_of_booked_rooms < hotel.number_of_rooms:
            hotel.no_rooms_available = False
            if hotel.no_rooms_available == False:
                hotel.number_of_booked_rooms  = 1
                hotel.save()
                room_booking = RoomBooking.objects.create(hotel=hotel, guest=request.user, **form.cleaned_data)
                Room.objects.create(hotel=hotel, room_information=room_booking, is_booked=True, checked_in=True, **form.cleaned_data)

            return HttpResponse('<h1>You just booked a hotel</h1>')

        else:
            hotel.no_rooms_available = True
            hotel.number_of_booked_rooms = hotel.number_of_rooms
            hotel.save()
            if hotel.no_rooms_available == True:
                return HttpResponse('<h1>This hotel is maximally booked...</h1>')

and also the 'forms.py'

class BookingARoomForm(ModelForm):
    date_to_check_in = forms.DateField(widget=forms.TextInput(attrs={'type': 'date'}), required=True)
    date_to_check_out = forms.DateField(widget=forms.TextInput(attrs={'type': 'date'}), required=True)
    room_number = forms.ModelChoiceField(queryset=Room.objects.all(), empty_label="(Nothing)")

    class Meta:
        model = RoomBooking
        fields = ['date_to_check_in', 'date_to_check_out', 'room_number']

what I want to do is to get the form room_number to be a drop down with the number of rooms a particular hotel has and I want it to be dynamic so that if a hotel has 7 hotel rooms, there would be 7 rooms in the drop down and if another has 10, there would be 10 rooms on the dropdown. Can you please suggest how I can do that in the views and transfer the filtered object to the forms or can you suggest a more logical way to do it more than this.

CodePudding user response:

I think you're looking for ModelChoiceField.
Especially note the queryset argument. In the view that is backing the page, you can change the QuerySet you provide based on whatever criteria you care about.

CodePudding user response:

The answer you're looking for isnt easy to describe here so better if you go through this article You need to use jquery/AJAX for dynamic dropdown values.

https://tutorial101.blogspot.com/2020/06/django-chained-dropdown-list-select-box.html

  • Related