models.py

Consider if models.py file is as follows

one to many field example

from django.db import models
from django.contrib.auth.models import User

# ONE Manufacturer MANY Cars

class Manufacturer(models.Model):
    name = models.CharField(max_length=200)
    address = models.TextField()
    # add specific plural name to be shown in admin panel for the model
    class Meta:
        verbose_name = 'Manufac'
        verbose_name_plural = 'Manufacturers'
        ordering = ["-name"] # default ordering for Model.objects.all()

class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer', related_name='car')

    name = models.CharField(max_length=50)
    model_name = models.CharField(max_length=75)

# TODOs include extending default user model example (one-to-one mapping)

# TODOs include one to many field example

# TODOs include many to many field example

# TODOs include foreign_key repated_name and help_text example

# TODOs list all commonly used field types

Ref link: Field Types

# TODOs separate created and modified field template for quick access

# TODOs unique, null, blank ,notnull, max_length, default constraints

Makemigrations, Migrate

Once all model changes are done run the following commands to reflect those changes in DB

# cd into directory containing manage.py file

(my_env)$ python manage.py makemigrations app_1 app_2
(my_env)$ python manage.py migrate

Django shell

You can also assess application database through shell.

(my_env)$ python manage.py shell

if ipython is installed in virtualenv with (my_env)$ pip install ipython, above command will open ipython instead of default python shell


results matching ""

    No results matching ""