Django

Django

Django

降序

在id前面加-

1
  task_objs = Task.objects.all().order_by('-id')

django里面的常用命令

shell

python3 manage.py shell

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@linux events]# python3 manage.py shell
/usr/local/lib/python3.6/site-packages/OpenSSL/_util.py:6: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
  from cryptography.hazmat.bindings.openssl.binding import Binding
Python 3.6.8 (default, Jun 20 2023, 11:53:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from alert import models
>>> from alert.models import Alert
>>> Alert.objects.all()
<QuerySet [<Alert: Alert object (18)>, <Alert: Alert object (19)>, <Alert: Alert object (20)>, <Alert: Alert object (21)>, <Alert: Alert object (22)>, <Alert: Alert object (23)>, <Alert: Alert object (24)>, <Alert: Alert object (25)>, <Alert: Alert object (26)>, <Alert: Alert object (27)>, <Alert: Alert object (28)>]>

通过这个可以进入django的上下文,非常方便

test

python3 manage.py test alert.tests.TestAlert.test_update

  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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(battle-event) ➜  battle-event git:(main) ✗ python3 manage.py test alert.tests.TestAlert.test_update
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
--- Logging error ---
Traceback (most recent call last):
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/utils/dateparse.py", line 114, in parse_datetime
    return datetime.datetime.fromisoformat(value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: year 0 is out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 1546, in to_python
    parsed = parse_datetime(value)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/utils/dateparse.py", line 129, in parse_datetime
    return datetime.datetime(**kw, tzinfo=tzinfo)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: year 0 is out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/liuliancao/projects/battle-event/alert/service.py", line 185, in AddAlert
    Alert_obj = Alert.objects.create(**params)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/query.py", line 658, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/base.py", line 814, in save
    self.save_base(
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/base.py", line 877, in save_base
    updated = self._save_table(
              ^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/base.py", line 1020, in _save_table
    results = self._do_insert(
              ^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/base.py", line 1061, in _do_insert
    return manager._insert(
           ^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/query.py", line 1805, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1821, in execute_sql
    for sql, params in self.as_sql():
                       ^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1745, in as_sql
    value_rows = [
                 ^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1746, in <listcomp>
    [
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1747, in <listcomp>
    self.prepare_value(field, self.pre_save_val(field, obj))
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1686, in prepare_value
    return field.get_db_prep_save(value, connection=self.connection)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 954, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 1607, in get_db_prep_value
    value = self.get_prep_value(value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 1585, in get_prep_value
    value = super().get_prep_value(value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 1464, in get_prep_value
    return self.to_python(value)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 1550, in to_python
    raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“0000-00-00 00:00:00” value has the correct format (YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time.']

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.11/logging/handlers.py", line 73, in emit
    if self.shouldRollover(record):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/logging/handlers.py", line 196, in shouldRollover
    msg = "%s\n" % self.format(record)
                   ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/logging/__init__.py", line 953, in format
    return fmt.format(record)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/logging/__init__.py", line 687, in format
    record.message = record.getMessage()
                     ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/logging/__init__.py", line 377, in getMessage
    msg = msg % self.args
          ~~~~^~~~~~~~~~~
TypeError: not all arguments converted during string formatting
Call stack:
  File "/home/liuliancao/projects/battle-event/manage.py", line 23, in <module>
    main()
  File "/home/liuliancao/projects/battle-event/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv
    super().run_from_argv(argv)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/core/management/commands/test.py", line 68, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/test/runner.py", line 1061, in run_tests
    result = self.run_suite(suite)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/test/runner.py", line 983, in run_suite
    return runner.run(suite)
  File "/usr/lib/python3.11/unittest/runner.py", line 217, in run
    test(result)
  File "/usr/lib/python3.11/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib/python3.11/unittest/suite.py", line 122, in run
    test(result)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/test/testcases.py", line 381, in __call__
    self._setup_and_call(result)
  File "/home/liuliancao/.local/share/virtualenvs/battle-event-6fbFqh4k/lib/python3.11/site-packages/django/test/testcases.py", line 416, in _setup_and_call
    super().__call__(result)
  File "/usr/lib/python3.11/unittest/case.py", line 678, in __call__
    return self.run(*args, **kwds)
  File "/usr/lib/python3.11/unittest/case.py", line 623, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.11/unittest/case.py", line 579, in _callTestMethod
    if method() is not None:
  File "/home/liuliancao/projects/battle-event/alert/tests.py", line 57, in test_update
    ret = AddAlert(self.alert)
  File "/home/liuliancao/projects/battle-event/alert/service.py", line 193, in AddAlert
    logger.error("create Alert error: ", str(e))
Message: 'create Alert error: '
Arguments: ("['“0000-00-00 00:00:00” value has the correct format (YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time.']",)
E
======================================================================
ERROR: test_update (alert.tests.TestAlert.test_update)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/liuliancao/projects/battle-event/alert/tests.py", line 58, in test_update
    a_id = ret['data']['id']
           ~~~~~~~~~~~^^^^^^
TypeError: 'NoneType' object is not subscriptable

----------------------------------------------------------------------
Ran 1 test in 0.008s

FAILED (errors=1)
Destroying test database for alias 'default'...

建议大家多写写测试用例,节省后续的开发时间

参考文档