In this third part we are going to create the model for out todo item. The model is going to be created into the file named
models.py from within the folder
todo. So, in the
models.py file paste the following code:
from django.db import models class TodoItem(models.Model): name = models.CharField(max_length = 30, blank = False) done = models.BooleanField(blank = False, default = False) def __str__(self): return self.name
We declared a class named
TodoItem. This class inherits from the Django built-in
models.Model. This class has two properties:
name– which is the todo item itself – It is a character field, with a maximum length of 30 characters, and it can’t be left empty.
done– which is just a Boolean field
The last two rows are responsible for displaying the name in a readable format in the admin panel.
That’s it for our class!
Now, we would want to register the
TodoItem with our
admin.py file, so we will be able to add new items through the admin panel. For that open the
admin.py file from
todo folder and make sure it has the following code in it:
from django.contrib import admin from .models import TodoItem admin.site.register(TodoItem)
First of all we had to import the
TodoItem from the models.py file then we register it with the site’s admin panel. Now, we will be able to add a new todo item via admin panel.
There is one problem, we don’t have a user name and a password to connect to the admin panel. Moreover, we created a model and we didn’t do migrations. We will do that in part 4.