Reputation: 3802
I have the following problem with Django.
class UserProfile(Model):
inventory = models.M2M(InventoryItem)
class InventoryItem(Model):
item = GenericForeignKey()
class Equipment(Model):
base = GenericForeignKey()
Every user can have many items. Inventory item can point to equipment, materials and so on, but in this case it points to Equipment model. Equipment model has a relation to either Weapon or Armour or Accessory.
I need to remove a specific item from user's inventory.
UserProfile.objects.get(pk=1).inventory.objects.all()[0].delete()
** This also deletes equipment and weapon/armour/accessory objects related which is not intended. **
I have already added on_delete=DO_NOTHING on all foreign keys, but I do not see such option possible on GenericForeignKeys. What's the solution?
Upvotes: 0
Views: 613
Reputation: 2856
UserProfile.objects.get(pk=1).delete()
it looks like you're deleting the UserProfile, but not the specific item
Upvotes: 1