VeryApt / django-phone-field

Lightweight model and form field for phone numbers in Django
GNU General Public License v3.0
52 stars 13 forks source link

When using dumpscript from django-extensions, PhoneField's are output using their repr. #19

Open ericvsmith opened 9 months ago

ericvsmith commented 9 months ago

For example, if I have an app foo, that has a model Contact that contains a field phone of type PhoneField (along with some other fields), then the output of dumpscript might contain:

    foo_contact_1.zip_code = ''
    foo_contact_1.phone = <phone_field.phone_number.PhoneNumber object at 0x000001BE2EACBF90>
    foo_contact_1.email = 'me@hotmail.com'

The intention, of course, would be that the .phone attribute is set to a value (presumably a string) that can then be saved to the database.

ericvsmith commented 9 months ago

I supposed the problem is that dumpscript is just too dumb. It has this code, after it checks for known field types: https://github.com/django-extensions/django-extensions/blob/0e5747586332333f572bcfbf674b712e11aa69dd/django_extensions/management/commands/dumpscript.py#L705C13-L707

    # A normal field (e.g. a python built-in)
    else:
        return repr(value)

So I guess the only solution on the django-phone-field side is to have __repr__ return repr(self.formatted), which would make the above:

    foo_contact_1.zip_code = ''
    foo_contact_1.phone = '(800) 555-1212"
    foo_contact_1.email = 'me@hotmail.com'

I'm not sure if that would break anything else. I'll play around with it.