On Mon, 2009-05-04 at 12:07 -0700, Tomáš Ehrlich wrote:
> Hi there,
> I wonder if is there any way to add extra input field in admin, which
> I don't want to define in model.
> 
> For example, I want to import external files. I have simple blog model
> (title, content, pub_date), but in admin I want to also have file
> input field, to import files.
> 
> Then I can:
> 1) Import external file, parse it and save values (title, content,
> pub_date) to database.
>  or
> 2) Enter values (title, ...) as usuall directly in admin.
> 
> Is there any way how to do this without making admin template for this
> application ?

Firstly, creating an admin template isn't a bad thing. Turns out there's
probably a more direct way to do what you want, but this is the second
time today on this list I've read about somebody wanting to preemptively
avoid a designed-in way to modify the admin. It's like asking "can I do
this in Python without using the 'def' keyword?" :-)

More importantly, though, the trick to this sort of thing is changing
the model form that is given to the ModelAdmin class. You can do
whatever you like in that form, although subclass ModelForm is probably
good thing, since there are some assumptions built into the Admin (and
it's not really onerous). You will probably also need to override one or
both of save_form() and save_model() in your ModelAdmin subclass.
They're not too badly documented in the django.contrib.admin.options
code (as docstrings) and a bit of playing around should reveal what's
going.

I've had a TO-BLOG item floating around for a while to remind me to
write about funky admin customisation along these lines. It's certainly
possible For now, as a bit of a guide, I can point you at [1], which
contains a similarly tricked-out admin page. It replaces some of the
default model fields with an entirely non-model field and then patches
in the necessary inforamtion in save_form() and save_model(). From
memory, the bug mentioned in the "XXX" comment there (line 46) has now
been fixed in Django trunk, too, although I haven't checked that out (I
remember seeing a patch for a ticket go past in the last couple of
weeks, that's all).

[1]
http://github.com/malcolmt/django-acacia/blob/6158e0137b2be9b6fa753eb3466e9d80ea2fae27/acacia/admin.py

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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to