Assume we have class Employee. I want to have a field which references a different instance of the same class.
How to write this? How about the followi
You can reference other models by name (using a string, including package), instead of by the class directly:
So, if your Employee class is in the hr app:
class Employee(models.model):
other_employee = models.ForeignKey('hr.models.Employee', null=True, blank=True)
A constraint forcing id and ref_employee_id to have separate values is outside the scope of Django's ORM. You will need to add said constraint at the database level, via SQL in syncdb or manually.
I believe you can even exclude the app name which would look like:
ref_employee= models.ForeignKey('Employee',null=True,blank=True)
http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
To create a recursive relationship -- an object that has a many-to-one relationship with itself -- use
models.ForeignKey('self').
So you have it right. It's usually faster to determine if code will do what you want by running it :)