Skip to content

Fix AlertAdminForm tags field and MultiChoiceArrayField issues in Dja… - #537

Merged
Lorygold merged 3 commits into
certego:developfrom
Nivikiwi:fix-alertadmin-tags
Jan 29, 2026
Merged

Fix AlertAdminForm tags field and MultiChoiceArrayField issues in Dja…#537
Lorygold merged 3 commits into
certego:developfrom
Nivikiwi:fix-alertadmin-tags

Conversation

@Nivikiwi

@Nivikiwi Nivikiwi commented Jan 17, 2026

Copy link
Copy Markdown
Contributor

Description
This PR fixes the tags field behavior in the Django Admin for the Alert model.
Previously, the tags ArrayField was rendered as a free-text input or appeared empty due to incorrect form/widget configuration. Multiple attempts using custom widgets and SimpleArrayField caused admin page crashes and internal server errors.

1. Steps to Reproduce

  • Start the Django development server
    Run the command : python manage.py runserver on virtual environment

  • Use the command : "docker restart buffalogs"
    run the command on the project root (BuffaLogs)

  • Go to the admin panel
    http://127.0.0.1:8001/admin/

  • Open any existing Alert object or create a new one

  • Observe the tags field:
    Renders as a free-text input or
    Renders empty with no selectable values or
    Admin page crashes with HTTP 500 error

2. Evidence

  • Django Admin returned:
    500 Internal Server Error

  • Tracebacks included errors such as:
    TypeError: Field.init() got an unexpected keyword argument 'base_field'

3. Attempted Solutions

  • Tried using filter_horizontal → only works with ManyToManyField
  • Custom SimpleArrayField + widgets → broke admin rendering
  • Passing base_field to MultipleChoiceField → invalid argument
  • Overriding admin widgets directly → values disappeared

4. Changes

  • Simplified tags handling by removing Select2 and custom widgets.
  • Replaced complex multi-select configuration with a standard MultipleChoiceField.
  • Added explicit clean_tags() implementation to correctly parse multi-value inputs from admin POST data.
  • Prevented invalid None values by normalizing empty selections to an empty list.
  • Reduced admin form complexity and eliminated prior widget-related errors.

5. Results and Screenshots

  • Django Admin loads successfully
  • Tags render as a searchable multi-select dropdown
  • Multiple tags can be selected and saved
  • Existing tag values are preserved
  • No runtime errors or admin crashes

image

Closes #497

@Nivikiwi Nivikiwi changed the title Fix AlertAdminForm tags field and MultiChoiceArrayField issues in Dja… Fix AlertAdminForm tags field and MultiChoiceArrayField issues in Dja… Fixes #537 Jan 18, 2026
@Nivikiwi Nivikiwi changed the title Fix AlertAdminForm tags field and MultiChoiceArrayField issues in Dja… Fixes #537 Fix AlertAdminForm tags field and MultiChoiceArrayField issues in Dja… Jan 18, 2026
@Lorygold

Copy link
Copy Markdown
Contributor

why 171 files changed?

@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch from bfe17ab to 21c3892 Compare January 20, 2026 10:37
@Nivikiwi

Copy link
Copy Markdown
Contributor Author

why 171 files changed?

sorry i accidentally pushed all the files, the files that are not even changed were pushed.
I have edited it , now you can see 4 files only

@Lorygold

Lorygold commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

could be possible to do it without new tools, such as select2? Same as the other fields are implemented

@Nivikiwi

Nivikiwi commented Jan 20, 2026

Copy link
Copy Markdown
Contributor Author

could be possible to do it without new tools, such as select2? Same as the other fields are implemented

I initially attempted this using Django’s built-in widgets and MultipleChoiceField, but couldn’t get it working as expected for this field. While exploring alternatives, I tried DjangoSelect2 as a quick solution. I found out that DjangoSelect2 has a better UI interface and the user need not search for the tag manually in the list. I checked the method used by others... they used something called append which is not having a good UI interface when compared with Djangoselect2 , user need to manually find tags in the list and it is hard to work with when there is a list of long tags

I agree that it’s better to avoid new dependencies here and I can refactor this to use Django’s native admin widgets instead.

@Lorygold

Copy link
Copy Markdown
Contributor

yes, the same widget already used for other fields (such as in the Config model) are preferred

@Nivikiwi

Copy link
Copy Markdown
Contributor Author

yes, the same widget already used for other fields (such as in the Config model) are preferred

I’ve pushed the updated code. Would you like me to make any additional changes?
The updated code output
image

Comment thread buffalogs/celerybeat-schedule Outdated
Comment thread docker-compose.yaml Outdated
Comment thread buffalogs/buffalogs/settings/settings.py Outdated
@Lorygold

Copy link
Copy Markdown
Contributor

the black linter fails, run it locally as defined in the CONTRIBUTING.md file

@Nivikiwi

Copy link
Copy Markdown
Contributor Author

the black linter fails, run it locally as defined in the CONTRIBUTING.md file

I did use the command "black (file.py)" locally but I didn't understand why it is not reflecting on the file

@Lorygold

Lorygold commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

@Nivikiwi you should use the linters locally with the configurations files defined in the project, so running them with exactly the same command defined in the CONTRIBUTING.md file.
For example:
> black --config .github/configurations/python_linters/.black .
not only > black .

Did you?

@Nivikiwi

Copy link
Copy Markdown
Contributor Author

@Nivikiwi you should use the linters locally with the configurations files defined in the project, so running them with exactly the same command defined in the CONTRIBUTING.md file. For example: > black --config .github/configurations/python_linters/.black . not only > black .

Did you?

yes i did that and i am able to see image

@Lorygold

Copy link
Copy Markdown
Contributor

which version of black do you have locally?

@Nivikiwi

Copy link
Copy Markdown
Contributor Author

which version of black do you have locally?

black, 24.2.0

@Nivikiwi

Nivikiwi commented Jan 23, 2026

Copy link
Copy Markdown
Contributor Author

which version of black do you have locally?

black, 24.2.0
I think there has been a version mismatch

@Nivikiwi Nivikiwi closed this Jan 23, 2026
@Nivikiwi Nivikiwi reopened this Jan 23, 2026
@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch from cc4432b to 317b74b Compare January 26, 2026 14:00
@Lorygold

Copy link
Copy Markdown
Contributor

Try to install black 26.1.0 locally and running:
> black . --check --diff --config ${GITHUB_WORKSPACE}/.github/configurations/python_linters/.black

@Lorygold

Lorygold commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

@Nivikiwi in the CI it's already been updated, you have just to merged from the develop branch, not updating it manually in this PR

Comment thread .github/workflows/_python.yml
Comment thread django-buffalogs/setup.cfg
Comment thread CHANGELOG.md
@Lorygold

Copy link
Copy Markdown
Contributor

@Nivikiwi sorry, but why you changed a lot of files that should not been affected by this fix?

@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch from 6fc4490 to c562f2a Compare January 27, 2026 13:48
@Nivikiwi

Copy link
Copy Markdown
Contributor Author

@Nivikiwi sorry, but why you changed a lot of files that should not been affected by this fix?

I'm so sorry but i was just checking why my files are failing in the CI run test
I have undo the files

@Lorygold

Copy link
Copy Markdown
Contributor

@Nivikiwi I tested it locally, the changes to the forms.py work correctly, undo the changed to the other files (settings.py, docker-compose and _python.yml) please and it's okay

@Nivikiwi

Nivikiwi commented Jan 27, 2026

Copy link
Copy Markdown
Contributor Author

@Nivikiwi I tested it locally, the changes to the forms.py work correctly, undo the changed to the other files (settings.py, docker-compose and _python.yml) please and it's okay

Thank you so much and sorry for the inconvenience
I tested it locally and reformatted settings.py file locally with black and also ran "black . --check --diff --config .github/configurations/python_linters/.black" this command
It all looks good now
Can you retrigger the CI test now

@Lorygold

Copy link
Copy Markdown
Contributor

@Nivikiwi I tested it locally, the changes to the forms.py work correctly, undo the changed to the other files (settings.py, docker-compose and _python.yml) please and it's okay

Thank you so much and sorry for the inconvenience I tested it locally and reformatted settings.py file locally with black and also ran "black . --check --diff --config .github/configurations/python_linters/.black" this command It all looks good now Can you retrigger the CI test now

Undo the changed to the other files (settings.py, docker-compose and _python.yml), not needed

@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch 2 times, most recently from 1a9533f to d1d2ed2 Compare January 27, 2026 16:19
@Lorygold

Copy link
Copy Markdown
Contributor

Here we go, could you please undo also the changes to the docker-compose and settings files, please?

@Nivikiwi

Copy link
Copy Markdown
Contributor Author

Here we go, could you please undo also the changes to the docker-compose and settings files, please?

yes

@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch 2 times, most recently from 257023f to c0facee Compare January 28, 2026 10:29
Comment thread buffalogs/impossible_travel/forms.py Outdated
@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch from c0facee to efa98e3 Compare January 28, 2026 12:21
@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch from efa98e3 to b265f53 Compare January 28, 2026 12:26
Comment thread buffalogs/impossible_travel/forms.py Outdated
@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch 2 times, most recently from 72e2306 to 5811876 Compare January 28, 2026 15:47
@Nivikiwi

Copy link
Copy Markdown
Contributor Author

the CI test is failing again and I don't understand where it is going wrong as I have formatted the forms.py file locally and checked locally if it it formatted and it says it has been formatted
Can you help me?
Thank you

@Lorygold

Copy link
Copy Markdown
Contributor

it's because the following lines are still modified, not left unchanged as required:

image

@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch from 5811876 to d6c0c07 Compare January 29, 2026 10:58
@Nivikiwi
Nivikiwi force-pushed the fix-alertadmin-tags branch from d6c0c07 to a52477b Compare January 29, 2026 11:00
@Nivikiwi

Copy link
Copy Markdown
Contributor Author

it's because the following lines are still modified, not left unchanged as required:

image

Yes , now I have reformatted the file and also the lines are now back to how there actually were

@Nivikiwi

Copy link
Copy Markdown
Contributor Author

it's because the following lines are still modified, not left unchanged as required:

image

I have kept those lines as they were but the CI test failed again
I ran black forms.py command and noticed that when I executed the command, it formatted the file in such a way that the import from .constants line the alertagvalues and everything else goes one below the other because of the formatting

@Lorygold
Lorygold self-requested a review January 29, 2026 15:33
@Lorygold

Copy link
Copy Markdown
Contributor

I've fixed just applying black, isort and flake8 locally and pushing the changes; I think that you just have different linters versions locally

@Lorygold
Lorygold merged commit fd4c53e into certego:develop Jan 29, 2026
2 checks passed
@Nivikiwi

Copy link
Copy Markdown
Contributor Author

I've fixed just applying black, isort and flake8 locally and pushing the changes; I think that you just have different linters versions locally

I think so
Thank you so much for your help and guidance throughout this issue.
This was my first contribution, and I made a few mistakes along the way — your explanations and patience really helped me understand where I was going wrong.
I really appreciate you taking the time to help me close this issue. Thanks again!

@Lorygold

Copy link
Copy Markdown
Contributor

I've fixed just applying black, isort and flake8 locally and pushing the changes; I think that you just have different linters versions locally

I think so Thank you so much for your help and guidance throughout this issue. This was my first contribution, and I made a few mistakes along the way — your explanations and patience really helped me understand where I was going wrong. I really appreciate you taking the time to help me close this issue. Thanks again!

Thank you very much for your kind words. I’m really glad my explanations were helpful and that you were able to close the issue successfully. It was a pleasure to help, and I appreciate your thoughtful feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REFACTOR] <Admin page>: Improve Alert.tags field visualization in Django Admin

2 participants