gpt4 book ai didi

python - 在 django 中减去日期

转载 作者:行者123 更新时间:2023-11-28 20:12:31 25 4
gpt4 key购买 nike

我正在尝试减去两个日期。我读到它可以像减去两个类属性一样简单地完成,但是当我执行 manage.py runserver 时出现此错误

expected string or bytes-like object

代码:

class example(models.Model):
date1 = models.DateField()
date2 = models.DateField()
diff = models.IntegerField(null=True, blank=True)

def save(self, *args, **kwargs):
self.diff = self.date1 - self.date2
super(example, self).save(*args, **kwargs)

当我将鼠标光标放在 - 符号上时,PyCharm 显示:“类‘DateField’未定义‘sub’,因此不能在其实例上使用‘-’运算符”

我该怎么做才能解决这个问题/修复操作?

回溯:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/contas/atestado_medico/add/

Django Version: 2.2.5
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'contas',
'import_export']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\options.py" in wrapper
606. return self.admin_site.admin_view(view)(*args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\sites.py" in inner
223. return view(request, *args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\options.py" in add_view
1634. return self.changeform_view(request, None, form_url, extra_context)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\decorators.py" in _wrapper
45. return bound_method(*args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\options.py" in changeform_view
1522. return self._changeform_view(request, object_id, form_url, extra_context)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\options.py" in _changeform_view
1561. self.save_model(request, new_object, form, not add)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\options.py" in save_model
1088. obj.save()

File "C:\Users\Joao\Desktop\Henrisa\contas\models.py" in save
205. super(Atestado_Medico, self).save(*args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py" in save
741. force_update=force_update, update_fields=update_fields)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py" in save_base
779. force_update, using, update_fields,

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py" in _save_table
870. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py" in _do_insert
908. using=using, raw=raw)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py" in _insert
1186. return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1334. for sql, params in self.as_sql():

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
1278. for obj in self.query.objs

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
1278. for obj in self.query.objs

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py" in <listcomp>
1277. [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py" in prepare_value
1218. value = field.get_db_prep_save(value, connection=self.connection)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
789. return self.get_db_prep_value(value, connection=connection, prepared=False)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
1275. value = self.get_prep_value(value)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
1270. return self.to_python(value)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py" in to_python
1232. parsed = parse_date(value)

File "C:\Users\Joao\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\dateparse.py" in parse_date
74. match = date_re.match(value)

Exception Type: TypeError at /admin/contas/atestado_medico/add/
Exception Value: expected string or bytes-like object

最佳答案

如下所示更改您的save 函数,因为减去日期会得到一个 timedelta 对象:

def save(self, *args, **kwargs):
self.diff = (self.date1 - self.date2).days
super(example, self).save(*args, **kwargs)

关于python - 在 django 中减去日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com