Reputation: 6458
I have a problem that the save method on a many to many model is not inserting into the database:
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)
The code above should insert a record into the XREF table with values (1992,6481), but it does not. Here are my model definitions:
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
Any ideas?
Upvotes: 1
Views: 840
Reputation: 655
You can use get_or_create instead of try
/except
construction.
Upvotes: 1