我有以下模型:
class MyModel1(models.Model):
field1 = models.CharField(max_length=128, blank=True, null=True)
fieldrelated1 = models.OneToOneField('MyModel2', max_length=128, blank=True, null=True, related_name='mymodel2')
fieldrelated2 = models.OneToOneField('MyModel3', max_length=128, blank=True, null=True, related_name='mymodel3')
fieldrelated3 = models.OneToOneField('MyModel4', max_length=128, blank=True, null=True, related_name='mymodel4')
class MyModel2(models.Model):
field2 = models.CharField(max_length=128, blank=True, null=True)
myfk = models.ForeignKey(MyModel1, max_length=128, blank=True, null=True)
class MyModel3(models.Model):
field3 = models.CharField(max_length=128, blank=True, null=True)
test = models.CharField(max_length=128, blank=True, null=True)
class MyModel4(models.Model):
field4 = models.CharField(max_length=128, blank=True, null=True)
test = models.CharField(max_length=128, blank=True, null=True)
然后,此方法:
def create_child_objects(instance, created, rad, **kwargs):
if not created or rad:
return
field1 = instance.field1
if not instance.fieldrelated1_id:
fieldrelated1, _ = MyModel2.objects.get_or_create(field1=field2)
instance.fieldrelated1 = fieldrelated1
if not instance.fieldrelated2_id:
fieldrelated2, _ = MyModel3.objects.get_or_create(field1=field3)
instance.fieldrelated2 = fieldrelated2
if not instance.fieldrelated3_id:
fieldrelated3, _ = MyModel4.objects.get_or_create(field1=field4)
instance.fieldrelated3 = fieldrelated3
instance.save()
models.signals.post_save.connect(create_child_records, sender=MyModel1, dispatch_uid='create_child_objects')
到目前为止,该方法可以保存父级,并在创建子级时保存特定字段。
例如,我想将实际的父实例保存到孩子的CharField
字段中时,而不是将父辈的CharField
保存到孩子ForeignKey
中来,就像这样:
def create_child_objects(instance, created, rad, **kwargs):
if not created or rad:
return
field1 = instance.__str__
if not instance.fieldrelated1_id:
fieldrelated1, _ = MyModel2.objects.get_or_create(field1=myfk)
instance.fieldrelated1 = fieldrelated1
if not instance.fieldrelated2_id:
fieldrelated2, _ = MyModel3.objects.get_or_create(field1=field3)
instance.fieldrelated2 = fieldrelated2
if not instance.fieldrelated3_id:
fieldrelated3, _ = MyModel4.objects.get_or_create(field1=field4)
instance.fieldrelated3 = fieldrelated3
instance.save()
models.signals.post_save.connect(create_child_objects, sender=MyModel1, dispatch_uid='create_child_records')
使用以下方法将父对象保存到db中:
def __str__(self):
return ('{}: {}: {}'.format(self.field1, self.field2, self.field3))
因此,在第二个create_child_objects
之后,它向我抛出:
TypeError at /admin/mymodel/instance/add/
int() argument must be a string, a bytes-like object or a number, not 'method'
那么,如何将父对象描述符保存到父对象的ForeignKey
字段中?
编辑
myfk
字段上保存的是MyModel1
的ID,因此保存它就足够了,但是当我尝试这样field1 = MyModel1.id
它说:
ValueError at /admin/mymodel/instance/add/
Cannot assign "98": "MyModel2.field1.id" must be a "MyModel1" instance.
答案 0 :(得分:1)
field1 = instance.__str__
更改为
field1 = instance.__str__()
或
field1 = str(instance)
__str__
是一种方法,因此您的分配使field1
指向此方法,而不是方法的结果。
所有MyModel[2-4].objects.get_or_create(field1=field2)
行都是错误的:
field1
字段,因此正确的过滤器应为field[2-4]=field1
__str__
方法是这些ID的组合,必须首先解析此字符串;只有提取的值会匹配我不了解您的__str__
方法的工作方式,因为该模型没有field2
,field3
和field4
。
我建议您重新考虑该方法。看看generic relations。