Alright, bad example. 

Lets try LEGO pieces. I have a LEGO set that has many pieces (ManyToMany). 
But I have 4 wheels and 1 brick in that set. The 'brick' can be added with 
no problem, but how do I save the information that there's 4 'wheel' 
objects and not only one?

W dniu czwartek, 5 lipca 2012 19:40:06 UTC+2 użytkownik Tomas Neme napisał:
>
> > But! I do have another problem. Lets ditch our football example. Let's 
> say
> > that I have something like that:
> >
> > class CherryTree(models.Model):
> >     name = models.IntegerField()
> >     cherries = models.ManyToManyField('CherryFruit')
> >
> > class CherryFruit(models.Model):
> >     name = models.CharField(max_length=50)
> >
> > More or less. The point is, I want to ass many CherryFruit to one
> > CherryTree. How do I pass the additional information? I suspect this can
> > have something to do with the 'through' argument, but I'm probably
> > completely wrong. Or do I have to create additional Model, like:
> >
> > class CherriesOnTree(models.Model):
> >     name = models.ForeignKey('CherryFruit')
> >     tree = models.ForeignKey('CherryTree')
> >     amount = models.IntegerField()
> >
> > and add each fruit separately? Hope It's clear what I mean :).
>
> well, no, no and no, but interesting anyways.
>
> A tree and it's fruit is the same as a team and it's players.
>
> You'd do
>
> class CherryTree(models.Model):
>     name = models.IntegerField()
>
> class CherryFruit(models.Model):
>     tree = models.ForeignKey(CherryTree)
>     name = models.CharField(max_length=50)
>
> because each cherry belongs to a single tree. You can think of it as a 
> parent-child relationship, the children are the ones that have the 
> ForeignKey.
>
> The "through" parameter in a many to many just defines the name of the 
> table to be used for that "CherriesOnTree" model you put there, which you 
> wouldn't need to define. Also, the "amount" field would be of no point, 
> would it?
>
> then you would do this:
>
> # create a new tree
> my_tree = Tree("Tree on the corner")
> # or maybe get a tree from the database
> my_tree = Tree.objects.get(name="Tree on the corner")
>
> # create a new cherry for this tree
> cherry = Cherry(tree=my_tree, name="Some Cherry Name")
> cherry.save()
> ## or if you don't need to do anything afterwards, you can simply
> Cherry(tree=my_tree, name="Some Cherry Name").save()
>
> a many-to-many would be used, for example, in a classes and students 
> scenario
>
> Each student goes to many classes, each class has many students, so:
>
> class Class(models.Model):
>    name = models.CharField(max_length=50)
>
> class Student(models.Model):
>    name = models.CharField(max_length=50)
>    classes = models.ManyToManyField(Class)
>
> # create class
> math = Class(name="Math")
> math.save()
>
> # create student
> student = Student(name="Charles")
> student.save() # need to save before setting up many to manies
> # Charles goes to Math class
> student.classes.add(math)
>
> Here an extra table will be created to hold the many-to-many 
> relationships. I think it's called <appname>_class_m2m_student, or 
> something like that.
>
> The through class is used if you want to add some extra data, like for 
> example, where does a student seat in a particular class, or what other 
> students he's in groups with in there, see
>
>
> https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany
>
> for more details
>
> -- 
> "The whole of Japan is pure invention. There is no such country, there are 
> no such people" --Oscar Wilde
>
> |_|0|_|
> |_|_|0|
> |0|0|0|
>
> (\__/)
> (='.'=)This is Bunny. Copy and paste bunny 
> (")_(") to help him gain world domination.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/5h8HZPc1lAUJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to