> For the complete documentation index, see [llms.txt](https://sathyakumars-kb.gitbook.io/user-authentication-based-django-app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sathyakumars-kb.gitbook.io/user-authentication-based-django-app/model-and-django-admin-area.md).

# Model & Django Admin Area

In Django, a model is a Python class that represents a database table. It defines the fields and behavior of the data that will be stored in the corresponding database table. Models in Django follow the Object-Relational Mapping (ORM) paradigm, which means that you can interact with the database using high-level Python objects instead of directly writing SQL queries.

Here are the key aspects of the Django admin area:

## **1. Define Models:**

In your app directory (todo`/models.py`), define the data models for todo app. This is where you specify the structure of your database tables.&#x20;

Here is a TodoList Django model:

```python
from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Task(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    title = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    complete = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        order_with_respect_to = 'user'
```

Your `Task` model in Django represents a table in the database with the following fields:

* `user`: A foreign key relationship to the `User` model from `django.contrib.auth.models`. It establishes a many-to-one relationship between tasks and users, meaning each task is associated with a single user, and each user can have multiple tasks. The `on_delete=models.CASCADE` argument means that if a user is deleted, all tasks associated with that user will also be deleted.
* `title`: A character field for the title of the task.
* `description`: A text field for a more detailed description of the task. It allows null values, which means a task can exist without a description.
* `complete`: A boolean field indicating whether the task is complete or not. It has a default value of `False`.
* `created`: A DateTimeField that automatically sets the current date and time when a task is created.
* `__str__`: A method that defines a human-readable string representation of each instance of the `Task` model. In this case, it returns the title of the task.
* `Meta`: A class within the model that can be used to configure various options. In this case, `order_with_respect_to` is set to `'user'`, indicating that the tasks should be ordered with respect to the associated user.

This model can be used to store information about tasks, including who the task belongs to, its title, description, completion status, and creation timestamp. The `User` foreign key establishes a connection between tasks and users, allowing you to associate tasks with specific users in your application.

## **2. Model Registration:**

By default, all models are available in the admin interface, but you can customize how they are displayed by creating an `admin.py` file in your app and registering your models there.

```python
# yourapp/admin.py
from django.contrib import admin
from .models import Task

admin.site.register(Task)
```

## &#x20;3. Migrate the model (Task):

Django uses migrations to manage database schema changes. Run the following commands to apply your models to the database: (refer fig. 2.3 & fig. 2.4)

```
python manage.py makemigrations
python manage.py migrate
```

<figure><img src="/files/XtreK6OvNq3ruQDJjAqv" alt=""><figcaption><p>Fig. 2.3</p></figcaption></figure>

<figure><img src="/files/xodYBASYQd1i9KZseo1T" alt=""><figcaption><p>Fig. 2.4</p></figcaption></figure>

In summary, this output represents the successful application of database migrations for the specified Django applications, ensuring that the database schema is up to date with the latest changes defined in the migrations.

## **4. Django Admin Area Installation:**

Django's admin area is a powerful and customizable feature that comes built-in with the Django web framework. It provides a user-friendly interface for managing and interacting with your application's data. The admin area is automatically generated based on your models, allowing you to perform CRUD (Create, Read, Update, Delete) operations on your database records without having to write custom views and templates.

To use the admin interface, you need to make sure it's enabled in your Django project. In your `urls.py` file, you should have something like:

```python
from django.contrib import admin
urlpatterns = [
    # ... other URL patterns ...
    path('admin/', admin.site.urls),
]
```

This sets up the admin URLs, and you can access the admin interface at `http://yourdomain.com/admin/`.

## **5. Creating Admin Users:**

Admin users are different from regular users. You need to create an admin user to access the admin interface. You can do this using the `createsuperuser` management command: (refer fig. 2.5)

```
python manage.py createsuperuser
```

<figure><img src="/files/ohoCOuLod7aEo8u0dXMQ" alt=""><figcaption><p>Fig. 2.5</p></figcaption></figure>

## &#x20;6. Django Admin:

This sets up the admin URLs, and you can access the admin interface at `http://127.0.0.1:8000/admin/`. Type the below request in web browser (refer fig. 2.6 and fig. 2.7)

```
http://127.0.0.1:8000/admin/
```

<figure><img src="/files/YQcAnXcTA5ivyzoBUjwq" alt=""><figcaption><p>Fig 2.6 Django Admin Login</p></figcaption></figure>

<figure><img src="/files/HqFHQtRRB8bt0Qziv2IH" alt=""><figcaption><p>Fig. 2.7 Django Panel</p></figcaption></figure>

## &#x20;7. Adding some tasks to the defined model

<figure><img src="/files/8NAIO6ixjfeh8BC8xIf8" alt=""><figcaption><p>Fig. 2.8 Model Task</p></figcaption></figure>

<figure><img src="/files/3jek0shYWt0eLejQ0dcV" alt=""><figcaption><p>Fig. 2.9 Tasks Adding</p></figcaption></figure>

<figure><img src="/files/eXKcJPs2gb5fY4mvRkIf" alt=""><figcaption><p>Fig. 2.10 Added Tasks</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sathyakumars-kb.gitbook.io/user-authentication-based-django-app/model-and-django-admin-area.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
