create_or_update in ModelForm

Posted by ykaganovich on Stack Overflow See other posts from Stack Overflow or by ykaganovich
Published on 2010-04-28T20:55:07Z Indexed on 2010/04/28 20:57 UTC
Read the original article Hit count: 310

Filed under:
|

I want to have a ModelForm that can create_or_update a model instance based on the request parameters.

I've been trying to cobble something together, but am realizing that my python fu is not strong enough, and the ModelForm implementation code is a quite hairy.

I found this create_or_update snipplet for working with a Model, but I think it would be incredibly useful if it were integrated with a ModelForm.

I would expect it to behave similarly to ModelForm.save():

class BetterModelForm(forms.ModelForm): def init(self, *args, **kwargs)

def create_or_update(self):
    #magic
    return (instance, created, updated)

Conversely I'd also be interested in hearing compelling reasons why this is not a good idea.

© Stack Overflow or respective owner

Related posts about django-models

Related posts about django-forms