hi. imagine following scenario:

#--------------------------------------------------------------
from django.db import models
from django.forms.models import ModelForm

# ignore this Sample for now. focus on Master and Detail
class Sample(models.Model):
    foo = CharField()

class Master(models.Model):
    field1 = models.CharField()
    field2 = models.CharField()
    samples = models. ManyToManyField(Sample)

class Detail(models.Model):
    attr1 =  models.CharField()
    attr2 =  models.CharField()
    master = models.ForeignKey(Master)

class MasterDetailForm(ModelForm):
    class Meta:
        model = Master /
#--------------------------------------------------------------

I plan to write a dynamic (javascript powered) HTML page, that will
show a form with the Master fields at the top, and will allow the user
to add several Details on demand, by appending elements to the DOM
(with javascript).

I already have something like this for Many-to-Many fields (for the
Sample model, for example): the user presses an Add button, the app
shows him a popup, he selects the option he wants, and by javascript I
add a new [input hidden] field to the DOM with the ID of the option
choosen. when the form is submitted, django transparently handles the
several hidden ID's from the request, and does his magic mapping the
M2M relationship. it works perfectly.

but now, I cannot just use hidden ID's. I need several complete
entities shown on screen.
I need the user to fill them, and submit everything at once.

imagine you have a Sales screen, where the user (salesman) types the
Customer Info (master/header) and also add (javascript magic) the sold
items (details), typing their codes, quantities and prices, and only
at the end he submits the form.

so, the question, after all, is: how should be my HTML/DOM, and how
should I prepare my modelForm so Django would be able to parse the
request and give me a complete Master entity with Detail entities
inside?

I don't need the javascripts (no problems with js), I just need to
know, for example, if an array of the fields of my Detail model would
be the correct approach. for example:

<form method=post ...>
Master fields:
<input type=text name=field1 />
<input type=text name=field2 />

Multiple details:
<input type=text name=attr1 />
<input type=text name=attr2 />
<input type=text name=attr1 />
<input type=text name=attr2 />
<input type=text name=attr1 />
<input type=text name=attr2 />

<input type=submit />
</form>

is there a way for Django to parse such POST and give me a Master
entity with 3 Details inside?

if it is not possible, I will have to get only the Master from the
POST and handle the Details by hand, like this:

if request.method == 'POST':
    form = MasterModelForm(request.POST)
    if form.is_valid():
        master = form.save()
        for i in range(len(request.POST['attr1'])):
            detail = Detail()
            detail.attr1 = request.POST['attr1'][i]
            detail.attr2 = request.POST['attr2'][i]
            detail.master = master
            detail.save()

or something like this.

is there any better ideia?

thanks in advance.

p.s.: as you could see, I don't worry to write the html by hand. I
don't want to use form.as_p() or something. I just need to get the
form from the request, like in form =
MasterDetailModelForm(request.POST). thanks again.

p.s.2: I wonder if a formset could help here.

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

Reply via email to