4

I'm working on what I think is a pretty standard django site, but am having trouble getting my admin section to display the proper fields.

Here's my models.py:

class Tech(models.Model):
    name = models.CharField(max_length = 30)

class Project(models.Model):
    title = models.CharField(max_length = 50)
    techs = models.ManyToManyField(Tech)

In other words, a Project can have different Tech objects and different tech objects can belong to different Projects (Project X was created with Python and Django, Project Y was C# and SQL Server)

However, the admin site doesn't display any UI for the Tech objects. Here's my admin.py:

class TechInline(admin.TabularInline):
    model = Tech
    extra = 5

class ProjectAdmin(admin.ModelAdmin):
    fields = ['title']
    inlines = []
    list_display = ('title')

admin.site.register(Project, ProjectAdmin)

I've tried adding the TechInline class to the inlines list, but that causes a

<class 'home.projects.models.Tech'> has no ForeignKey to <class 'home.projects.models.Project'>

Error. Also tried adding techs to the fields list, but that gives a

no such table: projects_project_techs

Error. I verified, and there is no projects_project_techs table, but there is a projects_tech one. Did something perhaps get screwed up in my syncdb?

I am using Sqlite as my database if that helps.

John Millikin
  • 197,344
  • 39
  • 212
  • 226
swilliams
  • 48,060
  • 27
  • 100
  • 130

3 Answers3

3

I've tried adding the TechInline class to the inlines list, but that causes a

'TechInLine' not defined

Is that a straight copy-paste? It looks like you just made a typo -- try TechInline instead of TechInLine.

If your syncdb didn't create the proper table, you can do it manually. Execute this command:

python manage.py sqlreset <myapp>

And look for the definition for the projects_project_techs table. Copy and paste it into the client for your database.

John Millikin
  • 197,344
  • 39
  • 212
  • 226
0

Assuming your app is called "projects", the default name for your techs table will be projects_tech and the projects table will be projects_project.

The many-to-many table should be something like projects_project_techs

Andrew Ingram
  • 5,160
  • 2
  • 25
  • 37
0

@John Millikin - Thanks for the sqlreset tip, that put me on the right path. The sqlreset generated code that showed me that the projects_project_techs was never actually created. I ended up just deleting my deb.db database and regenerating it. techs then showed up as it should.

And just as a sidenote, I had to do an admin.site.register(Tech) to be able to create new instances of the class from the Project page too.

I'll probably post another question to see if there is a better way to implement model changes (since I'm pretty sure that is what caused my problem) without wiping the database.

swilliams
  • 48,060
  • 27
  • 100
  • 130
  • The generated SQL code can be copied directly into the database client. Run "sqlite3 ", then select the "create table projects_project_techs..." section, copy it, and paste it into the SQLite prompt. – John Millikin Oct 02 '08 at 18:42
  • To implement model changes you want to use South. – Aaron McMillin Nov 22 '11 at 19:06