Articles → DJANGO → Create Your First Model In Django

Create Your First Model In Django






Steps Of Creating The Model




  1. Create a new module using the following command
  2. <python_path>\python manage.py startapp <module_name>


  3. In the module, add the following code in models.py
  4. from django.db import models
    
    # Create your models here.
    class MyNewScreen(models.Model):
        name = models.CharField(max_length = 120)
        def __str__(self):
            return self.name
    




  5. In the root folder, add the module in settings.py
  6. Picture showing adding the module in the settings.py file
    Click to Enlarge

  7. Inside the module, add following code in admin.py
  8. from django.contrib import admin
    
    # Register your models here.
    from .models import MyNewScreen
    
    admin.site.register(MyNewScreen)
    


  9. In the command prompt, execute the following command
  10. <python path>\python manage.py makemigrations
    <python path>\python manage.py migrate
    

Output




Picture showing the administrators screens in Django
Click to Enlarge



Picture showing the database tables created for Django project
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, December 30, 2019
 
Updated On  -  Thursday, January 16, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250