ordering in a Python module

django, python

Solution

You can use a string with the class name to instantiate the first model before the other has been created:

class Images(models.Model):
    job=models.OneToOneField('Jobs')
    image=models.ImageField()

class Jobs(models.Model):
    picture=models.ForeignKey(Images, null=True)

From the docs on models:

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.

Problem

``` class Images(models.Model): job=models.OneToOneField(Jobs) image=models.ImageField() class Jobs(models.Model): picture=models.ForeignKey(Images, null=True) ``` it gives an error on ``` job=models.OneToOneField(Jobs) ``` it because class job define later in the module, but if i change positions of these two classes then it will give me an error on ``` picture=models.ForeignKey(Images, null=True) ``` What should i do in this case?(without put classes in different modules)

Original source