Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django models ForeignKey on_delete attribute: full meaning?

Here some models:

class UserProfile(models.Model):       name = models.CharField(max_length=30)     email = models.EmailField(unique=True, db_index=True)      birthday = models.DateField()  class Photo(models.Model):     user = models.ForeignKey(UserProfile)     description = models.TextField(blank=True)     photo = models.ImageField(upload_to='img/photo') 

Let's say a user has 10 photos (10 objects of Photo model). When user deletes himself will all those 10 Photo database rows delete themselves automatically? (I've read docs, but English is not my native language, so I didn't understand everything about the on_delete attribute.)

like image 703
Vitalii Ponomar Avatar asked Dec 17 '11 09:12

Vitalii Ponomar


People also ask

What does on_delete do in Django models?

Django is famous for its robust relational management database management system. The on_delete handle is used to handle the deletion of reference data to maintain the database integrity.

What is Django ForeignKey model?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases.

What is __ str __ in Django?

The __str__ method just tells Django what to print when it needs to print out an instance of the any model.

Why we use ForeignKey in Django models?

What is ForeignKey in Django? ForeignKey is a Field (which represents a column in a database table), and it's used to create many-to-one relationships within tables. It's a standard practice in relational databases to connect data using ForeignKeys.


1 Answers

I'll go through the values for on_delete as they apply to this case. As it notes in the docs, these are all in that models module, so you'd use it as models.ForeignKey(UserProfile, on_delete=models.CASCADE), etc.

These rules apply however you delete an object, whether you do it in the admin panel or working directly with the Model instance. (But it won't take effect if you work directly with the underlying database in SQL.)

  • CASCADE: when you delete the UserProfile, all related Photos will be deleted too. This is the default. (So in answer to that aspect of your question, yes, if you delete your user account the photos will be deleted automatically.)

  • PROTECT: this will stop you from deleting a UserProfile with related Photos, raising a django.db.models.ProtectedError if you try. The idea would be that the user would need to disassociate or delete all Photos before they could delete their profile.

  • SET_NULL: when you delete the UserProfile, all associated Photos will still exist but will no longer be associated with any UserProfile. This would require null=True in the ForeignKey definition.

  • SET_DEFAULT: when you delete the UserProfile, all associated Photos will be changed to point to their default UserProfile as specified by the default attribute in the ForeignKey definition (you could use this to pass "orphaned" photos off to a certain user - but this isn't going to be common, SET_NULL or SET() will be much more common)

  • SET(): when you delete the UserProfile, the target of the Photos' ForeignKey will be set to the value passed in to the SET function, or what it returns if it is a callable. (Sorry, I haven't explained that well, but the docs have an example which explains better.)

  • DO_NOTHING: when you delete the UserProfile, all related Photos will remain unaltered, thus having a broken reference, unless you have used some other SQL to take care of it.

(Also, on_delete isn't a method. It's an attribute of the ForeignKey field.)

like image 80
Chris Morgan Avatar answered Sep 23 '22 13:09

Chris Morgan