Move a python / django object from a parent model to a child (subclass)

后端 未结 5 1145
[愿得一人]
[愿得一人] 2021-01-03 01:44

I am subclassing an existing model. I want many of the members of the parent class to now, instead, be members of the child class.

For example, I have a model Swall

5条回答
  •  别那么骄傲
    2021-01-03 02:00

    It's a bit of a hack, but this works:

    swallow = Swallow.objects.get(id=1)
    swallow.__class__ = AfricanSwallow
    # set any required AfricanSwallow fields here
    swallow.save()
    

提交回复
热议问题