Hi, 
The first form saves fine but the second still dosn't.
I can't seem to pass the poll object to the save method of the form 
properly.

Since i'm using nonrel once i've passed the poll object I can append the 
comment using something like poll.comments.append(comment) but I just don't 
seem to be able to get it going. 

I've re-written the form quite a few times without any luck and have looked 
at a few similar examples but can't quite piece it together. Any chance you 
could show me how its done? 

On Thursday, 7 November 2013 23:02:48 UTC-10, Jason S wrote:
>
> Hi Paul,
> After a bit more playing around I got it going, there was another 
> unrelated issue with my code.
> Learnt to use the debugging info more effectively in the process which is 
> great.
>
> Thanks again for your help, very much appreciated.
> Kind regards,
> Jason
>
> On Thursday, 7 November 2013 04:56:14 UTC-10, pnich...@gmail.com wrote:
>>
>> Hey Jason--
>>
>> You defined the save method as needing the user parameter, but you don't 
>> pass that in.  Try that (assuming user should equal request.user).  Good 
>> luck!
>>
>> -Paul
>>
>> On Thursday, November 7, 2013 5:25:18 AM UTC-5, Jason S wrote:
>>>
>>> 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):
>>>  10         return self.name
>>>  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):
>>>  20        return self.name
>>>  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
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ee89b689-ea05-40c6-a68d-216c462db268%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to