I would like to ask some advice on Modeling a specific model behavior. Basically I have a model Item. It describes the name and description of an item.
I have a inventory, which should hold a "List" of items, considering the quantity of each item should be specified in the inventory.
Each User
should have one unique inventory.
Here's what I'm trying to do:
class User(models.Model):
name = models.CharField(max_length=100)
invetory =models.ForeignKey(inventory,on_delete=models.CASCADE)
class item(models.Model):
name =models.CharField(max_length=40)
description = models.TextField(max_length=200)
value = models.FloatField()
class inventory(models.Model):
?
I'm not sure if this is the right approach.
You should use many-to-many relations. First of all you should delete the FK from the User model. Then create a separate model for items and finally link many users to many items (one user can handle multiple items and one item can belong to multiple users). Something like that:
class User(models.Model):
name = models.CharField(max_length=100)
class item(models.Model):
name =models.CharField(max_length=40)
class inventory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
item = models.ForeignKey(item, on_delete=models.DO_NOTHING)
quantity = models.FloatField()
PS Class names should use PascalCase convention https://www.python.org/dev/peps/pep-0008/?#class-names