简体   繁体   English

Django 未检测到 model 中的日期时间字段

[英]Django not detecting date time field in model

I have this model我有这个 model

class Moments(models.Model):
    content = models.TextField(blank=True)  
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    publishing_date = models.DateTimeField(name='Date Published', default=timezone.now)
    
    def get_publishing_date(self):
        return self.publishing_date

Now on calling the get_publishing_date, its telling that现在调用 get_publishing_date,它告诉你

Internal Server Error: /torko/feed/maX/0/
Traceback (most recent call last):
  File "C:\Users\soumi\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\soumi\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "e:\web_projects\torko\tko\views.py", line 169, in get_feed_of_user_by_id
    html = moments_mini_template.render({'moments_object': expression, 'date_published': expression.get_publishing_date().strftime("%H:%M %p, %d %B, %Y")})
  File "e:\web_projects\torko\tko\models.py", line 51, in get_publishing_date
    return self.publishing_date
AttributeError: 'Moments' object has no attribute 'publishing_date'

Another fact is that the publishing_date is present in django admin page.另一个事实是发布日期存在于 django 管理页面中。

I suppose it is because you change the field name with the name attribute: ....models.DateTimeField(name='Date....我想这是因为您使用 name 属性更改了字段名称: ....models.DateTimeField(name='Date....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM