I am doing a django project where users submit ASCII art. My users are named 'Artists' and their creations are named 'Art', like so:
class Artist(models.Model):
user = models.OneToOneField(User)
birthday = models.DateField()
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Art(models.Model):
creator = models.ForeignKey(User)
...
def __unicode__(self):
return self.title
(Where the 'user' attribute of the Artist refers to the default django User model.)
What I am trying to do at the moment is to create the art and have the current logged in user being recorded as the arts creator. This is a snippet of the view that is doing that:
from django.contrib.auth.models import User
def SubmitArt(request):
art = Art(..., creator=request.user)
art.save()
The problem with this is that this only seems to work when the initial superuser that I created at the start of my project is logged in. Every other user gives me this error when the submit form is completed:
IntegrityError at /submit/
insert or update on table "artapp_art" violates foreign key constraint "artapp_art_creator_id_fkey"
DETAIL: Key (creator_id)=(3) is not present in table "artapp_artist".
My models used to read like this:
creator = models.ForeignKey('Artist')
(which never worked) before I used south to change them. I am assuming this is a problem with my database, but I'm not sure how to fix it. I am using bitnami djangostack on a windows 7 machine. Thank you very much.