1

I have 2 models like this:

class User(MP_Node):
    login = models.CharField(max_length=32, unique=True)
    password = models.CharField(max_length=128)


class Parent(User):
    fname = models.CharField(max_length=32)
    fmobileno = models.CharField(max_length=16)
    foccupation = models.CharField(max_length=16)
    fannual_income = models.FloatField()

Now I save a new Parent like that:

parent = Parent()            
        user = User.objects.get(login='root')
        parent.user_ptr = user.add_child()
        parent.login = registration_form.cleaned_data.get('login')
        parent.password = registration_form.cleaned_data.get('password')
        parent.password = hashlib.md5(parent.password.encode('utf-8')).hexdigest()

        parent.fname =registration_form.cleaned_data.get('fname')
        parent.fmobileno =registration_form.cleaned_data.get('fmobileno')
        parent.foccupation =registration_form.cleaned_data.get('foccupation')
        parent.fannual_income =registration_form.cleaned_data.get('fannual_income')

but I get error Column 'depth' cannot be null Are there any one know how to save a new Parent into database?

Thank you so much!

songoku1610
  • 1,576
  • 1
  • 13
  • 17

1 Answers1

0

I have resolved it Simple by use this

u = user.add_child()
parent = Parent(user_ptr_id=u.pk)  
parent.__dict__.update(u.__dict__)
parent.save()

Referent from here Django model inheritance: create sub-instance of existing instance (downcast)?

Community
  • 1
  • 1
songoku1610
  • 1,576
  • 1
  • 13
  • 17