I've made a nice form, and a big complicated 'add' function for handling it. It starts like this...
def add(req):
if req.method == 'POST':
form = ArticleForm(req.POST)
if form.is_valid():
article = form.save(commit=False)
article.author = req.user
# more processing ...
Now I don't really want to duplicate all that functionality in the edit()
method, so I figured edit
could use the exact same template, and maybe just add an id
field to the form so the add
function knew what it was editing. But there's a couple problems with this
- Where would I set
article.id
in theadd
func? It would have to be afterform.save
because that's where the article gets created, but it would never even reach that, because the form is invalid due to unique constraints (unless the user edited everything). I can just remove theis_valid
check, but thenform.save
fails instead. - If the form actually is invalid, the field I dynamically added in the edit function isn't preserved.
So how do I deal with this?