3

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

Yahya Yahyaoui
  • 2,833
  • 23
  • 30
Ara Sivaneswaran
  • 365
  • 1
  • 10
  • 25

2 Answers2

7

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
Ambroise
  • 1,649
  • 1
  • 13
  • 16
1

What you can do is to set on_delete param like

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

on_delete=models.SET_NULL will set the foreign key value to null

anuragal
  • 3,024
  • 19
  • 27