Fixtures

Our database is empty, but right now we don't have any views to add objects to our database. We could do what we did in the last chapter and create a management command, but there's an easier way. I have added three Car objects to the database and then created a dump of this data that you can load. A data dump like this is called a fixture. We'll discuss fixtures in a bit; for now, let's see how to use them to load data in our database.

On your command line, run this command in the project root with the virtual environment activated:

> python manage.py loaddata frontend/fixtures/initial.json
Installed 3 object(s) from 1 fixture(s)

Refresh the web page and now you should see a web page similar to this:

Fixtures

Now we have three cars in our database. You should play around the app for a while. It has a details page for each car and allows you to submit a booking request from the details page.

Note

If you try to use the booking form, note that the start and end dates need to be in the YYYY-MM-DD format. For example, 2016-12-22 is a valid date format accepted by the form.

To learn more about fixtures, take a look at the Django documentation at https://docs.djangoproject.com/en/stable/howto/initial-data/. Fixtures are a feature of Django that let you dump the data in your database in simple text files using a number of formats. The most commonly used format is JSON. Once you have a fixture file, you can then use it to populate your database with data, as we did here, using the loaddata command.

Before we move on to admin customization, I want to talk about a couple of new things that I have used in the models for this app. You should take a look at frontend/models.py to see how our models are configured, and then read the following information, which explains the new concepts.

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

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