[Answer]-Django form with dynamic fields and manytomany relationschips


For problem 1, this makes sense because if you create a model which is refered to by other models, said model key must exist. What I’d suggest you do is look into saving multiple models with a transaction.

2 is pretty easy, just use jQuery/Javascript to show/hide the appropriate fields in the browser based on the user’s selection event.

Based on your comment, here’s an example of how I handle data to and from the server

//Submit data to server, assuming you have already extracted out the relevant data values
    url : "someUURLLocation/",
    type : "POST",
    data : {"data" : JSON.stringify({"field1" : var1, "field2" :var2}),
    dataType : "json",
    success : function(results){
      if (results.success == "true") {
        //handle DOM insertion and other stuff
      } else 


from django.conf.urls import patterns, url
from $APPNAME import views

urlpatterns = patterns("",
  url(r'^someURLLocation/$', views.handleDataSubmission),


from django.http import HttpResponse
from django.utils import simplejson

def handleDataSubmission(request):
  data = simplejson.loads(request.POST.get("data", None))

  if data is not None:

    newPost = Post.objects.create( field1 = data["field1"], field2 = data["field2"])
    dataReturn  = [{"val1" : newPost.title, "val2" : newPost.date}]

    return HttpResponse(simplejson.dumps({"success" : "true", "data" : dataReturn}), mimetype = "application/json")

    return HttpResponse(simplejson.dumps({"success" : "false", "message" : "Invalid data received by server"}), mimetype = "application/json")

Leave a comment