I have the following model : Objects C, contained by objects B, contained by object A
I a have a dynamic form in a template, and using jQuery functions, I can add dynamically several fields to this form, each group of fields representing Objects (B or C)
When submitted by a post action, I'd like to create my object tree. My objective is to create an object tree when validating this template.
Currently it works, but I guess my solution is really dirty:
- Creating lists using js/jquery, according how many elements I have in my form
- Passing theses lists using $.post as data arguments
- Using the lists in request, creating objects in django data model in a view
I'm quite sure there's a really better solution :p
- Use multiple post to create Objects through different views (distinct for A, B and C) ? (But sync problems..)
- Use json to represent objects directly on js/jquery side ?
- Another solution ?
Many Thanks - edited for clarity
EDIT:
Note : My data model here is different (I'd simplified before) So : Session is former "object A" Exercise is "object B" Repetition is "object C" -Sorry for that, I hope It'll be clear enough (I'll correct in final post)
Some progress here, with your help guys :) I've played a little bit with Form and FormSet. Obviously, its powerfull :)
So now I have the following view:
class RepetitionForm(forms.Form):
weight = forms.CharField()
count = forms.CharField()
def workoutForm(request):
RepetitionFormSet = formset_factory(RepetitionForm, extra=1)
if request.method == 'POST':
repetitionFormSet = RepetitionFormSet(request.POST)
user = get_object_or_404(User, pk=1)
session = Session(date=datetime.date.today(), user=user)
session.save()
exerciseTypeRef = get_object_or_404(ExerciseTypeRef, pk=1)
exercise = Exercise(session = session, exerciseTypeRef = exerciseTypeRef)
exercise.save()
if repetitionFormSet.is_valid():
for repetitionForm in repetitionFormSet.cleaned_data:
if(repetitionForm.is_valid()):
weight = repetitionForm.data['weight']
count = repetitionForm.data['count']
return HttpResponse("ok")
else:
repetitionFormSet = RepetitionFormSet()
return render_to_response('workoutForm.html', {'formSet': repetitionFormSet}, context_instance=RequestContext(request))
Template side is like :
{% csrf_token %}
{{ formSet.management_form }}
<ul id="idUlFormSet">
{% for item in formSet %}
<li>
{{ item }}
</li>
{% endfor %}
(More code is used in template to add dynamically form instances, as this clever post describes it Django - Javascript dynamic inline FormSet with autocomplete, i wont explain it here)
Currently, when submitting form, I received this error from view :
"Key 'weight' not found in
If I'm trying a
repetitionItem.isValid()
"RepetitionForm' object has no attribute 'isValid"
The same problem appears if I'm using forms.ModelForm instead of forms.Form
I'm quite stuck :p
My Models
class User(models.Model):
name = models.CharField(max_length=100)
mail = models.CharField(max_length=100)
dateCreated = models.DateTimeField('User creation date')
def __unicode__(self):
return self.name
class Session(models.Model):
date = models.DateField('Session Date')
user = models.ForeignKey(User)
def __unicode__(self):
return self.date.strftime("%Y/%m/%d")
class ExerciseTypeRef(models.Model):
name = models.CharField(max_length=100)
desc = models.CharField(max_length=300)
def __unicode__(self):
return self.name
class Exercise(models.Model):
session = models.ForeignKey(Session)
exerciseTypeRef = models.ForeignKey(ExerciseTypeRef)
def __unicode__(self):
return self.exerciseTypeRef.name
class Repetition(models.Model):
exercise = models.ForeignKey(Exercise)
count = models.IntegerField()
weight = models.IntegerField()