我遇到的问题是多对多模型的save方法没有插入数据库:
try:
dnpg = Device_Name_Product_Group_XREF.objects.get(device_name = dn, product_group = product_group)
except Device_Name_Product_Group_XREF.DoesNotExist:
dnpg = Device_Name_Product_Group_XREF(device_name=dn, product_group = product_group)
dnpg.save()
# this prints: {'product_group': 1992L, 'device_name': 6481L}
print model_to_dict(dnpg)
上面的代码应该在XREF表中插入一个带有值(1992,6481)的记录,但事实并非如此。以下是我的模型定义:
class Device_Name_Product_Group_XREF(models.Model):
device_name = models.ForeignKey(Device_Name, primary_key=True, to_field = "id", db_column="DEVICE_NAME_ID")
product_group = models.ForeignKey(Product_Group, primary_key=True, to_field = "id", db_column="PRODUCT_GROUP_ID")
class Meta:
db_table = 'ADMIN_DEVICE_NAMES_PRODUCT_GROUP_XREF'
managed = False
有什么想法吗?