dragon-fire-fly / developer_matcher

2 stars 1 forks source link

[BUG] Checkboxes are not rendered pre-checked #45

Closed dragon-fire-fly closed 1 year ago

dragon-fire-fly commented 1 year ago

Describe the bug When a user goes to the edit form for either the user or a project, the programming language checkboxes are not pre-checked with the information in the database. Even if programming languages are associated with a user/project, the checkboxes are all rendered unchecked.

To Reproduce Steps to reproduce the behavior:

  1. Go to 'user profile'
  2. Click on 'edit'
  3. Scroll down to 'checkbox section for programming languages'
  4. See error

Expected behavior If the user/project has associated programming language(s), the checkbox(es) for that language(s) should render pre-checked.

Screenshots prior to entering the update form: Screenshot from 2023-02-23 19-47-28 in the update form: Screenshot from 2023-02-23 19-48-58

Desktop (please complete the following information):

Additional context When checkboxes are checked in the edit profile/project form, the data is being saved to the programming language database, so connection between the checkboxes and the database does not appear to be the issue.

dragon-fire-fly commented 1 year ago

Found this stack overflow article which addresses this problem: https://stackoverflow.com/questions/33597775/django-checkboxselectmultiple-my-checkboxes-are-not-checked

Added the following code to each of the relevant form classes:

def __init__(self, *args, **kwargs):
        super(UserEditForm, self).__init__(*args, **kwargs)
        p_lang_q_set = ProgrammingLanguage.objects.filter(user=self.instance)
        p_langs_init = []
        for lang in p_lang_q_set:
            p_langs_init.append(lang.pk)
        self.initial["p_language"] = p_langs_init

Image