Thursday, April 4, 2013

Problem with intermediary table from legacy database

Under Django 1.5.1 I'm having a go at using ManyToManyField (see below models.py and admin.py files) referring to an Sqlite3 legacy db. 

I'm receiving the following fatal error message in admin

Exception Value:
'LibroOption.fields' can't include the ManyToManyField field 
'autore' because 'autore' manually specifies a 'through' model

(I've also tried to let Django to automagically define the intermediary table eliminating from models.py the through option, and it works like a charm: that is, when as admin  I open the Libri model I see the autore field horizontally filtered.)

How can I solve this problem having to necessarily use this legacy db and its tables which are fed by a Filemaker procedure too?

Thanks from Rome
Vittorio

========================
models.py
from django.db import models

# Create your models here.
class Autore(models.Model):
   nome = models.CharField(db_index=True,max_length=50) 
   cognome = models.CharField(db_index=True,max_length=50)
   def __unicode__(self):
       return u"%s %s" % (self.nome, self.cognome)
   class Meta:
        verbose_name_plural = "Autori" 
        db_table="Autori"
        ordering=['cognome', 'nome']
...................
...................
class Libro(models.Model):
   titolo = models.CharField(db_index=True,max_length=200) 
   autore = models.ManyToManyField(Autore,through='LibriAutori')
   def __unicode__(self):
      return self.titolo
   class Meta:
        verbose_name_plural = "Libri"
        db_table="Libri"
        ordering=['titolo']

class LibriAutori(models.Model):
   libro = models.ForeignKey(Libro,db_column='libro')
   autori = models.ForeignKey(Autore,db_column='autori') 
   class Meta:
        verbose_name_plural = "LibriAutori"
   db_table='LibriAutori'
=====================================
admin.py
from django.contrib import admin
from biblio.models import *
from django import forms
#

class LibroOption(admin.ModelAdmin):
    list_display = ('titolo','autore')
    fields=(('titolo'),'autore')
    filter_horizontal = ['autore', ]
    order_by= ['titolo', ]


admin.site.register(Autore)
admin.site.register(Libro, LibroOption)
admin.site.register(LibriAutori)

No comments:

Post a Comment