ForeignKey field with null=True gives 'NoneType' object has no attribute id <p>I am new to Django.</p> <p>I have the following models.py</p> <pre><code>from django.db import models from django.contrib.auth.models import User from django.utils import timezone import os def get_upload_path(instance, filename): return os.path.join( "folder_%d" % instance.folder.id, filename) class Folder(models.Model): folder_name=models.CharField(max_length=100) parent_folder=models.ForeignKey('self', null=True, blank=True) folder_description=models.TextField(max_length=200) def __unicode__(self): return self.folder_name class File(models.Model): folder=models.ForeignKey(Folder, null=True, blank=True) uploaded_file=models.FileField(upload_to=get_upload_path) pub_date = models.DateTimeField('date published',default=timezone.now()) tag=models.ManyToManyField(FileTag) notes=models.TextField(max_length=200) uploader=models.ForeignKey(User) def __unicode__(self): return str(self.uploaded_file) def filename(self): return os.path.basename(self.uploaded_file.name) </code></pre> <p>If I try to save a File object with folder attribute with null its gives me 'AttributeError' saying 'NoneType' Object has no attribute id</p>