Skip to content

Center Model

Now, we are going to write model for center app. First see the class diagram of this project.

Class Diagram

In this diagram, you can see a center class. It has only two attributes i.e. name and address.

Now, open the models.py file of center app.

Create a model named Center.

class Center(models.Model):

Lets add the model field like name and address.

name = models.CharField(max_length=124)

Here, we are using CharField as model field type and max_length is our model field option which is set to 124.

address = models.TextField(max_length=500)

Here, we are using TextField as model field type and max_length is our model field option which is set to 500.

If you are still confused on when to use charfield and when to textfield then please note that CharField has max_length of 255 characters whereas TextField can hold more than 255 characters.

So, if you want to store less number of characters use CharField and if you want to store large number of charcters then use TextField.

Finally, add the str method to represent each Center object in human readable form.

def __str__(self):
    return self.name

Finally, we have wrote model for Center and now its time to run the migration.


Open a new terminal and execute this command

cd mysite
python manage.py makemigrations

This command will create a new migration file in your migration folder.

python manage.py migrate

This command will migrate your changes stored in your migration file.


Now, open admin.py file of center app and register Center model.

from center.models import Center

admin.site.register(Center)

Open the development server and then open Django admin panel.

In the admin panel, you can see Center and perform CRUD operation using it.