Skip to content Skip to sidebar Skip to footer

Many To One Relation In Django With Existing Objects

I've started to study Django yesterday and I'm having some trouble with to creating a Many to One Relation based on objects I've previously created. models.py # -*- coding: utf-8 -

Solution 1:

Your relationship is going the wrong way. If a Product can have multiple Items, make a ForeignKey from Item to Product. With the ForeignKey on Product, you're saying each product has exactly one Item, but each Item can belong to multiple Products.

The docs have more details and examples: https://docs.djangoproject.com/en/1.7/topics/db/examples/many_to_one/

Post a Comment for "Many To One Relation In Django With Existing Objects"