from django import forms

from .models import Label, Talk


class TalkForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(TalkForm, self).__init__(*args, **kwargs)
        labels = Label.objects.all()
        for label_ in labels:
            self.fields.update({
                'label_{}'.format(label_.id): forms.BooleanField(
                    label=label_.name,
                    required=False,
                )})

    class Meta:
        model = Talk
        exclude = ['company']

    def save(self):
        talk = super(TalkForm, self).save()
        for label_ in Label.objects.all():
            if self.cleaned_data['label_{}'.format(label_.id)]:
                label_.talks.add(talk)

from django import forms

from .models import Label, Talk


class TalkForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(TalkForm, self).__init__(*args, **kwargs)
        instance = kwargs['instance']
        labels = Label.objects.all()
        for label_ in labels:
            if instance is None:
                initial = False
            else:
                initial = label_.talks.filter(id=instance.id).exists()
            self.fields.update({
                'label_{}'.format(label_.id): forms.BooleanField(
                    label=label_.name,
                    required=False,
                    initial=initial,
                )})

    class Meta:
        model = Talk
        exclude = ['company']

    def save(self, commit=True):
        talk = super(TalkForm, self).save()
        for label_ in Label.objects.all():
            if self.cleaned_data['label_{}'.format(label_.id)]:
                label_.talks.add(talk)

Update TalkForm to use checked labels
Update TalkForm to use checked labels

  from django import forms
  
  from .models import Label, Talk
  
  
  class TalkForm(forms.ModelForm):
  
      def __init__(self, *args, **kwargs):
          super(TalkForm, self).__init__(*args, **kwargs)
+         instance = kwargs['instance']
          labels = Label.objects.all()
          for label_ in labels:
+             if instance is None:
+                 initial = False
+             else:
+                 initial = label_.talks.filter(id=instance.id).exists()
              self.fields.update({
                  'label_{}'.format(label_.id): forms.BooleanField(
                      label=label_.name,
                      required=False,
+                     initial=initial,
                  )})
  
      class Meta:
          model = Talk
          exclude = ['company']
  
-     def save(self):
+     def save(self, commit=True):
          talk = super(TalkForm, self).save()
          for label_ in Label.objects.all():
              if self.cleaned_data['label_{}'.format(label_.id)]:
                  label_.talks.add(talk)
  
Update TalkForm to use checked labels
## Code Before:
from django import forms

from .models import Label, Talk


class TalkForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(TalkForm, self).__init__(*args, **kwargs)
        labels = Label.objects.all()
        for label_ in labels:
            self.fields.update({
                'label_{}'.format(label_.id): forms.BooleanField(
                    label=label_.name,
                    required=False,
                )})

    class Meta:
        model = Talk
        exclude = ['company']

    def save(self):
        talk = super(TalkForm, self).save()
        for label_ in Label.objects.all():
            if self.cleaned_data['label_{}'.format(label_.id)]:
                label_.talks.add(talk)

## Instruction:
Update TalkForm to use checked labels
## Code After:
from django import forms

from .models import Label, Talk


class TalkForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(TalkForm, self).__init__(*args, **kwargs)
        instance = kwargs['instance']
        labels = Label.objects.all()
        for label_ in labels:
            if instance is None:
                initial = False
            else:
                initial = label_.talks.filter(id=instance.id).exists()
            self.fields.update({
                'label_{}'.format(label_.id): forms.BooleanField(
                    label=label_.name,
                    required=False,
                    initial=initial,
                )})

    class Meta:
        model = Talk
        exclude = ['company']

    def save(self, commit=True):
        talk = super(TalkForm, self).save()
        for label_ in Label.objects.all():
            if self.cleaned_data['label_{}'.format(label_.id)]:
                label_.talks.add(talk)
