92๐
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 ๐
5๐
I believe you can even exclude the app name which would look like:
ref_employee= models.ForeignKey('Employee',null=True,blank=True)
- [Django]-Django Admin โ Specific user (admin) content
- [Django]-How to get the domain name of my site within a Django template?
- [Django]-Atomic increment of a counter in django
4๐
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)
- [Django]-"{% extends %}" and "{% include %}" in Django Templates
- [Django]-Django: FloatField or DecimalField for Currency?
- [Django]-Django rest framework โ filtering for serializer field
3๐
Yes, this is correct but consider writing 'null=True'
because if you donโt mention that, your database recursively creates a joint table.
- [Django]-Django 1.8 migrate is not creating tables
- [Django]-How to set or get a cookie value in django
- [Django]-Django: Catching Integrity Error and showing a customized message using template
0๐
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.
- [Django]-In Django, how do I check if a user is in a certain group?
- [Django]-Django: Assigning variables in template
- [Django]-Django 1.7 migrate gets error "table already exists"