3๐
โ
You can use it to create links to other objects of this Model.
For example if you have many members in a website and each has an inviter (also of Member type) you can do the following:
class Member(Model):
inviter = models.ForeignKey(
'self',
related_name="invited_set"
)
If you want the inviter, you do:
Member.objects.get(id__exact=5).inviter
If you want all members that this member has invited you use:
Member.objects.get(id__exact=5).invited_set
๐คLycha
1๐
For models not yet defined:
class Gallery(models.Model):
title_image = models.ForeignKey('Image')
class Image(models.Model):
part_of = models.ForeignKey(Gallery)
since these classes refer to each other, at least one of them needs to refer to a class not yet defined.
๐คsecond
- [Django]-Mock keeps calling real function
- [Django]-Efficient SELECT with complex WHERE condition โ do I need to store a column with the calculated value?
- [Django]-The model FlatPage is already registered
- [Django]-Post LIST OF OBJECTS from HTML/axios with multipart/form-data to DRF multipart parser
Source:stackexchange.com