2015-04-03 69 views
0

我有一個問題,在許多保存方法很多模型沒有插入數據庫:Django的模型保存()不插入新記錄

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 

任何想法?

+0

你有什麼錯誤嗎?你能展示完整的追溯? – Ymartin 2015-04-03 16:54:51

回答