Extending the ManyToMany relation, maybe a bad idea

41 views
Skip to first unread message

Joshua Russo

unread,
Jul 25, 2011, 12:08:26 AM7/25/11
to django...@googlegroups.com
Ok, so I've created a structure of selectable properties, that for the most part is a straight forward many-to-many relationship. What I've added is a character field in the relationship table that should conditionally display based on the setup of the property being selected.

What I wanted to do was create a complex CheckboxSelectMany widget and supporting form and model fields, but once I got into the saving, deletion, and retrieval in the related field model fields, it looked like too much of a rewrite.

My current thought is to do each checkbox / conditional field combination as an individual form. Though I'm not sure if this is the best way to go about it either, because I don't see any real automated way to do this; short of retrieving all of the checkbox options and creating the form instances myself. I'm not opposed to this, I just want to make sure I'm not re-inventing the wheel.

Any suggestions?

Shawn Milochik

unread,
Jul 25, 2011, 6:08:57 AM7/25/11
to django...@googlegroups.com
Have you looked into creating your own connector table for your
ManyToMany using the 'through' keyword?

https://docs.djangoproject.com/en/1.3/topics/db/models/#many-to-many-relationships


Joshua Russo

unread,
Jul 27, 2011, 7:49:42 PM7/27/11
to django...@googlegroups.com
I am using the through keyword. I'm just trying to figure out the most efficient way to create a form field and widget to update the data.

What I'm struggling with is how a ManyToMany field with an intermediate table containing extra fields fits into the ModelForm saving. I'm just not sure where to save the extra field data, how best to collect it in the form, or how I would handle re-retrieving of the extra fields. The only  thing I've created so far is an extension of the ModelMultipleChoice field that creates a custom choices structure and an accompanying widget that extends CheckboxSelectMultiple. This will display the structure, but what's the best practice here for saving and re-displaying?


Joshua Russo

unread,
Jul 27, 2011, 8:25:45 PM7/27/11
to django...@googlegroups.com
Someone tell me where I'm going wrong conceptually here, I feel like I'm making this too hard.

Shawn Milochik

unread,
Jul 27, 2011, 8:33:21 PM7/27/11
to django...@googlegroups.com
You can override the ModelForm.

Add the fields to the ModelForm. Set the initial values by overriding
__init__, and save them by overriding the save() method.

Should be pretty simple.

Joshua Russo

unread,
Jul 27, 2011, 9:00:49 PM7/27/11
to django...@googlegroups.com
My problem is that I have multiple values and I need to associate the list of extra fields with selected relations for the ManyToMany relation. Below is a simplified version of what my setup is. 

I want to create an edit form for Organization, but I'm struggling with how best to save (and re-retrieve for display) the descr field in OrganizationItem. I don't see how adding fields to the OrganizationForm will accomplish this.

# Models

class Organization(models.Model):
    name      = models.CharField("Organization's name", max_length=100)
    items     = models.ManyToManyField(ListItem, through='OrganizationItem')

class OrganizationItem(models.Model):
    organization = models.ForeignKey(Organization)
    item       = models.ForeignKey(ListItem)
    descr      = models.TextField("Description", blank=True)

class ListItem(models.Model):
    name     = models.CharField("Name", max_length=200)
    order    = models.IntegerField("Order")
    extraInfo     = models.BooleanField("Requires extra information")
    multiLineInfo = models.BooleanField("The display should use a multi-line edit box instead of a single line edit")
    descrLabel    = models.CharField("Extra information label", max_length=50,
        blank=True)

# ModelForm

class OrganizationForm(ModelForm):    
    class Meta:
        model = Organization


Joshua Russo

unread,
Jul 27, 2011, 10:37:10 PM7/27/11
to django...@googlegroups.com
Ok, so I can retrieve this extra data in the __init__ of the model form, but it seems like I will need to skip all of the cleaning and automatic validation of the form; because the data, to be displayed with the associated checkbox will have to be included with the items field in the OrganizationForm and fields and form names need to have a one to one relationship. 

I keep thinking I'm doing something wrong here. From the lack of responses I feel like there is no real good way to handle a group of options with additional relational data.

Tom Evans

unread,
Jul 28, 2011, 5:43:12 AM7/28/11
to django...@googlegroups.com
On Thu, Jul 28, 2011 at 3:37 AM, Joshua Russo <josh.r...@gmail.com> wrote:
> I keep thinking I'm doing something wrong here. From the lack of responses I
> feel like there is no real good way to handle a group of options with
> additional relational data.

Or alternatively, no-one understood what you were trying to do and
ignored your post until you made enough sense. Like now.

How you proceed depends upon how much customization you need to do.
This situation is calling out for either a model formset on
OrganizationItem, or an inline formset. There is an excellent blog
post on inline formsets here[1].

Cheers

Tom

[1] http://charlesleifer.com/blog/djangos-inlineformsetfactory-and-you/

Joshua Russo

unread,
Jul 28, 2011, 8:47:16 AM7/28/11
to django...@googlegroups.com
Ya, sorry, I was just getting frustrated because I was taking longer than I wanted to implement this. The formset is what I ended up with. The tricky part is still that I want a defined list of options in the formset, so I can't just make it a model formset. I haven't worked in Django in a while and it just takes a little bit to get my mind around what the options really are.
Reply all
Reply to author
Forward
0 new messages