Home > front end >  Django shell get models object function does not work any ideas?
Django shell get models object function does not work any ideas?

Time:01-03

i have tried to look at the django documentation but cant find what i am looking for. I have a django models, and in this model i have defined som logic, the problem is that i cant get the value when i try fetching the recepie through django shell. I want to se if the def recepie_status is working.

My model:

class Recepie(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=60, null=False, blank=True, verbose_name='Recepie name')
description = models.TextField(max_length=500, null=True, blank=True, verbose_name='Description')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# slug = models.SlugField(max_length=255, verbose_name=_('Recepie Slug'), default=name)
share = models.BooleanField(null=True, blank=True)

def recepie_status(self):
    import datetime 
    import date, timedelta, datetime
    status=(date.today()-timedelta(days=15))
    if self.created_at > status:
        return "New"
    else:
        return "Old"

i have done this in the django shell:

>>> one = Recepie.objects.get(pk=1)
>>> print (one.name) #this works
>>> from datetime import timedelta, date, datetime
>>> print (one.recepie_status())

throws this error in the django shell

 E:\Projekt\Fooders\fooders\recepies\models.py in recepie_status(self)
 18
 19     def recepie_status(self):
 20         status=(date.today()-timedelta(days=15))
 21         if self.created_at > status:
 22             return "New"

 ModuleNotFoundError: No module named 'date'

CodePudding user response:

The issue in the following line import date

import date is cause of the error, to import date do the following

 from datetime import date
  • Related