Booking management

Let's leave the car admin section as it is for now and move on to the admin for Booking model. Every time a visitor to the site submits the Book Now form accessible from the car details page, we create a new Booking model record. We need a way to allow the client to look at these booking enquiries, filter them based on some criteria, and accept or reject them. Let's look at how to do this. For a start, let's make sure that our The Booking model shows up as an item in our admin panel. To do so, add these two lines to your frontend/admin.py file:

from frontend.models import Booking
admin.site.register(Booking)

If you take a look at the admin panel now at the URL http://127.0.0.1:8000/admin/, you should see that the Booking model has been added as a link. Open up the link and you should see a list page similar to the one for the Car model that we saw before. If you had submitted any booking requests, they should show up in the list. It's not pretty, but at least it works. Let's make it better. For one, we need to give the administrators more information about each booking enquiry. It will be good if we can show the customer's name, booking start and end dates, and whether the booking has already been approved.

While we could use the __str__ method again to create a string with all this information, so much information in one column isn't a pretty sight. Additionally, we would be missing out on the sorting capabilities that the Django admin provides us with for each model list page.

Let's look at how we can display multiple fields from our model in the list view. Along the way, you will also learn a bit more about how the admin works internally.

A peek behind the curtain

If you take a minute to think about what we have been able to achieve with just a couple of lines of code, you would probably be amazed at the power of the Django admin. How is this power achieved? Well, the answer to that is very complicated. Even I don't yet understand fully how the admin app works. It is a very complex piece of programming.

Note

Even though the admin app is quite complex, it is still Python code. If you're feeling adventurous or just generally bored one day, try to look at the source code for the admin app. It's in the VIRTUAL_ENV/lib/python3.5/site-packages/django/contrib/admin folder. Replace VIRTUAL_ENV with the folder that holds the virtual environments you create for your projects.

One of the main components of the admin system is the ModelAdmin class. Just as the models.Model class allows us to define complicated database models using a very simple class definition, the ModelAdmin class allows us to customize the admin interface for a model in great detail. Let's see how we can use it to add extra fields to our list of booking enquiries. Change the frontend/admin.py file to match the following contents:

from django.contrib import admin

from frontend.models import Car
from frontend.models import Booking


class BookingModelAdmin(admin.ModelAdmin):
    list_display = ['customer_name', 'booking_start_date', 'booking_end_date', 'is_approved']


admin.site.register(Car)
admin.site.register(Booking, BookingModelAdmin)

Now if you open up the admin list page for the Booking model, you should see something similar to this with all the important fields displayed:

A peek behind the curtain

This presents the user with a very nice tabular view. The client now sees all of the relevant details and has the ability to sort the table according to their needs. Django is also helpful enough to show the date values in a nice format. Let's look at what we have done here.

We first created a ModelAdmin subclass called BookingModelAdmin. We then configured the fields that we would like to show in the list page using the list_display attribute. Finally, we need to associate our ModelAdmin class with the Booking model class so that the admin can customize itself according to our requirements. We do that using the following:

admin.site.register(Booking, BookingModelAdmin)

If you look at how we register the Car model, it looks similar to the Booking model:

admin.site.register(Car)

That's because it is the same thing. If you don't provide a custom ModelAdmin subclass, Django uses the default options, which is what we see with the Car model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.74.25