Relationships
Examples:
species_owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='species_owner')
fossil_species = models.ForeignKey(DBSpecies, on_delete=models.SET_NULL, verbose_name="Species", related_name='species', limit_choices_to={'species_is_archived': False}, null=True)
associated_fossils = models.ManyToManyField('self', verbose_name="Associated fossils", blank=True)
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
fossils = models.ManyToManyField(DBFossil, through='FossilEvent')
fossil = models.ForeignKey(DBFossil, on_delete=models.CASCADE)
event = models.ForeignKey(DBEvent, on_delete=models.CASCADE, related_name='fossil_events')
RELATIONSHIPS
- One-to-one relationship:
In Django, a one-to-one relationship is defined using a OneToOneField. This field creates a unique relationship between two model classes, where each instance of the first model class is associated with only one instance of the second model class, and vice versa. For example, in a Django project for a university, each student can have only one student ID, and each ID can be associated with only one student. In this case, you would define a one-to-one relationship between the Student and StudentID model classes using a OneToOneField.
- Many-to-one relationship:
In Django, a many-to-one relationship is defined using a ForeignKey field. This field creates a relationship between two model classes, where each instance of the first model class can be associated with multiple instances of the second model class, but each instance of the second model class can be associated with only one instance of the first model class. For example, in a Django project for a company, each employee can report to only one manager, but each manager can have multiple employees. In this case, you would define a many-to-one relationship between the Employee and Manager model classes using a ForeignKey field.
- Many-to-many relationship:
In Django, a many-to-many relationship is defined using a ManyToManyField. This field creates a relationship between two model classes, where each instance of the first model class can be associated with multiple instances of the second model class, and vice versa. For example, in a Django project for a bookstore, each book can be written by multiple authors, and each author can write multiple books. In this case, you would define a many-to-many relationship between the Book and Author model classes using a ManyToManyField.
on_delete=models.
- CASCADE When a referenced object is deleted, also delete the objects that have foreign key references to it. This can lead to a cascading deletion of multiple objects, so use with caution.
- PROTECT Prevent deletion of the referenced object by raising a ProtectedError if it has any referencing objects. This is useful if you want to prevent accidental deletion of important objects.
- SET_NULL Set the foreign key to NULL for all referencing objects when the referenced object is deleted. This is useful if you want to retain the referencing objects but remove the association to the deleted object.
- SET_DEFAULT Set the foreign key to its default value when the referenced object is deleted.
- SET() Set the foreign key to the specified value when the referenced object is deleted. This is useful for customizing the behavior of the foreign key.
- DO_NOTHING Do nothing when the referenced object is deleted. This can lead to database integrity issues, so use with caution.
help_text and relationsips
ForeignKey, ManyToManyField and OneToOneField require the first argument to be a model class, so use the verbose_name keyword argument instead of help_text
Other felds: fossil_name = models.CharField("Fossil name", max_length=250)
Relationships: fossil_species = models.ForeignKey(DBSpecies, on_delete=models.PROTECT, verbose_name="Species", related_name='species')