Last Updated: February 25, 2016
·
1.081K
· boblefrag

Django function to create form for a model object

def create_form(type, id):
    """
    Create the form for the object beeing edited.
    """
    inner_model = get_model('<your app>', type)
    instance = inner_model.objects.get(pk=id)

    class MetaForm(ModelForm):
        class Meta:
            model = inner_model

    form = MetaForm(instance=instance)
    return form