On Mon, 2006-05-15 at 16:59 +0200, Alexandre CONRAD wrote:
> Hello,
> 
> I've already talked about it in the IRC channel, but I'm posting here to 
> make sure...
> 
> I have a model Client like so:
> 
> class Client(models.Model):
>      name = models.CharField(maxlength=30, core=True)
>      contact_name = models.CharField(maxlength=30, blank=True)
>      contact_email = models.EmailField(blank=True)
> 
> So I'd like to add a new client. Isn't it possible to use the 
> autogeneration of the admin for my app ?
> 
> Instead of writing a template declaring *every* field of the Client 
> model, wouldn't it be possible to autogenerate the form from the 
> existing model ?
> 
> If I never add a new entry in my model, let's say:
> 
>      city = models.CharField(maxlength=30, core=True)
> 
> when I reload my "add_client" (or "edit_client") page, it would 
> automaticly display the form including the new "city" field without 
> having to touch my template code.
> 
> I'm about to go into a "for" loop inside the form.data dictionnary to 
> generate every field. But as it already took me some time to use 
> organize the "fields" variable like I wanted for the "Admin" class, I 
> was telling myself there *MUST* be someway to reuse the admin code for 
> autogenerating forms in my own application.

Django provides a way to do this: it's called the admin interface!
That's the generic data entry point. Beyond that, what you are asking
for does not seem to be something that would be used a lot, because
every form looks different. If you are creating custom add and edit
pages, it is presumably because you want them to not look like the admin
template (note that if you just want to change the style or even the
templates of the admin page, you can do that), so replicating the admin
interface's logic seems a little redundant.

However, to give you some help in the code reuse department, you can
have a look at the code that creates the admin forms prior to rendering
(it's not documented in docs/, but it isn't too hard to understand).
Have a look in django/contrib/admin/views/main.py at the
AdminBoundField , AdminBoundFieldSet and AdminBoundLineField classes.
Particularly the latter two both provide iterators so you can construct
an appropriate instance and iterate over it in your template to produce
the output. I've never tried this, but it doesn't look too painful from
just scanning the code.

Regards,
Malcolm




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to