I have a three models like this:
class Document(models.Model):name = models.CharField(max_length=1000, blank=False, null=False)user = models.ForeignKey(User, unique=False)class Name(models.Model):name = models.CharField(max_length=1000, blank=False, null=False)user = models.ForeignKey(User, unique=False)doc = models.ForeignKey(Document, unique=False)class Text(models.Model):name = models.CharField(max_length=1000, blank=False, null=False)text = models.TextField()user = models.ForeignKey(User, unique=False)doc = models.ForeignKey(Document, unique=False)
I am using Formsets to create as many forms of name and Text for one Document. Here is something I have in the template:
<form action="" method="POST">{% csrf_token %}
<h2>Text Items</h2>{{ text_formset.management_form }}{{ name_formset.management_form }}{% for form in text_formset.forms %}<div class="Items">{{ form.as_p }}<p style=""><a class="delete" href="#">Delete</a></p></div>{% endfor %}<h2>name Items</h2>{% for form in name_formset.forms %}<div class="item">{{ form.as_p }}<p style=""><a class="delete" href="#">Delete</a></p></div>{% endfor %}<input type="submit" value=" Submit " /></form>
I am using formsets so the form classes look like:
class DocumentForm (ModelForm):class Meta:model = Documentclass TextForm (ModelForm):class Meta:model = Textclass nameForm (ModelForm):class Meta:model = name
I have to give the users to be able to reorder the elements according to their wish on the front-end side. Something like Wufoo forms where you can drag them. I can use jQuery draggable etc. to achieve that but
Approaches that I have thought are building, modifying forms on the client side by having the view return the JSON object but I am not sure how well Django will play with that. I want to be able to use the CSRF security facility.
I tried to combine the formset objects but could not sort the formsets. In my view I have
text_formset = TextFormSet(prefix="doc_text", initial=text_initial_data)name_formset = NameFormSet(prefix="doc_name", initial=name_initial_data)complete_formset = list(text_formset) + list(name_formset)
Sorting:
def doc_edit_sorter(left, right):return cmp(left.WHATHERE, right.WHATEHERE)