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

155 lines
3.6 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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:
```python
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](https://docs.djangoproject.com/en/5.0/topics/db/models/)
## 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](https://docs.djangoproject.com/en/5.0/ref/models/fields/#field-types)
- for optional fields that can be blank use blank=true
- for default value to be inserted inside a field use default="my_value"
```python
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
```sh
python manage.py makemigration
```
- migrate
make change to db based on plan in last command
```sh
python manage.py migrate
```
# interaction with model
for interaction directly use python shell, use this command in project folder with env active:
```sh
python manage.py shell
```
## import
before any interaction first you must import your model, use this in your views or first in py shell
```python
# 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
```python
person.objects.create(first_name="max",last_name="ford")
```
- with save method
``` python
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
```python
people = person.objects.all()
print(people)
```
- all
```python
person.objects.all()
```
- filter
```python
person.objects.filter(first_name="mohammad")
```
- Single Record
```python
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)
```python
someone = person.objects.get(id=1)
someone.first_name = "ali"
someone.save()
```
Update Using `update()
```python
# 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
```python
someone = person.objects.get(id=1)
someone.delete()
```
Delete Multiple Objects Using `filter()`
```python
person.objects.filter(first_name="aliii").delete()
```
- **Note:** The `delete()` method deletes all matching records.
---
# References