3

Python version 2.7

Django version 1.11

Trying to make my first Django app saving documents, here the part of models.py

class Docs(models.Model):
    FacilityRef = models.ForeignKey(Facility)
    Date = models.DateField(default=date.today)
    Type = models.CharField(max_length=50)
    Link = models.FileField(upload_to='Docs/%Y/%m/%d')

When making migration got the following error:

Date = models.DateField(default=date.today) NameError: name 'date' is not defined

Part of views.py:

    from django.http import HttpResponse
    import datetime

Part of models.py

    from django.db import models
    import datetime

Tried to insert the following strings in views.py and models.py as it mentioned in here and here it didn't help

from django.utils import timezone
from datetime import datetime

What kind of import should I do to make this function work?

alexeyk0
  • 35
  • 1
  • 3

1 Answers1

6

date is a submodule of the datetime module. You never import a module called date, so you get a NameError when you try to call it. It should be

import datetime

Date = models.DateField(default=datetime.date.today)

or

from datetime import date

Date = models.DateField(default=date.today)
wpercy
  • 9,636
  • 4
  • 33
  • 45