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.)
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.
ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases.
The __str__ method just tells Django what to print when it needs to print out an instance of the any model.
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.
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 Photo
s 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 Photo
s, raising a django.db.models.ProtectedError
if you try. The idea would be that the user would need to disassociate or delete all Photo
s before they could delete their profile.
SET_NULL
: when you delete the UserProfile
, all associated Photo
s 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 Photo
s 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 Photo
s' 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 Photo
s 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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With