views:

17

answers:

2

I have 2 models and i want ,any instances on class 1 in class 2 how do i do it in django models ...and i tried using manytomany field but the problem is when i add a new object it will have that manytomany field already there (which i dont want ) ...any idea how do i do this ...

+1  A: 

A one to many relation is just a ForeignKey in the other direction.

Ignacio Vazquez-Abrams
but the problem is that i want to have multiple instances of class 1 in class 2....its not possible with foreignkey!!..
ramsin
Yes it is. Make a `ForeignKey` from `Model1` to `Model2`. Model2 gets an attribute called `model1_set` which is a manager.
Ignacio Vazquez-Abrams
can u please give a example i am confused ....i have class 1name = models.Char.....class 2mod1 = models.Foregign...........here i want multiple instances of mod1... how do i do it
ramsin
The other way around, like I said in my answer.
Ignacio Vazquez-Abrams
but how do i put it as i want ... is there any way ....
ramsin
Do you really believe that I'm hiding something from you?
Ignacio Vazquez-Abrams
funny ..... but can u give me a example for that ....
ramsin
class 1 otherclass = ForeignKey(class 2) ...
Ignacio Vazquez-Abrams
the problem that i am facing is ... i am not understanding how i refer a foreignkey in reverse direction
ramsin
It's all in the docs: http://docs.djangoproject.com/en/dev/ref/models/relations/#ref-models-relations
Ignacio Vazquez-Abrams
thanks a ton...... i got it..... i am very new to this trying to learn ... thank u dude ..
ramsin
A: 

One reason you may be having problems is relationships are somewhat backwards in relational databases from what they are in objects. It sounds like you want one instance of class2 to contain a list of class1. The way you do that is to have a ForeignKey on class1 that references class2. If you're not familiar with relational databases it'll seem like class2 should reference class1, but in the relational world, when you have a One-To-Many, the many need to reference the single.

For example say you have a folder structure. The "normal" way you'd think about it is that you have a folder, and that folder has some files in it. But in a relational database, the folder doesn't have a list of files. In fact nothing can (directly) have a list of anything else. What you do is then each file would know the id of the folder they're in. Now when you have a folder and you want to find the files that are in it, what you do is look at all the files and find the ones that have that folder as it's container.

Davy8
It sounds like you want one instance of class2 to contain a list of class1 --- exactly that is what i want to do.... but every time it has to be fresh additions ...by default the existing objects are shown up when i tried using manytomany realtion
ramsin
Use ForeignKey like Ignacio said. The problem is you're trying to put ForeignKey on Class2. That's incorrect. ForeignKey needs to be on class1 which points to a single class2. That will give you a one-to-many between class2 and class1, class1 contains exactly 1 class2, and class2 will contain (indirectly) many class1's. Django takes care of that magic for you and will automatically create a list of class1's on class2, but realize that that doesn't actually exist as a list in the database. A list of something for 1 database row is impossible. When a ManyToMany is needed a 3rd table is made
Davy8