0

I've attempted to add a second ForeignKey relationship to a Django Model which relates to a model which hasn't yet been created.

class Forms2012(models.Model):
    """
    Employer forms for the 2012-13 tax year
    """
    employer = models.ForeignKey('Employer', blank=True, null=True)
    # employee model is yet to be implemented
    employee = models.ForeignKey('Employee', blank=True, null=True)
    name = models.CharField(
        max_length=50,
        choices=constants.FORM_CHOICES)
    description = models.CharField(max_length=255)
    datemodified = models.DateTimeField()

As you might expect this is giving a not installed or is abstract error. However I've been told it should be possible to validate this because that key is optional.

Could someone explain if this is possible, I've added the flags blank=True, null=True to the FK but model validation fails so I think I'm fighting a losing battle.

markwalker_
  • 12,078
  • 7
  • 62
  • 99

2 Answers2

1

Why don't you make (temporary) dummy models?

John Peters
  • 1,177
  • 14
  • 24
  • Thats what I wanted to do but was advised otherwise. I wasn't sure if there was something I'd missed as I've not been working with Django for very long. – markwalker_ Oct 26 '12 at 08:04
  • I wouldn't know why not to do this. Even if you don't use South it is easy enough to drop the dummy table and do a syncdb when you have the actual model done. – John Peters Oct 26 '12 at 08:09
0

I would recommend that you implement a "stub" Employee model eg

class Employee(models.Model):
    pass

If you are using database migrations ( eg South ), which you should, it should be a simple matter to "fill out" the Employee class later on.

However, if you want a fairly involved "solution" to the problem, you can have a look at the accepted answer in this question. The author of the question & answer admits that it is ugly ( even though it works ). The "stub" solution is better.

Community
  • 1
  • 1
Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30