views:

96

answers:

2

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!

+2  A: 

I may have missed something, but reply_to needs an instance of the New model. New.id doesn't look like one to me?

new_obj.reply_to = New.id

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

new_post = New(whatever, goes, here)
new_post.save()

Then assign it to reply_to

new_obj.reply_to = new_post

Or similar.

edit

Without knowing exactly that ReplyForm looks like I'm guessing a bit, but presumably it's based on the Reply object, 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:

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
   new_obj.save()

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.

pycruft
yes:) it works, hard coded i mean :new_obj.creator = request.user new_post = New(1) new_obj.reply_to = new_post new_obj.save()but i don't know how to pass the messageId (from New model) to the reply. i tried using u = New.objects.get(pk=id), and New(u), but no results.
dana
If you have an existing post (instance of New) you can pull from the database, then you can use that rather than creating a new one. e.g. `u = New.object.get(pk=id) # assuming this works.` `new_obj.reply_to = u` `new_obj.save()`. Do you have the information available to pull the right post from the db at that point? (I may be being dim, presumably it's in the form object? `new_obj.reply_to = form.reply_to` or similar?)
pycruft
i've edited my question above:)
dana
i've edited, adding my ReplyForm.it doesn't work like above, it says:'ReplyForm' object has no attribute 'reply_to'as reply_to is a foreign key to the New i.e the table containing user's posts
dana
Right. If I understand things then I think the problem you have is that you don't have a way to identify the post (New instance) which is being replied to?There are probably different ways to deal with it, but I would imagine that putting it in the form would make for the easiest. I imagine that the last post looked at could instead go in the user's session or similar, but that would seem more convoluted to me.You need to 1. identify which post is being replied to, 2. look that object up from the database and 3. assign it to new_obj.reply_to ... which will all depend on your app
pycruft
yes:) that's it. all i need is that 'id' of the post one is answering to,and of course, one way of taking it when one pushes the reply button(link) . i guess i'm close, but still miss something, i'll think more:)
dana
+1  A: 

Not sure if this'll be appropriate for your app or not, but you could try making use of a form widget, in particular the HiddenInput one to include the post (id) in the form. Something like

class ReplyForm(ModelForm):
    reply_to = forms.ModelChoiceField(New.objects.all(), widget=forms.HiddenField)
    class Meta:
        model = Reply
        fields = ['reply', 'reply_to']
        widgets = {
           'reply_to': HiddenField,
        }

(Not sure that's entirely correct but see overriding-the-default-field-types-or-widgets for more).

You've now enabled the id you need to be passed to the client and back through the form, you now just need to put it in when you create the form for display in the first place

else: 
    form = ReplyForm()
    form.reply_to = # ... fill in the current post (New inst) being replied to
    # presumably from somewhere in the request object?
    return render_to_response('replies/replies.html', { 'form': form, }, 

Hopefully that doesn't lead you off on the wrong track - completely untested, E&OE, YMMV, etc, etc

pycruft
hmm... it still doesn't work.I've reformulated it in another question here:http://stackoverflow.com/questions/2923607/django-blog-reply-systemthanks:)
dana