Preprocess data before showing it on Admin

137 views
Skip to first unread message

Pedro Vasconcelos

unread,
Apr 1, 2012, 3:25:23 PM4/1/12
to django...@googlegroups.com
Hello!

Anyone knows how preprocess data before showing it on Admin (change page)?
 ______      ______________      _______
/      \    /              \    /       \
|  DB  | -> | PREPROCESSOR | -> | ADMIN |
\______/    \______________/    \_______/

I want to edit data (in Admin) in a human-friendly-format (my custom format) and store it in XML. So, when a user comes to edit data on Admin, it cant be the raw data from DB. It need to be converted (preprocessed) before and then showed.

Anyone can help me? Thanks!

--
Pedro Vasconcelos
85 8767.1843
ptronico (skype)

Marc Aymerich

unread,
Apr 1, 2012, 3:36:44 PM4/1/12
to django...@googlegroups.com

Pedro Vasconcelos

unread,
Apr 1, 2012, 3:42:38 PM4/1/12
to django...@googlegroups.com
Hello Marc! 

Thanks for replying. Unfortunately I cant realised how to get the current value, change it and populate the form using a ModelForm. I've tried it actually.

I appreciate if you send me tips on that direction!

Thanks a lot!

--
Marc

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

Marc Aymerich

unread,
Apr 1, 2012, 3:55:43 PM4/1/12
to django...@googlegroups.com
On Sun, Apr 1, 2012 at 9:42 PM, Pedro Vasconcelos <pe...@pedrorafa.com> wrote:
> Hello Marc!
>
> Thanks for replying. Unfortunately I cant realised how to get the current
> value, change it and populate the form using a ModelForm. I've tried it
> actually.
>
sure, you can do it on form __init__ method

something like

class YourForm(ModelForm):
def __init__(self, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
self.fields['your_field'].initial = your_postprocess_method()

also you should override the save form method in order to "reverse"
the postprocess and save the data in raw format.


--
Marc

Pedro Vasconcelos

unread,
Apr 1, 2012, 5:11:08 PM4/1/12
to django...@googlegroups.com
Thanks for your time and your tips Marc!

I had tried earlie what you suggested but it didnt solved my problem because it only assigns initial values to new records and not to existents ones.

But i finally figured out a small change on code which does it well. I just replaced "self.fields['your_field'].initial" with "self.initial['text']".

Thanks again! Regards,



--
Marc

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

Reply all
Reply to author
Forward
0 new messages