How to insert a deserialized django object?
I would like to import on my django server 'A' some objects from another django server 'B'.
To do so, On server 'B' I do : python manage.py dumpdata --format=xml
Then I get the xml part I want, and on the server 'A' I try to deserialize one object : Here what I have done on python shell :
data="""<object pk="69" model="inventory.cinodeserver">
<field type="CharField" name="server_name">rfrsmh81</field>
<field type="CharField" name="server_type">Sun Fire V440</field>
<field type="CharField" name="server_serial">0449AL2A3A</field>
<field type="CharField" name="server_os">SunOS 5.8 HW 2/04</field>
<field type="CharField" name="server_fw">4.18.10</field>
</object>
"""
l=list(serializers.deserialize("xml", data))
o=l[0]
print o
<DeserializedObject: inventory.CINodeServer(pk=69)>
The problem is that I already have another object at pk=69, I just want to insert the imported object at the end of the Mysql Table.
So I tried this :
o.object.pk=None
o.save()
And I got :
I开发者_如何学GontegrityError: (1048, "Column 'cinodecompany_ptr_id' cannot be null")
Note, the model is :
class CINodeServer(CINodeCompany):
class Meta:
app_label = 'inventory'
server_name = models.CharField(max_length=64,blank=True)
server_type = models.CharField(max_length=64,blank=True)
server_serial = models.CharField(max_length=64,blank=True)
server_os = models.CharField(max_length=30,blank=True)
server_fw = models.CharField(max_length=20,blank=True)
So how can I add the imported object at the end of my SQL table ?
Your model is missing the attribute 'cinodecompany_ptr_id' as stated in the error:
IntegrityError: (1048, "Column 'cinodecompany_ptr_id' cannot be null")
Have a look at the database table of your model to see the definition for that column.
The model code you have posted is not complete. It inherits from CINodeCompany which you have not included. That missing field is probably defined in that parent class.
精彩评论