简体   繁体   中英

Django not detecting date time field in model

I have this 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

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.

I suppose it is because you change the field name with the name attribute: ....models.DateTimeField(name='Date....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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