i have a Reply class:
class Reply(models.Model):
reply_to = models.ForeignKey(New)
creator = models.ForeignKey(User)
reply = models.CharField(max_length=140,blank=False)
a replay form:
class ReplyForm(ModelForm):
class Meta:
model = Reply
fields = ['reply']
where New is the Post class (containing users posts)
and a view
def save_reply(request):
#u = New.objects.get(pk=id)
if request.method == 'POST':
form = ReplyForm(request.POST)
if form.is_valid():
new_obj = form.save(commit=False)
new_obj.creator = request.user
new_obj.reply_to = form.reply_to
# reply_to_id = u
new_post = New(2) #this works hardcoded, but how can i get the blog New post #id, as a parameter, instead?
new_obj.reply_to = new_post
new_obj.save()
return HttpResponseRedirect('.')
else:
form = ReplyForm()
return render_to_response(‘replies/replies.html’, {
‘form’: form,
},
context_instance=RequestContext(request))
where created_by belongs to New class and represents the creator of the post (which is to be replied)
how can i assign the current post to the reply under it?
thanks in advance!
I may have missed something, but
reply_toneeds an instance of the New model.New.iddoesn’t look like one to me?Do you have an instance of the New model available at that point that you can assign?
ah, I see you’ve tweaked the question
If you don’t have an instance of the New model, you’ll need to create one
Then assign it to
reply_toOr similar.
edit
Without knowing exactly that
ReplyFormlooks like I’m guessing a bit, but presumably it’s based on theReplyobject, letting the user select the reply_to field somehow or other?Assuming that the form’s reply_to variable is populated & correct I think you should just be able to do:
In fact since it’s a foreign key, the
new_obj = form.save(commit=False)may have already set .reply_to for you? The Django Model Forms docs may help.