First time here? Check out the FAQ!

Why "must add revision before saving the answer"?

  • retag add tags


In, there's the following:

    #must add revision before saving the answer
        author = edited_by,
        revised_at = edited_at,
        text = text,
        comment = comment,
        by_email = by_email

How come the revision has to be saved before the answer?


kate_r's avatar
asked 2013-06-15 20:14:52 -0500
edit flag offensive 0 remove flag close merge delete


add a comment see more comments

1 Answer


Not sure what your intention is. The safest approach is to use function user.post_answer(). There are also post_question() and post_comment() - those will take care of all the underlying records and will test the permissions.

If you want to follow the underlying mechanics - basically any time a foreign key relation to another object - that one has to be created first. In this case - answer object is created first, then we create the revision, because revision requires the foreign key to the answer post.

Evgeny's avatar
answered 2013-06-15 20:19:24 -0500, updated 2013-06-15 20:21:54 -0500
edit flag offensive 0 remove flag delete link


I might not be reading the code right. Isn't the revision created before the answer here? self.add_revision(..) is called before self.parse_save(..), which saves the answer object. It only doesn't crash here because the post object itself would have already been created (because it's an edit function). My point is mostly that the comment "must add revision before saving the answer" might perhaps be misleading?

kate_r's avatar kate_r (2013-06-15 20:44:26 -0500) edit

That's probably in the edit function - in that case we already have the post id to use for the foreign key. In the create answer function we do this in the opposite order, see PostManager.create_new.

Evgeny's avatar Evgeny (2013-06-15 20:47:46 -0500) edit

If you use functions added to the user object, you can bypass all those details (and there are more, actually).

Evgeny's avatar Evgeny (2013-06-15 20:50:12 -0500) edit

Sure. Thanks for the pointers. So does that mean that comment in the edit function doesn't actually hold?

kate_r's avatar kate_r (2013-06-15 20:54:36 -0500) edit

No it all works, just in the edit function we have the post, so we can safely create a revision. When we make a new post - we first make the post and then add the revision. See functions available in the askbot/models/, added via User.add_to_class - they start at about 75% down the file.

Evgeny's avatar Evgeny (2013-06-15 20:58:03 -0500) edit
add a comment see more comments