Django DB Model

Jeanoza·2021년 5월 24일
0

django

목록 보기
1/3

Django shell

(mysite) python manage.py shell

Create Model

# importer Question Model(class)
>>> from pybo.models import Question, Answer

# Créer Model instance
>>> from django.utils import timezone
>>> q = Question(subject='Qu\'est-ce que Pybo?', content='Je voudrais savoir Pybo', create_date=timezone.now())

# Sauvegarder
>>> q.save()

Django Queries Méthode

http://docs.djangoproject.com/en/3.0/topics/db/queries

migration & makemigrations

  • migration : créer un table

  • makemigrations : détecter s'il y a un changement d'un model - création d'une table ou modification d'une property de ce model.
    => si oui, il crée une fiche dans la répertoire migrations, qui va appeler query via la méthode de Django.

    Donc, makemigrations => migration après avoir changé d'un model.

# migration
python manage.py migration

# makemigrations
python manage.py makemigrations

.filter() & .get()

djangoShell

from pybo.models import Question

>>>Question.objects.filter(id=1)
<QuerySet [<Question: Qu'est-ce que Pybo?>]>

>>>Question.objects.get(id=1)
<Question: Qu'est-ce que Pybo?>

.filter : return queryset - plusieurs
.get: return query - un seul

profile
Développeur Frontend Junior

0개의 댓글