How to add condition in django-widget-tweaks checkbox form (checked and disabled if attributes is existed)

I'm using django-widget-tweaks and dynamic-field to render my form. This form is use to crete a new Line. User need to select Department (one line has one department) and Process (one line has many process)

forms.py

class LineForm(DynamicFormMixin, forms.Form):
    def process_choices(form):
        department= form['department'].value()
        return Process.objects.filter(department=department) 
    
    name = forms.CharField(label='Line Name', max_length=100)

    department = forms.ModelChoiceField(
        queryset = Department.objects.all(),
        initial = Department.objects.first()
    )

    # process field 
    process = DynamicField(
        forms.ModelMultipleChoiceField,
        queryset=process_choices,
        required=False,
        label="Process",
        widget=forms.CheckboxSelectMultiple(),

models.py

class Process(models.Model):
    process_id = models.AutoField(db_column='Line_ID', primary_key=True)
    name = models.CharField(db_column='Line_Name', max_length=30)
    department = models.ForeignKey(Department, on_delete=models.CASCADE)
    masterLine = models.ForeignKey(MasterLine, null=True, on_delete=models.SET_NULL)

From the relationship in the model, how can I customized the checkbox by adding a condition:

if process already have line associated with it, the process will checked and disabled

Back to Top