0

I'm trying to create a program in Django for an employee database How do I solve a problem when deleting one of the children, the employee and all his other information is deleted?

I use

class EmployeesAdmin(admin.ModelAdmin): inlines = [PersonalInfoInline,ChildrenInline,]

class Employees(models.Model):
    code = models.CharField(_('رقم التسجيل '), max_length=20)
    image_profile = models.ImageField(_(" الصورة الشخصية "), upload_to="profile", blank=True, null=True)
    firstname = models.CharField(_('الاسم '), max_length=100)
    middlename = models.CharField(_('اسم الاب '), max_length=100, blank=True, null=True)
    lastname = models.CharField(_('اسم الجد '), max_length=100, blank=True, null=True)
    data_birth = models.DateField(_('تاريخ الولادة '), blank=True, null=True)
    nikename = models.CharField(_('اللقب '), max_length=100, blank=True, null=True)



class Meta:
   ordering = ['date_hired']
        get_latest_by = 'date_hired'

class Children(models.Model):  # المعلومات الابناء ===================================
    employee = models.ForeignKey(Employees, on_delete=models.SET_NULL, null=True)
    name = models.CharField(max_length=255, blank=True, null=True)
    sex = models.CharField(max_length=32, choices=(('ذكر', 'ذكر'), ('انثى', 'انثى'), ('otherother', 'other')),
                           blank=True)
    dob = models.DateField(blank=True, null=True)

    

    def __str__(self):
        return self.name or ''

Each employee has his own children

1 Answers1

0

At What does on_delete do on Django models?, it says that SET_NULL sets the reference to NULL if it is nullable. For instance, when you delete a User, you might want to keep the comments he posted on blog posts. It never said anything about the children being set to NULL.

Arjun
  • 1
  • 3