ManyToMany quick question

71 views
Skip to first unread message

willyhakim

unread,
Mar 31, 2014, 6:07:05 PM3/31/14
to django...@googlegroups.com
when creating your models, how do you know which field to assign the ManyToManyField ?

Mike Dewhirst

unread,
Mar 31, 2014, 7:45:08 PM3/31/14
to django...@googlegroups.com
On 1/04/2014 9:07 AM, willyhakim wrote:
> when creating your models, how do you know which field to assign the
> ManyToManyField ?

https://docs.djangoproject.com/en/1.6/topics/db/models/#many-to-many-relationships

>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/08b9538b-a98f-4768-ad0d-f8f6b8419440%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/08b9538b-a98f-4768-ad0d-f8f6b8419440%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

willyhakim

unread,
Mar 31, 2014, 9:38:48 PM3/31/14
to django...@googlegroups.com


So sorry, I should have been clear. I meant when you are assigning a field ManyToManyField (through) properties

 

Camilo Torres

unread,
Mar 31, 2014, 11:34:44 PM3/31/14
to django...@googlegroups.com
On Monday, March 31, 2014 5:37:05 PM UTC-4:30, willyhakim wrote:
when creating your models, how do you know which field to assign the ManyToManyField ?

 from django.db import models
    

class Student(models.Model):
    name = models.TextField()


class Course(models.Model):
    name = models.TextField()
    students = models.ManyToManyField(Student, related_name='courses')

>>> from testapp import models
>>> a = models.Student(name='aaaaa')
>>> a.save()
>>> b = models.Student(name='bbbbb')
>>> b.save()
>>> c = models.Course(name='cccccccc')
>>> c.save()
>>> c.students.add(a)
>>> c.students.add(b)
>>> c.students.all()
[<Student: Student object>, <Student: Student object>]

>>> a = models.Student.objects.get(name='aaaaa')
>>> a.name
'aaaaa'
>>> a.courses.all()
[<Course: Course object>]


Is this what you are asking?

willyhakim

unread,
Apr 1, 2014, 6:21:18 AM4/1/14
to django...@googlegroups.com
Below is my models.py
My problem is when I syncdb (I have dropped the db couple times to start over)
the country_name field with m2m never shows in the db. what am I doing wrong? I am using postgres

class Products(models.Model):

    hs_number = models.CharField(primary_key=True, blank=True, max_length=4)
    product_descript = models.CharField(max_length=250)

    class Meta:
        db_table = "Products"

    def __unicode__(self):
        return self.hs_number


class Countries(models.Model):
    country_id = models.CharField(primary_key=True, max_length=2, blank=True)
    country_name = models.ManyToManyField(Products, through="Imports", blank=True)
    region = models.CharField(max_length=200)

class Imports(models.Model):
    hs_number = models.ForeignKey(Products)
    country_id = models.ForeignKey(Countries)
    imported_value2008 = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)
    imported_value2009 = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)

antialiasis

unread,
Apr 1, 2014, 8:19:49 AM4/1/14
to django...@googlegroups.com
I don't know why you're calling a ManyToManyField to Products a country name, but you shouldn't get a country_name field - the Imports table will contain all the data needed to associate countries with products.

What exactly do you want to be stored in the country_name field on the Countries model? If you want it to be the name of the country, it should be a separate field from the ManyToManyField altogether.

willyhakim

unread,
Apr 1, 2014, 8:52:10 AM4/1/14
to django...@googlegroups.com


I think I was missing the concept all together. So the m2m field is just there to show the relationship but does not take any data. That is why when I look in pgadmin, I do not see it. 
So sorry, I was missing the concept all together.

Camilo Torres

unread,
Apr 1, 2014, 11:54:32 AM4/1/14
to django...@googlegroups.com
Hello,

By using default table names and default (or automatic) many to many field management, you get a join table like countries_products, but in your case, you are doing the many to many field management through the Imports model, so in the DB you should see data in the imports table for each relation between Countries and Products.

When doing many to many in a relational model, as in a RDBMS or SQL database, that relation must be modeled by using a third table. This is in a normalized schema. So, you will not see a join field, but a join table. Note: in a non-normalized schema it is possible to have a field for a many to many relation, but with data duplication, not the case of the Django ORM.

So in your model you will never see a country_name field. Think of this: if the country table had a country_name field, you can only put one value in that field for every row, so only one value for any Django model in the ORM, and this is not many to many, but one to many.

In your case, the relation is in the Imports model and hence in the imports database table.

Regards,
Camilo

willyhakim

unread,
Apr 1, 2014, 1:36:37 PM4/1/14
to django...@googlegroups.com

Camillo, YOU'RE DA MAN! Thank you so much? One day, at djangocon, I will buy you a beer
Reply all
Reply to author
Forward
0 new messages