I’m a new Django (1.3) user and I’m setting up the typical blog/article application. I have a model in which I’d like to save the current username (to see who created the article). I’ve implemented advice on this, but my code crashes the server with the message: “NameError: name ‘User’ is not defined”. Here’s my model:
from django.db import models
class Resort(models.Model):
name = models.CharField(max_length=300)
description = models.TextField()
location = models.CharField(max_length=300)
ski_hire = models.TextField()
weather = models.TextField()
piste_map = models.TextField()
webcam = models.TextField()
snow = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
author = models.ForeignKey(User)
def __unicode__(self):
return self.name
I have a sneaking suspicion that I need to override the save method in admin.py, but I’m not sure how to do this. Any advice would be greatly appreciated. Thanks!
You need to make sure you import user from
django.contrib.auth.modelsin your models.py when using User in a foreign key.In your admin.py you can use save_model: