ob-vaults/Super_Vault/200_programming/python/django/models.md
2024-09-12 17:54:01 +03:30

3.6 KiB
Raw Blame History

status: #doc Tags: #python/django links: Date: 2024-07-11


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

structure

  • each model is class in models.py in our app folder ex:
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

class name is name of the table and each field is name of column in db for more

fields

we have different fields for different data types most useful are :

  • CharField : for text with fixed max length

  • TextField : for text with flexible max length

  • BooleanField : for true or false value

  • IntegerField : for An integer. Values from -2147483648 to 2147483647

  • FloatField : A floating-point number represented in Python by a float instance. for more Field

  • for optional fields that can be blank use blank=true

  • for default value to be inserted inside a field use default="my_value"

class Person(models.Model):
    name = models.CharField(max_length=30,default="not_found")
    address = models.textfield(blank=true)
    is_alive = models.booleanfield(default=true)

migration

as we do our changes to django project we need to apply these changes to our database we use 2 command for this

  • makemigration make migration plan no change in db
python manage.py makemigration
  • migrate make change to db based on plan in last command
python manage.py migrate

interaction with model

for interaction directly use python shell, use this command in project folder with env active:

python manage.py shell

import

before any interaction first you must import your model, use this in your views or first in py shell

# my_app is name of app that you chose in begining
# person is name of model you want to import
from my_app.models import person

crud operation

create

with model in structure section in mind

  • with crate method
person.objects.create(first_name="max",last_name="ford")
  • with save method
from my_app.models import person

# Create an instance of the model
someone = person(first_name="max",last_name="ford")
# Save the object to the database
someone.save()

read

  • you can save queryset in variable
people = person.objects.all()
print(people)
  • all
person.objects.all()
  • filter
person.objects.filter(first_name="mohammad")
  • Single Record
person.objects.get(first_name="mohammad",last_name="karimi")

Note

get() raises a 'DoesNotExist' exception if no matching record is found and a 'MultipleObjectsReturned' exception if more than one record is found.

update

  • Update with save method (variable)
someone = person.objects.get(id=1)
someone.first_name = "ali"
someone.save()

Update Using `update()

# we can also use all() and get()
person.objects.filter(first_name="aliii").update(first_name="ali")
  • Note: The update() method updates all matching records in a single query.

delete

Delete a Single Object with variable

someone = person.objects.get(id=1)
someone.delete()

Delete Multiple Objects Using filter()

person.objects.filter(first_name="aliii").delete()
  • Note: The delete() method deletes all matching records.

References