Closed BenQuigley closed 2 years ago
Hi @BenQuigley, thanks for the PR, it's very much appreciated!
I wonder whether we actually want to force setting the value of formatted
to raw
during the creation of the address (when formatted
is empty, of course) as opposed to when returning values to the UI? It probably makes sense to require formatted
to exist, guaranteeing a human readable value at all times. What are your thoughts?
Hm, looks like that is already happening in the _to_python
function. Sure, that sounds good to me
The widget for this app, in
address.widgets.AddressWidget.render
, prepopulates the address form with the value ofaddress.formatted
if it exists, but otherwise it stays blank.However, an address can be created with just
address.models.Address.create(address="north pole")
, and this populates theAddress.raw
value and nothing else. So making a widget using an address like that as its instance / initial value results in a weird state where the widget was created correctly, but there is no initial value in the widget.The
__str__
method of the Address model contains logic that addresses this: it returns theformatted
value if one exists, otherwise it tries to construct one from thelocality
fields, otherwise it falls back to theraw
value. Using this method to populate theformatted
value into_dict
fixes this problem of the widget.