문제

I have a field with M2M relationship.when running syncdb,the field with M2M field does not form on the database. here is the model

class Eventgroups(models.Model):
    event=models.ManyToManyField(Event)
    group_name=models.CharField(max_length=100)

    def __unicode__(self):
        return "%s,  %s" \
            % (self.group_name, self.event)
    class Meta:
        db_table= 'eventgroup'
        verbose_name_plural='eventgroups'

events field is not created on database I would appreciate an insight to this problem please Regards, Joshua

올바른 솔루션이 없습니다

다른 팁

Everything is ok. You just don't understand how ManyToMany is realized on the SQL level. Simply speaking, this

class Foo(models.Model):
    #...

class Bar(models.Model):
   foo = models.ManyToManyField(Foo)

is technically the same as this:

class Foo(models.Model):
    #...

class Bar(models.Model):
    #...

class BarFoo(models.Model):
    foo = models.ForeignKey(Foo)
    bar = models.ForeignKey(Bar)

And if you'll check your database - you will find a table called EventgroupsEvent or something like that which contains the actual fields. It's just the ORM that lets you connect the models directly. Please read my answer here for a detailed explanation on how ManyToMany works in the background

It wont create a field named event instead, it will create a new table Eventgroups_event

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top