Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with django-countries #484

Open
debnet opened this issue Jul 11, 2024 · 3 comments
Open

Compatibility with django-countries #484

debnet opened this issue Jul 11, 2024 · 3 comments

Comments

@debnet
Copy link

debnet commented Jul 11, 2024

Hello,

I want to include language in my index document but in my project these fields are CountryField from django-countries third-party app (https://github.com/SmileyChris/django-countries) and can't be serialized when rebuilding the index.

I tried to create a custom field with (de-)serialize override to text but in the to_dict() result the behaviour doesn't seem to work.
I may have missed something in the documentation, I would appreciate some insight on this.

All the best.

@debnet
Copy link
Author

debnet commented Jul 11, 2024

I tried something like this but it doesn't work, and the documentation is not helpful.

class CustomCountryField(fields.TextField):
    def serialize(self, data):
        return data.code


@registry.register_document
class ArticleDocument(Document):
    country = CustomCountryField()
    ...

    @classmethod
    def get_model_field_class_to_field_class(cls):
        field_mapping = super().get_model_field_class_to_field_class()
        field_mapping[CountryField] = CustomCountryField
        return field_mapping

@safwanrahman
Copy link
Collaborator

I need to look at it. Will get back to you shortly

@debnet
Copy link
Author

debnet commented Jul 15, 2024

I managed to workaround by overriding get_value_from_instance method.

class CustomCountryField(fields.TextField):
    def get_value_from_instance(self, instance, field_value_to_ignore=None):
        value = super().get_value_from_instance(instance, field_value_to_ignore)
        return value.code if value else ""

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

No branches or pull requests

2 participants