reading-notes

This Repo required for Asac labs class 2


Project maintained by ManarAbdelkarim Hosted on GitHub Pages — Theme by mattgraham

Readings: Django Models

Model

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

The basics of Models

Fields:

The most important part of a model – and the only required part of a model – is the list of database fields it defines. Fields are specified by class attributes.

Feilds type:

model example:

from django.db import models

class MyModelName(models.Model):
    """A typical class defining a model, derived from the Model class."""

    # Fields
    my_field_name = models.CharField(max_length=20, help_text='Enter field documentation')

Django admin site

he Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

Registering models :

on admin.py page :

from .models import your_model

admin.site.register(your_model)

Creating a superuser:

from terminal :

    python3 manage.py createsuperuser

Logging in and using the site

changing the default names of the new data object:

to change the default name use str majic method in the model to return different value