我目前正在使用django 1.11开发一个项目,并且一段时间都在努力寻找错误原因。我不知道这是一个错误或预期的行为,并希望有人可以解决问题或指出我正确的方向。我已经搜索过类似的问题,但没有找到类似的东西。
问题是以下,我有一个基础模型类:
class BaseModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_at = models.DateTimeField(default=timezone.now, editable=False)
modified_at = models.DateTimeField(default=timezone.now, editable=False)
def save(self, *args, **kwargs):
if self.pk:
self.modified_at = timezone.now
return super(BaseModel, self).save(*args, **kwargs)
它在每个模型上运行正常,直到我需要覆盖另一个模型上的默认保存行为:
class Agent(BaseModel):
TIME_ZONES = [(tz, tz) for tz in pytz.common_timezones]
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
related_name='agent',
)
time_zone = models.CharField(choices=TIME_ZONES,
default=settings.TIME_ZONE,
max_length=100)
phone = models.CharField(max_length=50, null=True, blank=True)
email = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.user.username
def save(self, *args, **kwargs):
self.email = self.user.email
return super(Agent, self).save(*args, **kwargs)
当我尝试将新Agent模型实例保存到db中时Agent(user=user).save(),引发了异常expected string or bytes-like object。
如果我执行以下操作之一,则不会引发异常:
将Agent模型更改为直接继承models.Model并声明'created_at andmodified_at in theAgent`类
将Agent类的save方法的超级调用更改为super(BaseModel, self).super(args, *kwargs)仍然继承BaseModel
正如我之前所说,我不知道我是否遗漏了某些东西,这是预期的行为,或者它是一个错误。
完整的追溯是这样的:
Traceback:
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/views/generic/base.py" in view
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
File "/var/task/backend/users/views/sign_up.py" in dispatch
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/rest_framework/generics.py" in post
File "/var/task/backend/users/views/sign_up.py" in create
File "/var/task/backend/users/views/sign_up.py" in perform_create
File "/var/task/backend/users/serializers/sign_up.py" in save
File "/var/task/backend/users/models/agent.py" in save
File "/var/task/backend/core/models.py" in save
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/base.py" in save
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/base.py" in save_base
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/base.py" in _save_table
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/base.py" in _do_update
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/query.py" in _update
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in as_sql
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in get_prep_value
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in get_prep_value
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in to_python
File "/root/.virtualenvs/ve/lib/python3.6/site-packages/django/utils/dateparse.py" in parse_datetime
Exception Type: TypeError at /api/v1/auth/sign-up/
Exception Value: expected string or bytes-like object
self.modified_at = timezone.now
这是你的问题。你打算这样做timezone.now()。从函数引用而不是字符串或日期时间对象解析日期时崩溃。如果绕过类中的save方法,则不会触发该行为BaseModel。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。