views:

20

answers:

1

I've got a chicken and egg problem here. Firstly I've got a userprofile class which builds upon the default user model of django.

class UserProfile(models.Model):
    def __unicode__(self):
        return self.user.username
    user = models.OneToOneField(User, unique=True)
    exam = models.ForeignKey('questions.Exam', null=True)

Next, I've got an Exam class

class Exam(models.Model):
    def __unicode__(self):
        return self.id
    user = models.ForeignKey(UserProfile)

The UserProfile and Exam models are in different apps. However, they both have foreignkey to each other. I know its bad design, but its my peculiar requirement in my case. However UserProfile foreignkey to Exam can be Null. But whenever I try to create Exam in Admin, I get error saying "UserProfile" is a required field. And when I try to create User, the reverse happens. Is there a way to break this deadlock? Or should I redesign my app?

A: 

should I redesign my app?

Almost certainly. However, this actual problem is caused by the fact that you have null=True, which correctly sets up the database to allow nulls on the foreign key field, but you have not added blank=True, which is what Django uses to validate correct entries. Add that, and this part at least should work.

Daniel Roseman
It worked. But I'll change my design and make it more streamlined. Thanks.
Neo