开发者

django i can't save form with 2 foreign keys

开发者 https://www.devze.com 2022-12-31 23:37 出处:网络
i have a Reply class: class Reply(models.Model): reply_to = models.ForeignKey(New) creator = models.ForeignKey(User)

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 u开发者_开发问答sers 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_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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消