Hi,
Disclaimer - I'm new to django and python, so please bear with me.
Note: My django instance uses a nosql database.
I'm trying to create a formset which has multiple forms based on models.
The formset will have one form "post", then 1-3 "comment" forms. Eventually i'd like to be able to add/remove the comment fields but i'll work that out later once the form saves with manually set number of comment fields.
For now the formset just has the two forms "post" and "comment" to make it easy, but if i can save one it should work for more.
The form displays as expected but I get "save() takes at least 2 arguments (1 given)".
I think thats because i'm supplying the "post" data, but not the object itself? I've tried referencing it but without success.
I may need to set the form up as a class with methods and then use something like the following which is how another tut does it, but my first attempt to do it this way failed.
21 def post(self, request, *args, **kwargs):
22 self.object = self.get_object()
23 form = CommentForm(object=self.object, data=request.POST)
24
25 if form.is_valid():
26 form.save()
27 return HttpResponseRedirect(self.object.get_absolute_url())
Models:
7 class Object_Post(models.Model):
8 # Defines the post model
9 def __unicode__(self):
11
12 name = models.CharField(max_length=70)
13 desc = models.TextField()
14 Comments = ListField(EmbeddedModelField('Comment), editable=False)
15
16
17 class Comment(models.Model):
18 # Comments.
19 def __unicode__(self):
21
22 name = models.CharField(max_length=70)
23 desc = models.TextField()
Forms:
39 class PostForm(forms.ModelForm):
40 class Meta:
41 model = Object_Post
42
43 def save(self, user, commit = True):
44 Object_Post = super(PostForm, self).save(commit = False)
45 Object_Post.user = user
46
47 if commit:
48 Object_Post.save()
49
50 return Object_Post
51
52 class CommentForm(forms.ModelForm):
53 class Meta:
54 model = Comment
55
56 def save(self, user, commit = True):
57 Comment = super(CommentForm, self).save(commit = False)
58 Comment.user = user
59
60 if commit:
61 Comment.save()
62
63 return Comment
View:
65 def create_post(request):
66 ....
67 #
68 # Manually set number of comment fields for now
69 commentfields = 1
70
71 if request.method == "POST":
72 pform = PostForm(request.POST, instance=Object_Post())
73 #
74 cforms = [CommentForm(request.POST, prefix=str(x), instance=Comment()) for x in range(0,Commentfields)]
75 if pform.is_valid() and all([cf.is_valid() for cf in cforms]):
76 #
77 new_post = pform.save()
78 for cf in cforms:
79 new_Comment = cf.save(commit=False)
80 new_Comment.Object_Post = new_post
81 new_Comment.save()
82 return HttpResponseRedirect(reverse('blogtut.views.dashboard'))
83 else:
84 pform = PostForm(instance=Object_Post())
85 cforms = [CommentForm(prefix=str(x), instance=Comment()) for x in range(0,Commentfields)]
86 return render_to_response('create_object.html', {'Post_Form': pform, 'Comment_Form': cforms},
87 context_instance=RequestContext(request)
88 )
Template:
1 {% extends "base.html" %}
2
3 {% block content %}
4 <dl>
5
# Irrelevent to the form.
11
12 </dl>
13
14 <form action="{% url blogtut.views.create_post %}" method="post" accept-ch>
15 {% csrf_token %}
16 {{ form.as_p }}
17
18 Enter a name and description for the post: </br>
19 {{ Post_Form }} </br>
20 Enter one or more Comments:</br>
21 {% for mform in Comment_Form %}
22 Comment: {{ cform }}</br>
23 {% endfor %}
24 ....
25 <p><input type="submit" value="Create Now"/></p>
26 </form>
27
28 {% endblock %}
~
I'd really appreciate any help here as i've been hitting my head against this for a week or so now, would particularly appreciate examples as my python/django skills are novice and it'll help me understand.
Thanks for your time/help!
Jason