1

I am making a front end for custom testing framework. Each test has some associated scripts and each script has some associated parameters and each parameter has a parameter type. I want to be able to create a form to edit all of the parameters for a given test and have each of the fields display and validate according to the associated parameter type, ie if the parameter type is 'bool' the input should be a checkbox, if the type is a url it should validate appropriately.

models.py:

...
PARAM_TYPES = (('bool', 'Boolean (Flag Only)'),
               ('int', 'Integer'),
               ('ip', 'IP Address'),
               ('txt', 'Text'),
               ('url', 'url'),
               ('path', 'File Path'))

class Parameter(models.Model):
    name = models.CharField(max_length=50)
    flag = models.CharField(max_length=20)
    type = models.CharField(max_length=20, choices=PARAM_TYPES)
    description = models.TextField(max_length=200)
    ...

class ParameterInstance(models.Model):
    parameter = models.ForeignKey(Parameter)
    value = models.CharField(max_length=50, blank=True)
    ...

class ScriptInstance(models.Model):
    name = models.CharField(max_length=50)
    test = models.ForeignKey(Test) # One node may have many ScriptIntances (OneToMany)
    script = models.ForeignKey(Script) # Many ScriptInstances to one Script (ManyToOne)
    parameter_instances = models.ManyToManyField(ParameterInstance, blank=True)
    ...
...

Currently my views.py is repackaging the the parameter types, parameter instance id and value information and I am rendering different inputs in my template based on the type. And then using request.POST.getlist() to get the info back into the appropriate records.

It seems to me there should be a way to cause the forms in a formset to pick a widget based on the type. Or something else better than my current way.

Odemia
  • 11
  • 2
  • Define "dynamic"? JavaScript? After each POST? – Jack M. May 05 '11 at 21:46
  • Sorry that was bad wording. Hopefully my edits clarify things. I really don't care where the decision on how to render/validate the fields are made I am just trying to find the best (or better) way to edit my parameters. Hopefully using some of the rendering and/or validation already implemented in django. – Odemia May 07 '11 at 01:19
  • I may well be doing this completely wrong as almost all my experience is in very small embedded systems. If there wasn't a database involved I would use subclasses my parameter instance for each type, but that means a separate table for each type of parameter, which messes up the relationship between the script and it's parameters. Perhaps there is a way to do something similar in Django that I just haven't found yet? – Odemia May 07 '11 at 01:26

0 Answers0