How to achieve a construction similar to clean_<fieldname>?

5 views
Skip to first unread message

Allan James Vestal

unread,
Sep 9, 2011, 4:43:13 PM9/9/11
to Django users
Hello,

I'm creating a tool that will automate the downloading, processing and
repackaging of data.

The scripting for each data set is composed of two main parts: a
fairly standard models.py file, and a loaders.py file that will
contain all the instructions on how to turn the raw data into what
gets saved. The plan is for the two to be linked: each loader object
will have a Meta class in which the corresponding model will be
declared.

With respect to the data processing, I already have defined a
process_data() method to handle general tasks. But I also want each
field in the model to have a process_<fieldname>() method (so, for
example, I can contain all processing related to field A to the
process_a() method. How would I go about this?

Given a model, I want to be able to create process_<fieldname>()
methods for each field. I know forms have a similar construction with
their clean_<fieldname>() methods. I've been looking at the relevant
parts of the Django source, but can't seem to find where those get
created. Can someone steer me in the right direction?

Thanks in advance.

~Allan

Matt Schinckel

unread,
Sep 9, 2011, 9:04:11 PM9/9/11
to django...@googlegroups.com
You can find it in django.forms.forms.BaseForm._clean_fields (django/forms/forms.py line 271, the line you'll want to look at is 286).

To solve your specific problem, you'll want to do something like:

    for field in self.model._meta.fields:
        if hasattr(self, "process_%s" % field.name):
            getattr(self, "process_%s" % field.name)()
Reply all
Reply to author
Forward
0 new messages