ManyToMany through parameter bug workaround.

244 views
Skip to first unread message

Nicolas Emiliani

unread,
Aug 18, 2012, 12:30:06 PM8/18/12
to django...@googlegroups.com
Hi,

I was trying to create an m2m relationship using the 'through' parameter  because
I want to have an additional field in the relation table. The idea was to render this
using CheckboxSelectMultiple.

The thing is that when I add the model m2m field to the 'fieldset' field at the 
ModelForm it throws an exception :

'HomeAdmin.fieldsets[4][1]['fields']' can't include the ManyToManyField field 'attributes' because 'attributes' manually specifies a 'through' model.

Apparently this is a BUG pointed out here :


Basically if you use the 'through' parameter to use a relation table that contains an extra field/s
you can't render it using a CheckboxSelectMultple widget, not even if the extra field allows null
values.

I thought of fixing it this way (a nasty hack) :

class A(models.Model):
      .
      .
      foos = models.ManyToManyField('Foo')

class Foo(models.Model):
      name = CharField

And here comes the nasssty

class FooRelationshipValue(models.Model):
      a   =  ForeingKey(A)
      foo = ForeingKey(Foo) 
      value = DateField

Here I can store the value for the relationship when saving the Model in the DB.

If anyone has a better solution, suggestion, comment or thinks this is going 
to make me rot in hell please let me know.

Thanks.

--
Nicolas Emiliani

Lo unico instantaneo en la vida es el cafe, y es bien feo.

Nicolas Emiliani

unread,
Aug 18, 2012, 4:17:07 PM8/18/12
to django...@googlegroups.com

Here I can store the value for the relationship when saving the Model in the DB.

If anyone has a better solution, suggestion, comment or thinks this is going 
to make me rot in hell please let me know.

Well, I kept on reading and I found a clean solution :

It's explained here, the same happened to this guy and a workaround is provided.
The code in  MHCs response example has a few errors, but the idea is pretty clear.


Hope it saves some headaches.

 
-- 
Nicolas Emiliani

Lo unico instantaneo en la vida es el cafe, y es bien feo.
Reply all
Reply to author
Forward
0 new messages