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

Override formfield method on Brazilian Model Fields #489

Open
lucasgueiros opened this issue May 16, 2023 · 2 comments
Open

Override formfield method on Brazilian Model Fields #489

lucasgueiros opened this issue May 16, 2023 · 2 comments

Comments

@lucasgueiros
Copy link

Hello.
The Model Fields on Brazilian localflavor does not override the method formfield. By instance, the BRPostalCodeField is defined as:

class BRPostalCodeField(CharField):
    """
    A model field for the brazilian zip code

    .. versionadded:: 2.2
    """

    description = _("Postal Code")

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 9
        super().__init__(*args, **kwargs)
        self.validators.append(validators.BRPostalCodeValidator())

But in other countries this method was overrided. As instance, this is the Canadian PostalCode:

class CAPostalCodeField(CharField):
    """
    A model field that stores the Canadian Postal code in the database.

    Forms represent it as a :class:`~localflavor.ca.forms.CAPostalCodeField` field.

    .. versionadded:: 4.0
    """

    description = _("Canadian Postal Code")

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 7
        super().__init__(*args, **kwargs)

    def formfield(self, **kwargs):
        defaults = {'form_class': CAPostalCodeFormField}
        defaults.update(kwargs)
        return super().formfield(**defaults)

I made this change on Brazilian fields and it has passed all the tests. Do I need to write more tests or I can already submit a pull request?

@claudep
Copy link
Member

claudep commented May 16, 2023

It would be nice if you can provide a test that fails before your changes and passes afterwards (so that it would catch any future regression)!

@lucasgueiros
Copy link
Author

Thanks, @claudep !
I wrote 3 tests and altered one more. They fail with the current code but pass with my commits. I also added max_length and min_length validatiosn to the forms.BRZipCodeFIeld, so I could write it a test case similar to BRCPFFIeld and BRCNPJField. Finnaly, I removed the formfield override I made to BRStateField because it doesn't changed anything on the field behaviour. In future, it would be possible to enhance this field and, maybe, this override would be valuable. I will send all change as a Pull Request.

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