pythondjangodjango-modelsforeign-keyssetdefault

SET_DEFAULT for ForeignKey column itself


I'm create this table in Django models:

class customer(models.Model):
    name = models.CharField(max_length=100)
    inviter = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_DEFAULT)

inviter is ForeignKey of self

Now I want set_default one row of customer table for inviter column.


Solution

  • I think you should add default keyword

    inviter = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_DEFAULT, default=get_default)
    

    and predefine get_default method.