-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.txt
79 lines (66 loc) · 3.32 KB
/
commands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
🎓 El método filter:
-Question.objects.filter(pk=3) : arroja un resultado pero si no tuviera un registro en el pk 3 bota un arreglo vacio
-Question.objects.filter(question_text__startswith="¿Cuál") : arroja un resultado en este caso, dos registros.
-Question.objects.filter(pub_date__year=timezone.now().year) : arroja todos los registros dgitados en el año actual
__gt = Mayor que
__gte = Mayor o igual que
__lt = Menor que
__lte = Menos o igual que
__startswith = Empieza con
__endswith = Termina con
|
Les dejo el link donde podrán ver otros método para la búsqueda https://docs.djangoproject.com/en/4.0/ref/models/querysets/#field-lookups
🎓 Para limpíar la pantalla o la terminal es de la siguiente manera:
import os + enter
os.system("cls) + enter
¡¡ Y listo !!
🎓Acceciendo al conjunto de respuestas
-from polls.models import Question, Choice
-q = Question.objects.get(pk=1)
>>> q
<Question: ¿Cuál es el mejor curso de Platzi?>
>>> q.choice_set.all()
<QuerySet []>
>>> q.choice_set.create(choice_text="Curso Básico de Python", votes=0)
<Choice: Curso Básico de Python>
>>> q.choice_set.create(choice_text="Curso de Fundamentos de Ingeniería de Software", votes=0)
<Choice: Curso de Fundamentos de Ingeniería de Software>
>>> q.choice_set.create(choice_text="Curso Básico de Javascript", votes=0)
<Choice: Curso Básico de Javascript>
>>> q.choice_set.all()
<QuerySet [<Choice: Curso Básico de Python>, <Choice: Curso de Fundamentos de Ingeniería de Software>, <Choice: Curso Básico de Javascript>]>
>>> q.choice_set.count()
3
>>> from django.utils import timezone
>>> Choice.objects.filter(question__pub_date__year=timezone.now().year)
<QuerySet [<Choice: Curso Básico de Python>, <Choice: Curso de Fundamentos de Ingeniería de Software>, <Choice: Curso Básico de Javascript>]>
🎓 Corrigiendo un error en was_published_recently
py manage.py shell
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import datetime
>>> from django.utils import timezone
>>> from polls.models import Question
>>> q = Question(question_text="¿Quién es el mejor Course Director de Platzi?", pub_date=timezone.now() + datetime.timedelta(days=30))
>>> q.was_published_recently()
True
Podemos ver que aunque no es una fecha reciente, dice que si lo es!! 😣
Acerca del Test:
py manage.py test polls
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_was_published_recently_with_future_questions (polls.tests.QuestionModelTest)
was_published_recently return False for questions whose pub_date is in the future
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Gerox\Documents\Estudio\Cursos\Platzi\Curso Básico de Django con Python\premiosPlatzi\premiosplatziapp\polls\tests.py", line 18, in test_was_published_recently_with_future_questions
self.assertIs(future_question.was_published_recently(), False)
AssertionError: True is not False
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
Destroying test database for alias 'default'...