Search code examples
djangoone-to-onerelationships

Django One to One - Don't want delete related model


So here is the problem. I got 2 models:

RefereeLevel and Referee

Here is both:

class RefereeLevel(models.Model):
    level = models.PositiveSmallIntegerField(blank=False,default=1,verbose_name=_("level"),unique=True)
    salary = models.DecimalField(blank=False,default=0.00,decimal_places=2,max_digits=4,verbose_name=_("salary"))

    def __unicode__(self):  # Python 2.7: def __unicode__(self):
        return self.level

And the second class:

 class Referee(models.Model):
    member = models.OneToOneField(Member,related_name='member',blank=False)
    information = models.ForeignKey(RefereeLevel,related_name='information',blank=False,null=True,default=1)

What happens now is that if I delete a RefereeLevel, the Referee with that level is deleted. But I don't want that, I want the Referee's information be set to none.

Is that possible?

Thanks, Ara


Solution

  • You need to set the on_delete parameter.

    In your case :

    information = models.ForeignKey(RefereeLevel,related_name='information',blank=False,null=True,default=1, on_delete=models.SET_NULL