Difference between revisions of "Creating a Model in Django"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Creating a Model)
(Register the Model)
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
  
 
=Creating a Model=
 
=Creating a Model=
With your app folder (the one with 'views.py') create a new '.py' file and add the following:
+
Within your app folder (the one with 'views.py') create a new '.py' file and add the following:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
Line 45: Line 45:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now sung the admin program, you will need to run the manage, and then migrate. You will then need to do the same and this time run makemigrations.
+
Now start the admin program, you will need to run the manage, and then migrate. You will then need to do the same and this time run makemigrations.
  
 
=Checking your model=
 
=Checking your model=
  
 
run the server and visit http:\\127.0.0.1:8000 and you should see your web app. In the address bar add '/admin' and login. You should see you model listed on the dashboard. You should be able to create new entries from this admin dashboard.
 
run the server and visit http:\\127.0.0.1:8000 and you should see your web app. In the address bar add '/admin' and login. You should see you model listed on the dashboard. You should be able to create new entries from this admin dashboard.

Latest revision as of 10:18, 22 October 2020

One advantage of using Django is the ability to create a model, it is essentially a database table but without specifying any of the code to create the underlying structure. Model's can be related just like a relational SQL database, you can set a field as been a one to one field or a foreign key.

Creating a Model

Within your app folder (the one with 'views.py') create a new '.py' file and add the following:

from django.db import models

This will import the built in models code, Then create the class below for the new model:

class Product(models.Model):
    name = models.CharField(max_length=255)
    manufacturer = models.CharField(max_length=255)
    description = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['date_added']
        
    def __unicode__(self):
        return str(self.name)

    def __str__(self):
        return str(self.name)

It is important to notice that the 'class Meta:' and the 'def's are indented as part of the class.

The model above defines a field called 'name' this is a 'CharField' or text box and the maximum length is 255 characters. The model also defines 'description' this is a 'TextField' this is a multi line text area. Also noticed that 'date_added' is a 'DateTimeField' and it will be automatically completed.

The 'class Meta' will allow you to specify settings for the model, one example is the 'ordering' included above.

The def's simply set how each record in the model will be dislayed.

Register the Model

Now in the folder as the 'models.py' find the 'admin.py', if it doesn't exist you will need to create it. Add the following code:

from django.contrib import admin
from .models import Product

admin.site.register(Product)

Now start the admin program, you will need to run the manage, and then migrate. You will then need to do the same and this time run makemigrations.

Checking your model

run the server and visit http:\\127.0.0.1:8000 and you should see your web app. In the address bar add '/admin' and login. You should see you model listed on the dashboard. You should be able to create new entries from this admin dashboard.