Django - Making a Reservation system

Viewed 573

As a part of a task, I created an "apartment listings" website. I managed to get that done, but now I need to make a "reservation system" for it.

The basic idea is that a logged in user can select an apartment from the listed apartments, pick a "start_date" and an "end_date" (if the apartment already isn't booked ) and book the apartment.

Im a total Django newbie, and need some pointers in order to start somewhere with this task.

I have an Apartment model which contains all of the Apartments info that I use to print everything out with the template.

I'm using the provided django user models to register / log-in.

What kind of model do I need for the reservation, and how could I connect everything ? I tried making a reservation model, but I got no idea where to go next.

I'm not asking for you to solve this for me, I'm asking for someone to explain (if possibile in detal) to me how could I go on about achieving this on my own.

this is my apartment model:

class Apartment(models.Model):
    title = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    zipcode = models.CharField(max_length=20)
    description = models.TextField(blank=True)
    price = models.IntegerField()
    bedrooms = models.IntegerField()
    bathrooms = models.DecimalField(max_digits=2, decimal_places=1)
    garage = models.IntegerField(default=0)
    size = models.IntegerField()
    photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')
    photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    in_rent = models.BooleanField(default=False)
    list_date = models.DateTimeField(default=datetime.now, blank=True)

    def __str__(self):
        return self.title

Thank you so much !

1 Answers

Your assumption for making a Reservation object would be correct. You'll want to link those reservations to the Apartment in question. Django supports these kind of relationships through their ForeignKey and ManyToManyField.

These fields support linking of one-to-one, one-to-many, and many-to-many relationships.

In your case, one Apartment can have many Reservations, which means you need to have a field in your Reservation pointing to a single Apartment, which can be done using a ForeignKey

Your model should look something like this:

Reservation(models.Model)

    apartment = models.ForeignKey(to=Apartment, related_name='reservations', 
        on_delete=models.Cascade)

    start_date = models.DateField()
    end_date = models.DateField()

To retrieve all reservations for an appartment, you can simply use apartment.reservations since we've defined a related_name in the ForeignKey field.

You can even filter for specific dates by doing Reservations.objects.filter(apartment=apartment, start_date__gte=<your_start_date>, end_date__gte<your_end_date>)

Related