Skip to content

Raise a ValidationError for incorrect related slug when importing resources#631

Merged
AdrianDAlessandro merged 4 commits intomainfrom
import-fixes
Mar 19, 2026
Merged

Raise a ValidationError for incorrect related slug when importing resources#631
AdrianDAlessandro merged 4 commits intomainfrom
import-fixes

Conversation

@AdrianDAlessandro
Copy link
Copy Markdown
Collaborator

Description

This PR prevents incorrect slugs from being ignored when importing to the DB using the Resource classes in io_resources.py

It achieves this by adding a check to the clean method of the ForeignKey and M2M Widget classes. If any of the slugs provided in the import data do not exist, it raises a ValidationError. This error provides a dictionary with the offending field as keys, which renders nicely in the Admin backend.

M2M Example with the following import data

name,description,slug,kind,url,learning_resources
new tool,description,new-tool,tool,,what_is_open_source_software_oss|not-a-slug
Screenshot 2026-03-13 at 16 34 53

ForeignKey Example with the following import data

name,description,slug,language,url,provider
new tool,description,new-tool,en,,not-a-slug
Screenshot 2026-03-13 at 16 36 34

Fixes #630

Type of change

  • Documentation (non-breaking change that adds or improves the documentation)
  • New feature (non-breaking change which adds functionality)
  • Optimization (non-breaking, back-end change that speeds up the code)
  • Technical work (non-breaking, change which is work as part of a new feature)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (whatever its nature)

Key checklist

  • All tests pass (eg. python -m pytest)
  • The documentation builds and looks OK (eg. mkdocs serve)
  • Pre-commit hooks run successfully (eg. pre-commit run --all-files)

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added or an issue has been opened to tackle that in the future. (Indicate issue here: # (issue))

@AdrianDAlessandro AdrianDAlessandro marked this pull request as ready for review March 13, 2026 17:29
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

@dalonsoa dalonsoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality looks OK and makes sense, but importing a function as an underscore really does not look good.

Comment thread main/io_resources.py

from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Importing something as an underscore is pretty odd. It can be confusing and it is not informative, when used. Just import and use it normally, which is way cleaner.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a standard Django pattern for translation. I don't love it either, but it's convention, so I'm happy to go with it https://docs.djangoproject.com/en/6.0/topics/i18n/translation/#internationalization-in-python-code

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. It looks horrible, but if that's the standard, so be it...

Comment thread main/io_resources.py
try:
return super().clean(value, row=row, **kwargs)
except ObjectDoesNotExist as e:
raise ValidationError({self.column_name: _(force_str(e))})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here. I barely noticed the underscore was being used, which is not good.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's kind of the point, I think. I believe it's intended as an essentially invisible translation. If the website is in English, then the function does nothing, otherwise it will translate it.

Comment thread main/io_resources.py
)
raise ValidationError(
{
self.column_name: _(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here.

@AdrianDAlessandro AdrianDAlessandro merged commit 91a193b into main Mar 19, 2026
5 checks passed
@AdrianDAlessandro AdrianDAlessandro deleted the import-fixes branch March 19, 2026 11:28
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.

The import data ignores incorrect m2m field slugs

2 participants