ManytoManyField (through) not showing up !

21 views
Skip to first unread message

willy Hakizimana

unread,
Mar 28, 2014, 11:29:21 AM3/28/14
to django...@googlegroups.com
I am trying to do a ManytoManyField through a middle Table. After I run syncdb, I do not see the column in pdadmin.
What am I doing wrong?



Here is my code

class Countries(models.Model):

    country_name = models.CharField(max_length=200)
    country_id = models.CharField(primary_key=True, max_length=2)
    region = models.CharField(max_length=200)

    #Economic profile
    gdp = models.DecimalField(max_digits=17, decimal_places=2, default=0.00)
    gdp_growth = models.DecimalField(max_digits=5, decimal_places=2, default=0.00)

class Products(models.Model):

    product_id = models.AutoField(primary_key=True)
    hs_number = models.ManyToManyField(Countries, through='Imports')
    product_descript = models.CharField(max_length=250)

    class Meta:
        db_table = "Products"

    def __unicode__(self):
        return self.hs_number


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)

C. Kirby

unread,
Mar 28, 2014, 11:37:54 AM3/28/14
to django...@googlegroups.com
Did you add this field after you had already run syncdb? If so, you won't see it running syncdb again, that command only adds new models, it does not pick up modifications to models.

You have a couple of options.
1) If you are in development, you can just drop your existing database and use syncdb to recreate it with the new fields.
2a) If you are using django < 1.7, you can use south to handle database migrations
2b) If using django 1.7, use the built in migrations (which is a reimplementation of south in django core
Reply all
Reply to author
Forward
0 new messages