On Thu, Apr 4, 2013 at 5:28 PM, Vittorio <ml-...@de-martino.it> wrote:
> 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 > > The error is telling you exactly what the problem is. You can't put autore in the list of admin-displayed fields (or, for that matter, the list of filter_horizontal) because it uses a through model. There's a very simple reason for this - a through model exists to allow you to specify additional data on a relation. The basic m2m widget (in any of it's forms, including raw_id_field and filter_horizontal/vertical) only allow you to define the "other" object that you want a relation with. You can't set up a 'through' relation without specifying the extra data as well, so you're prohibited from using the basic m2m widgets on m2m relations with a through model. If you want to have an admin representation for an m2m relation with a through model, you need to add an inline to represent the through model. See [1] for more details. That said, if I'm reading your models correctly, it looks like the "through" model isn't actually required, it's just been introspected that way -- it's easier for the introspector to create a model for *every* table, rather than try to work out which tables are actually m2m tables. It should be possible to remove the explicit definition of the LibriAutori model, and replace the autori m2m relation with: autori = models.ForeignKey(Autore,db_column='autori', db_table='LibriAutori') That is - explicitly provide the name of the table you want to use as an m2m relation. See [2] for more details. [1] https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-models [2] https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.db_table Yours, Russ Magee %-) > (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) > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.