Search code examples
pythondjangoinline-formset

Python Django auto populate inline forms


I am currently working on an 'Job/command abstract manager'. What I want to accomplish is : - A jobInvoker defines params such as class to invoke, params to set in order to init (key / val) - A Tool may need to use a specific job invoker, and it needs to defines the job invokers params when launch (example : need to set up the actual command line to launch for a 'ShellJobinvoker'.

I need to add in the AtgcTool admin (I already use nested-inline https://github.com/Soaa-/django-nested-inlines) the following action : Upon JobInvoker selection, add automatically the fields to set up expected params from the one defined in the JobInvokerExpectedParam.

The job invoker definition :

class JobInvoker(models.Model):
    name = models.CharField("ServiceJob invoker name", max_length=50, null=False)
    description = models.TextField("ServiceJob invoker description", null=True, blank=True)
    isAvailable = models.BooleanField("ServiceJob availability flag", default=True)
    backUpAverage = models.IntegerField("ServiceJob load average value before switching to backUp", default=0)
    backUp = models.ForeignKey('self', null=True, blank=True, related_name='backUpService')
    clazz = models.CharField("ServiceJob invoker injected class name", max_length=150, null=False,
                             choices=settings.ATGC_SERVICES['SERVICE_JOB_INVOKER_IMPL'])

    def __str__(self):
        return self.name


class JobInvokerExpectedParam(Ordered):
    class Meta:
        db_table = 'atgc_job_invoker_param'
    name = models.CharField("Key", max_length=100, blank=True, null=False)
    default = models.CharField("Default", max_length=255, null=True, blank=True)
    job_invoker = models.ForeignKey(JobInvoker)

The Tool model :

class AtgcTool(models.Model):
    name = models.CharField("Service Tool name", max_length=50)
    version = models.CharField("Service tool current version", max_length=15)
    released = models.DateField("Service tool release date", null=True)
    online = models.DateField("Service tool online last update date", null=True)
    description = models.TextField("Service Tool description text", null=True, blank=True)
    slug = models.TextField("Service tool description slug", null=True, blank=True)

For the 'run params'

class RunnableOn(models.Model):
    """
    Specify if a is ServiceTool 'runnable on' a ServiceJobInvoker
    """
    class Meta:
        db_table = 'atgc_tool_runnable_on'
    invoker = models.OneToOneField(JobInvoker)
    tool = models.OneToOneField(AtgcTool)
    threshold = models.IntegerField("Load average threshold", default=100)


class RunnableOnParam(KeyValPair):
    class Meta:
        db_table = 'atgc_tool_run_param'
    mandatoryValue = True
    runOn = models.ForeignKey(RunnableOn)

Solution

  • I finally found a solution, using simply a ManyToMany relationship with related model class defining the parameters I wanted. In order to filter data in inlines, I simply filtered with the related tables data.