How to create a model in your Jiango Project

by SkillAiNest

If you are making something with Jiango, there is one thing you can’t leave: model. The model is the heart of any Jiango app. They explain how your data is made, how it is secure in the database, and how Jiango can communicate with it.

Now, if you are new to Jiango or are still wrapping your head around the basics, don’t worry. I’ve been there too. Models may seem a bit scared first, but once you see how they work, they are completely straight.

I will walk you through all this – step -by -step – so by the end of this post, you will not only know how to make the model, but also how to use them in real plans.

Let’s enter it.

Here’s what we will cover is:

  1. What is the model in Jiango?

  2. How to create a model in Jiango

  3. Extra models feature that you will use

  4. The use of models in Jiango Admin

  5. Normal questionnaire

  6. The final views

What is the model in Jiango?

A model in Jiango is just a Uzar class that tells Jingo how you want to see your data. Jiango takes care of the difficult part (talking to the database), so you can focus on describing your data in the simplest code.

Here is a sharp example of a basic model:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)
    published_date = models.DateField()
    price = models.DecimalField(max_digits=5, decimal_places=2)

Let me break it:

  • title And author There are just short pieces of text, so I’m using CharField.

  • published_date Have a date – quite easy, that’s DateField Is for.

  • price There is a large number with the decimal, so DecimalField Works

Each line describes a piece of data that I want to store for each book. Easy, okay?

How to create a model in Jiango

Step 1: Start a Jiango Project (if you don’t have before)

If you are absolutely new, first you need the Jiango Project:

django-admin startproject mysite
cd mysite
python manage.py startapp books

Now you have a Jiango app that has been called books Where you can keep your model.

Step 2: Explain your model

Inside your app folder (books), Open models.py. At the same place you will explain your model.

Here’s a little more real world example:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)
    birthdate = models.DateField()

    def __str__(self):
        return self.name


class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    summary = models.TextField()
    isbn = models.CharField(max_length=13, unique=True)
    published = models.DateField()
    price = models.DecimalField(max_digits=6, decimal_places=2)

    def __str__(self):
        return self.title

What is happening here:

  • I have made two models: Author And Book.

  • Book Have a relationship with Author Using ForeignKey. This means that a writer can have many books.

  • I am using __str__() When I see the items in Jiango Admin to return a good name.

Step 3: Register the app and create a database

Before using your model Jiango, make sure your app has been added to the project settings.

Open mysite/settings.py And find INSTALLED_APPS Add the list 'books', For this:

INSTALLED_APPS = (
    
    'books',
)

Now, run the migration to create a database table for your models:

python manage.py makemigrations
python manage.py migrate

In this way, Jiango converts your code into the original database table. The first command makes the migration file (mainly, instructions for the database), and the second applies to it.

Step 4: Make and use items

Now you can use these models in your code. Open Jiango Shell:

python manage.py shell

Then try it out:

from books.models import Author, Book
from datetime import date


jane = Author.objects.create(name="Jane Austen", birthdate=date(1775, 12, 16))


book = Book.objects.create(
    title="Pride and Prejudice",
    author=jane,
    summary="A novel about manners and marriage in early 19th-century England.",
    isbn="1234567890123",
    published=date(1813, 1, 28),
    price=9.99
)

print(book)

Jiango will automatically save them in your database.

1. The default values

You can give a field a default value:

is_published = models.BooleanField(default=False)

2. Auto -time stamps

These are extremely useful times to create or track the latest times:

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

3. Model Meta options

You can add class meta to customizing things like default order:

class Book(models.Model):
    

    class Meta:
        ordering = ('published')

The use of models in Jiango Admin

Jiango’s built -in admin panel is one of the best parts of the framework. But your models will not appear there unless you register them.

I books/admin.pyAdd:

from django.contrib import admin
from .models import Author, Book

admin.site.register(Author)
admin.site.register(Book)

Run now:

python manage.py createsuperuser

Go http://127.0.1:8000/adminLogin, and boom – your models are present with a full interface.

Normal questionnaire

Can I change it after creating a model?

Yes, but you will need to have a new migration:

python manage.py makemigrations
python manage.py migrate

Which databases work with Jiango?

Jiango works with Postgrass QL, SQL, Scalite (default), and more. Most people start with Sqlite when learning because it is easy and works outside the box.

What’s the difference between four fields and text fields?

Use CharField For short text with maximum length (such as name or title). Use TextField For long text (such as a blog post or summary).

The final views

Once you understand the models, the rest of the Jiango start clicking the place. Everything – shapes, views, templates – finally re -connect the model. This is how your app stores and works with real data.

The best way to learn is to make something. Start, perhaps a book catalog, task manager, or personal blog. Add the model at a time and play with them in the admin.

More resources

You may also like

Leave a Comment

At Skillainest, we believe the future belongs to those who embrace AI, upgrade their skills, and stay ahead of the curve.

Get latest news

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

@2025 Skillainest.Designed and Developed by Pro