我有一个ModelBase,还有ModelA和ModelB。
我想把ModelA实例改为ModelB实例。(我可以处理它们之间属性的差异)
我看过相关的问题,但对我来说不太适用。
如何从现有的基本模型实例中创建继承的Django模型实例?
在Django模型上更改子类的类
- 编辑
当你有地点 - 餐厅/酒吧关系时,我认为能够将餐厅切换成酒吧是相当合理的。
我有一个ModelBase,还有ModelA和ModelB。
我想把ModelA实例改为ModelB实例。(我可以处理它们之间属性的差异)
我看过相关的问题,但对我来说不太适用。
如何从现有的基本模型实例中创建继承的Django模型实例?
在Django模型上更改子类的类
当你有地点 - 餐厅/酒吧关系时,我认为能够将餐厅切换成酒吧是相当合理的。
我曾经遇到过同样的问题,yuvi和arctelix的解决方案对我都没有用。yuvi的解决方案会导致错误,arctelix的解决方案则创建新对象并赋予新pk。
这里的目标是在保留原始超类及其旧pk的情况下更改子类模型。
第一步:删除旧的子类,保留超类。请查看Django文档。
第二步:添加新的子类及其字段,并将超类传递给它。请查看此q
示例:一个地方可以是餐馆或咖啡馆,你想把一个餐厅变成咖啡馆;如下所示:
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
class Caffe(Place):
serves_hot_dogs = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
class Restaurant(Place):
serves_tea = models.BooleanField(default=False)
serves_coffee = models.BooleanField(default=False)
# get the objecte to be changed
rest = Restaurant.objects.get(pk=1) #arbitrary number
#delete the subclass while keeping the parent
rest.delete(keep_parents=True)
place = Place.objects.get(pk=1) # the primary key must be the same as the deleted restaurant
# Create a caffe and pass the original place
caffee = Caffe(place_ptr_id=place.pk) #this will empty the parent field
#update parent fields
caffee.__dict__.update(place.__dict__)
#add other field
........
#save the caffe
caffee.save()
save_base
或setattr
方法,而不是字典更新。 - DylanYounginstance = ModelA.objects.get(pk=1) #arbitrary
# find parent class fields:
fields = [f.name for f in ModelBase._meta.fields]
# get the values from the modelA instance
values = dict( [(x, getattr(instance, x)) for x in fields] )
#assign same values to new instance of second model
new_instance = ModelB(**values)
#add any additional information to new instance here
new_instance.save() #save new one
instance.delete() # remove the old one
但是,如果 ModelBase 不是抽象的,您将需要进行额外的解决方法:
fields = [f.name for f in ModelBase._meta.fields if f.name != 'id']
#... other parts are the same...
new_instance.modelbase_ptr = instance.modelbase_ptr #re-assign related parent
instance.delete() #delete this first!
new_instance.save()
在yuvi的答案中,手动分配modelbase_ptr并保存失败,因为在保存之前instance.modelbase_ptr被删除。
在yuvi的答案基础上,这里提供一个更明确的例子,并且适用于抽象和非抽象转换:
可选地保留原始id,并遵循django文档推荐的方法。
ex_model = ModelA
new_model = ModelB
ex_instance = ex_model.objects.get(pk=1) #arbitrary
# find fields required for new_model:
new_fields = [f.name for f in new_model._meta.fields]
# make new dict of existing field : value
new_fields_dict = dict( [(x, getattr(ex_instance, x, None)) for x in new_fields] )
# Save temp copy as new_model with new id
# modelbase_ptr will be created automatically as required
new_fields_dict.pop('project_ptr', None)
temp_instance = new_model(**new_fields_dict)
temp_instance.pk = None
temp_instance.id = None
temp_instance.save()
# you must set all your related fields here
temp_instance.copy_related(ex_instance)
ex_instance.delete()
# (optional) Save final copy as new_model with original id
final_instance = new_model(**new_fields_dict)
final_instance.save()
final_instance.copy_related(temp_instance)
temp_instance.delete()
# here are the removed fields, handle as required
removed_fields = [f.name for f in ex_model._meta.fields if f.name not in new_fields_dict.keys()]
removed_fields_dict = dict( [(x, getattr(ex_instance, x, None)) for x in removed_fields] )
在类ModelBase中:
def copy_related(self, from):
# include all your related fields here
self.related_field = from.related_field.all()
self.related_field_a = from.related_field_a.all()