form_for_model is marked as deprecated but ModelForm does not appear
to be a full replacement for it. Given an arbitrary model class (i.e.
one that is defined at runtime, not at "compile" time) there is no
clear way of creating a form from it using the ModelForm mechanism.

Here's what I am doing at the moment:

from django.db import models
from django import newforms as forms

model_name = 'sites.Site' # Actually comes from outside the system
model_class = models.get_model(*model_name.split("."))

form_class = forms.form_for_model(model_class)
form = form_class(data_dict)
if form.is_valid():
    form.save()
...

This works great, since form_for_model() works at runtime. With
ModelForm, the model class for which I want a form has to be baked in
to the class in the source code. I suppose I could dynamically
construct a ModelForm subclass at runtime (any pointers on how to do
that, including dynamically creating the inner class called "Meta",
would be welcome) but for the above the existing form_for_model()
function is a whole bunch more intuitive.

I like the syntactic sugar provided by declarative APIs such as
ModelForm, but I think it's important to provide a non-declarative
alternative for people who need to dynamically construct something at
runtime rather than baking the details in to their source code.

Cheers,

Simon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to