Monday, September 23, 2013

ManyToMany intermediary models with no required extra fields

Say you have a model with a ManyToManyField to another model:

class Band(models.Model):
    ...
    members = models.ManyToManyField('Person')

And now, say you decide that you want to use a custom intermediary model for the relationship, without any required extra fields. This could happen if, for instance, you would like to register a signal on the intermediary model, or if the only extra information you would like to add to the intermediary model is a DateTimeField with auto_now_add=True, or if the only extra fields you add have a sensible default that you're usually not going to want to mess with when you add a member to a band. This seems like a fairly simple, commonplace use case.

That simple use case, however, creates extremely tiresome complications stemming from Django's thinking there are some fields on the intermediary model that you're going to want to set, and therefore removing your ability to use the ManyRelatedManager's add or remove methods, a ModelForm's save_m2m method, etc. etc. etc. If only Django would stop thinking it knows better than you whether you need to set extra fields on the relationship and do exactly the same thing it does when the intermediary model is auto-created, everything would work swimmingly; however, because it doesn't, a bunch of code needs to be changed.

I've browsed around the Internet enough every time I've run into this problem that I believe I know there is no actual solution other than sucking it up, so that's not my question. My question is whether there are any plans to make Django a bit smarter and less overzealous about deciding you need to set extra information on the relationship, or whether I should make a ticket requesting this.

--
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+unsubscribe@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.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment