Python使用onetomany关系填充数据库

2024-10-16 20:41:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我是初学者,所以请对我放轻松一点。我正在编写一个脚本,这样当我决定删除数据库时,不必一直输入数据。我的整个剧本都很好,除了处理一对多的关系。它不会保存到数据库。有谁能告诉我我做错了什么,或者给我指出正确的方向吗?在

脚本:

try:
 pmod.Instrument.objects.get(title='kjkjsdfsadfs')
except pmod.Instrument.DoesNotExist:
 u = pmod.Instrument()
 u.title = 'Bach 42 Trombone'
 u.price = 550.00
 u.soundDescription = 'Good'
 u.functionalityDescription = 'Good'
 u.damageDescription = 'Good'
 u.accessoryDescription = 'Good'
 u.customerName = 'Jake'
 u.customerEmail = 'ks@gmail.com'
 u.instrumentCategory = 1
 print('Good2')
 u.save()

 print('Instrument1 saved')

型号:

^{pr2}$

Tags: 数据脚本数据库getobjects关系title方向