问题
- Why would I create a recursive relationship?
- Is this the same with the above?
aField = models.ForeignKey('self')
class aClass(models.Model): aField = models.ForeignKey('aClass')
回答1:
You may need to create a recursive relationship when you would like to have parent and child nodes with identical model structure. For example if you have comments with text, data and user_id:
class Comment( models.Model ): text = models.TextField() create_date_time = models.DateTimeField() parent_comment = models.ForeignKey( 'self' )
I think yes (you can try to test it) but it's not a good form. If you change a class name then you must change the string value in brackets. If you use 'self' you haven't this headache.
来源:https://stackoverflow.com/questions/13182721/django-model-recursive-relationship