Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Last active May 30, 2017 07:30
Show Gist options
  • Save prodeveloper/35954273ac140e47b42124485f09d1bc to your computer and use it in GitHub Desktop.
Save prodeveloper/35954273ac140e47b42124485f09d1bc to your computer and use it in GitHub Desktop.
Expounding on Models

In the previous class we had a cursory introduction to models. We were able to create a Student model and register it.

There is a lot that we skipped in that process. In this class, we shall be looking at how we can better use models in our admin panel.

##Step 1

You may have noticed from last time that our model had a rather weird name on the django admin panel.

In customizing the model admin we will use the class ModelAdmin.

We then set the class variable list_display to show fields we are interested in rather than just Student object

We may for example want to see the student name. Then our code would look as follows.

We can now go ahead and register the model admin so that the changes are reflected.

Now refresh the main page. You should be able to see the student class showing up as expected.

##Step 2

In the last class, we looked at model fields without much explanation as to what they are about.

This were defined in the models.

The most common fields that you will use include:

  • IntegerField: Usually for storing integers, numbers such as - 1, 0, 1, 2
  • DecimalField: Where the numbers you work with are not whole: 0.5, 1.5, 3.147
  • CharField: Where you need restricted number of characters, the name for example is a good candidate for this.
  • TextField: Where you want text but don’t want to be restricted on the length, e.g. post body for a blog post
  • EmailField: Where the entry you are taking in should follow the format of an email e.g “botul@akirachix.com
  • FileField: Stores file/binary data. This maybe documents manuals etc
  • ImageField: Stores images
  • Django provides a comprehensive listing on their website https://docs.djangoproject.com/en/1.11/ref/models/fields/

##Step 3

Let us now try to make our model more fully featured with the following attributes:

  • names: As a restricted text field of 100 characters
  • course: As a restricted text of 50 characters
  • description: As an unbounded text
  • registration_date: As a date
  • graduation_date: As a date

Please NOTE that we used a default value. This ensures that the fields will remain compatible with existing data. This is a good practice when modifying an existing database structure and in general where a reasonable default can be established.

Whenever an update is made. We must then run our migration commands as we learnt from our last class.

First

python manage.py makemigrations 

The we run the actual migration

python manage.py migrate

##Assignment

For your assignment create a new model for Teachers it should have the following attributes:

  1. Names
  2. Course
  3. Description
  4. Availability

This model should be registered on the Admin panel

##References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment