I am new to python and django please help me with this doubt .How to create a class in django which will be deleted when the element in other class is deleted ?
Asked
Active
Viewed 28 times
0
-
What have you tried to do this? – Sunderam Dubey May 26 '22 at 10:50
-
Please provide enough code so others can better understand or reproduce the problem. – Community May 26 '22 at 11:59
-
Does this answer your question? [What does on\_delete do on Django models?](https://stackoverflow.com/questions/38388423/what-does-on-delete-do-on-django-models) – Abdul Aziz Barkat May 26 '22 at 14:03
2 Answers
0
You can do like this:
class Company:
name = models.CharField(blank=False, max_length=150)
class Car:
company = models.ForeignKey(Company, on_delete=models.CASCADE)
Whenever company is deleted. All the cars with that company will also get deleted. If you do on_delete=models.PROTECT
it will prevent deletion of records because it's protected.

RiveN
- 2,595
- 11
- 13
- 26

Hemal Patel
- 878
- 6
- 16