Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django models: One to one relationship between objects of the same model class [duplicate]

Possible Duplicate:
Can I have a Django model that has a foreign key reference to itself?

I want to implement a simple folder-file like structure in my Django app. So I have a model for storing folders, but I also would like to store relation between this folder and parent folder. The simplified version of model would look like following:

class mFolder(models.Model):
    name = models.CharField(max_length=50)
    parentFolder = models.ForeignKey(mFolder, unique=False, related_name="childrenFolders")

However this is not possible, beacause mFolder is not declared yet.

Is there any simple solution for that problem?

Thanks for your help in advance.

like image 884
Paweł Sopel Avatar asked Oct 04 '12 12:10

Paweł Sopel


1 Answers

Should be 'self':

parentFolder = models.ForeignKey('self', unique=False, related_name="childrenFolders")
like image 175
Jingo Avatar answered Oct 20 '22 17:10

Jingo