Django admin无法使用M2M字段保存新记录 - 在可以使用多对多关系之前,实例需要具有主键值

时间:2013-06-11 17:24:51

标签: django django-admin django-orm

问题陈述: 我使用Django管理员来管理许多表,其中一些表具有多对多关系。我无法在已定义多个字段的表(模型)中保存新记录。我能够很好地渲染添加表单。问题仅在于尝试保存记录。更新现有记录时,我没有遇到同样的问题。

使用下面的模型,我收到以下错误:'Bout'实例需要具有主键值才能使用多对多关系。

Bout模型与Equipment模型有多对多的关系。 BoutEquipment模型是中间模型。

我已经在StackOverflow和Google上研究过这个问题的高低,但到目前为止还无法找到解决方案。

披露:我是Django的新手,也是Python的新手。我希望对这个问题有一个相对简单的解决方案。

提前致谢。

models.py

class Bout(models.Model):    
boutid = models.AutoField(db_column=u'BoutID', primary_key=True) 
sessionid = models.ForeignKey(Session, db_column=u'SessionID', verbose_name=u'Session') 
activitytypeid = models.ForeignKey(Activitytype, db_column=u'ActivityTypeID', verbose_name=u'Activity Type') 
locationid = models.ForeignKey(Location, db_column=u'LocationID',verbose_name=u'Location') 
equipment = models.ManyToManyField(Equipment, verbose_name=u'Related Equipment', related_name=u'Bout_Equipment', blank=True, null=True) #through = 'BoutEquipment'
intensitymetrics = models.ManyToManyField(Intensitymetric, verbose_name=u'Related Intensity Metrics', related_name=u'Bout_IntensityMetrics', blank=True, null=True) #through = 'BoutIntensitymetric'

def __unicode__(self):
    return u'%s %s' % (self.sessionid, self.activitytypeid)

class Meta:
    db_table = u'app_bout'
    verbose_name = u'Bout'
    verbose_name_plural = u'Bouts'

class Equipment(models.Model):
equipmentid = models.AutoField(db_column=u'EquipmentID', primary_key=True) 
name = models.CharField("Name", max_length=100, db_column=u'Name')
equipmenttypeid = models.ForeignKey(Equipmenttype, db_column=u'EquipmentTypeID', verbose_name = u'Equipment Type') 

def __unicode__(self):
    return self.name

class Meta:
    db_table = u'app_equipment'
    verbose_name = u'Equipment'
    verbose_name_plural = u'Equipment'

class BoutEquipment(models.Model):
id = models.AutoField(db_column=u'id', primary_key=True)
boutid = models.ForeignKey(Bout, db_column=u'Bout_ID') 
equipmentid = models.ForeignKey(Equipment, db_column=u'Equipment_ID') 

def __unicode__(self):
    return self.name

class Meta:
    db_table = u'app_bout_equipments'

admin.py

class EquipmentAdmin(admin.ModelAdmin):
form = EquipmentForm
inlines = [EquipmentShoeInline, EquipmentLogInline]
list_display = ('name','equipmenttypeid','manufacturer','retired','retiredby','retiredon','notes')
fields = (
          'name',
          ('equipmenttypeid','manufacturer'),
          ('retired','retiredby','retiredon'),
          'notes'
          )
class BoutAdmin(admin.ModelAdmin):
form = BoutForm
filter_horizontal = ('equipment','intensitymetrics',)
list_display = ('sessionid','activitytypeid','locationid','sequence','activehand','baddata')
inlines = [BoutDeviceInline,]
fields = (
          ('sessionid','locationid','activitytypeid'),
          'videofilelocation',
          'sequence',
          'activehand',
          'notes',
          'baddata',
          ('equipment','intensitymetrics')
          )

1 个答案:

答案 0 :(得分:0)

django中的manytomany字段是您希望彼此连接的两个模型之间的连接表。

这在SQL级别上发生,因此两个模型都必须存在于数据库中。

bout = Bout()
...

equipment = Equipment()
...

bout.equipment.add(equipment)
#fails because bout and equipment are not saved

bout.save()
bout.equipment.add(equipment)
#fails because equipment is not saved

equipment.save()
bout.equipment.add(equipment)
#yay :)