0
下面的配置模型允許上傳.ZIP並將提取的網絡設備.TXT文件綁定到設備配置所在的位置。django模型方法無法執行數據庫提交
class Configuration(models.Model):
configfile = models.FileField('Configuration File Upload', upload_to='somecompany/configs/', help_text='Select a .ZIP file which contains .TXT file configuration dumps from devices which belong to a single location.')
location_name = models.ForeignKey('Location', help_text='Associate the .ZIP file selected above to the location from which the device .TXT file configuration dumps were taken.')
我延伸默認保存模型方法的類,以允許的.ZIP(代碼未示出爲簡潔起見)處理。我解析了提取的.TXT文件,將所有需要的信息收集到變量中,並且試圖將該信息插入到我的數據庫中,但它失敗了。具體而言,下面我顯示所有從提取的.txt文件中的單獨一個所收集的值中的一個例子(隱私略有修改)和我在DB插入嘗試:
dbadd_ln = 'Red Rock'
dbadd_dn = 'DEVICE4'
dbadd_manu = 'cisco'
dbadd_os = 'nxos'
dbadd_dt = '-'
dbadd_prot = '-'
dbadd_cred = '-'
dbadd_ser = 'ABCD1234'
dbadd_addr = '10.10.10.10'
dbadd_model = 'N7K-C7010'
dbadd_ram = '2048256000'
dbadd_flash = '1109663744/1853116416'
dbadd_image = 'n7000-s1-dk9.5.2.9.bin'
dbadd = Device(location_name=dbadd_ln, device_name=dbadd_dn, device_type=dbadd_dt, protocol=dbadd_prot, credential=dbadd_cred, serial=dbadd_ser, address=dbadd_addr, manufacturer=dbadd_manu, model=dbadd_model, ram=dbadd_ram, flash=dbadd_flash, os=dbadd_os, image=dbadd_image)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "c:\code-projects\MYVIRTUALENV\lib\site-packages\django\db\models\base.py", line 431, in __init__
setattr(self, field.name, rel_obj)
File "c:\code-projects\MYVIRTUALENV\lib\site-packages\django\db\models\fields\related_descriptors.py", line 207, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "'Red Rock'": "Device.location_name" must be a "Location" instance.
「紅石」是一個合法的它已經存在於我的數據庫位置進入...
>>> Location.objects.filter(location_name='Red Rock')
[Location: Red Rock]
...所以我想我就不清楚這究竟是怎樣:
"Device.location_name" must be a "Location" instance.
任何援助,以幫助解決這個問題,我s讚賞。提前致謝。