Xtal
Xtal

Reputation: 305

Django ORM delete

Ok so i have 2 models simplified

Class A:
    name  = charfield

Class B:
     name = charfield
     linked = foreignkey(A)

My question how I can delete A.name and B.linked but protecting B.name from being deleted?

Upvotes: 0

Views: 155

Answers (1)

Daniel Rosenthal
Daniel Rosenthal

Reputation: 1386

You can set the ForeignKey.on_delete argument (https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.on_delete)

You'll want to do

linked = models.ForeignKey(A, blank=True, null=True, on_delete=models.SET_NULL)

Upvotes: 2

Related Questions