Friday, December 28, 2012

Building a form to activate/deactivate m2m relationships, rather than add/delete.

Hello everyone,
I'm new to Django and struggling a bit to build a form UI to manage a m2m relationship the way I would like. The stock inline formset technique gives me the functionality I need, of course, but I was hoping to be able to build a form that allows the user to "activate/deactivate" relationships rather than "add/delete" them. Let me explain:

I want to be able to manage customers' subscription preferences to various types of customer service communications. These entities are embodied in models called "customer" and "communication." The many-to-many relationship between them is encapsulated in an intermediary class called "subscription" which also reflects the customer's preferred medium (e.g., email, phone, etc.) for receiving the given communication. 

So my model looks something like this: 

class Customer (models.Model):      ...  class Subscription (models.Model):      medium          = models.CharField(max_length=50)       campaign   = models.ForeignKey(Communication)
customer = models.ForeignKey(Customer) class Communication (models.Model): name = models.CharField(max_length=100)

As I've said, I've been able to achieve a basic level of functionality with an off-the-rack inline formset. This gives me a form that lists all the subscribed communications and the ability to modify those, delete them, or ad new ones. So far so good. 

But what I would really like is a form that displays one sub-form (e.g., one row in a tabular form) for every communication that a customer could subscribe to with the name of that communication, a field for medium, and a checkbox to select whether that subscription was "active" or not. If that box is checked, a subscription will be added to the database (or updated, if it is already there), if it is unchecked, the corresponding subscription will be deleted (if it exists). But regardless, there would be one row for each possible communication whether it was subscribed to or not -- it's just that the active box would be pre-checked and the "medium" field pre-filled in rows that correspond to subscriptions that exist in the databse.

My sense is that I need to create a custom modelform and modelformset to handle this logic, but Im having difficulty figuring out exactly how to do this. I also want to make sure I'm not making this harder than it needs to be. (Django being Django, I sometimes imagine that there could simply a parameter that I could pass to the inlineformset_factory that would do all this for me. Such a thing certainly seems possible.) 

Any ideas?


Thanks a lot,

Paul

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/m-Gn795ClJ4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment