Adding a ManyToMany field to existing model

101 views
Skip to first unread message

jjgod

unread,
Apr 2, 2009, 1:02:28 PM4/2/09
to Django users
Hi,

I'm running Django version 1.1 beta 1 SVN-10368.

I'm having a problem trying to a ManyToMany field to an existing
model.

In an application called "videos", I created a models.py like this:

from django.db import models

class Speaker(models.Model):
name = models.CharField(max_length=200)
localized_name = models.CharField(max_length=200)
bio = models.TextField()

class Video(models.Model):
title = models.CharField(max_length=200)
localized_title = models.CharField(max_length=200)
description = models.TextField()
filmed_on = models.DateField('date filmed')
video_url = models.URLField(verify_exists=False)
subtitle_url = models.URLField(verify_exists=False)

then I add

speakers = models.ManyToManyField("Speaker")

to the end of Video class.

when I run "python manage.py sql videos", it returns:

BEGIN;
CREATE TABLE "videos_speaker" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(200) NOT NULL,
"localized_name" varchar(200) NOT NULL,
"bio" text NOT NULL
)
;
CREATE TABLE "videos_video" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(200) NOT NULL,
"localized_title" varchar(200) NOT NULL,
"description" text NOT NULL,
"filmed_on" date NOT NULL,
"video_url" varchar(200) NOT NULL,
"subtitle_url" varchar(200) NOT NULL
)
;
CREATE TABLE "videos_video_speakers" (
"id" integer NOT NULL PRIMARY KEY,
"video_id" integer NOT NULL REFERENCES "videos_video" ("id"),
"speaker_id" integer NOT NULL REFERENCES "videos_speaker" ("id"),
UNIQUE ("video_id", "speaker_id")
)
;
COMMIT;

which seems to be fine for me. Then I run "python manage.py syncdb",
yet it does not create any new table for me, and when I try to look
into the database generated with sqlite3 <dbname>, it shows:

sqlite> .tables
auth_group django_admin_log
auth_group_permissions django_content_type
auth_message django_session
auth_permission django_site
auth_user videos_speaker
auth_user_groups videos_video
auth_user_user_permissions

which apparently does not have the last table described in "python
manage.py sql videos". Then I try to add a video object in admin page,
it gives me error page like this:

OperationalError at /admin/videos/video/1/
no such table: videos_video_speakers

I'm fairly new to Django, so I really wish to know if I missed
something. Thanks in advance.

Joey Gartin

unread,
Apr 2, 2009, 2:07:28 PM4/2/09
to django...@googlegroups.com
gzjjgod
 
I am new also, but I noticed right away that you had quotes around Speaker
 
speakers = models.ManyToManyField("Speaker")
That may be the problem.  I have added manytomany fields after the fact and had no problems.
 
Good luck!

jjgod

unread,
Apr 2, 2009, 9:14:31 PM4/2/09
to Django users
Hi Joey,

On 4月3日, 上午2时07分, Joey Gartin <joeygar...@gmail.com> wrote:
> I am new also, but I noticed right away that you had quotes around Speaker
>
> speakers = models.ManyToManyField("Speaker")
> That may be the problem. I have added manytomany fields after the fact and
> had no problems.

Thanks, but I did try changing "Speaker" back to Speaker, yet nothing
happened
after I syncdb again.

- Jiang

soniiic

unread,
Apr 3, 2009, 5:20:33 AM4/3/09
to Django users
You need to delete the model's tables from the database and then do
syncdb. Else you could use some of the django plugins that senses the
difference between the model and the existing database and then it
changes the database for you. google for django-extensions
Reply all
Reply to author
Forward
0 new messages