Skip to content

Commit

Permalink
Merge branch 'release/0.2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
banagale committed Jun 28, 2020
2 parents e251edb + 9ff1768 commit a44165e
Show file tree
Hide file tree
Showing 15 changed files with 708 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [banagale]
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ db.sqlite3
media
dist/
*.egg-info/
.idea
.idea
.DS_Store
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ To simplify storage and access of addresses, a subclass of `ForeignKey` named
`AddressField` has been created. It provides an easy method for setting new
addresses.

## ON_DELETE behavior of Address Field

By default, if you delete an Address that is related to another object,
Django's [cascade behavior](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.on_delete)
is used. This means the related object will also be deleted. You may also choose
to set `null=True` when defining an address field to have the address set
to Null instead of deleting the related object. For more information and an example,
see the readme for the `django-address` example_site.

## Creation

It can be created using the same optional arguments as a ForeignKey field.
Expand Down
10 changes: 9 additions & 1 deletion address/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _to_python(value):
state_code = value.get('state_code', '')
locality = value.get('locality', '')
sublocality = value.get('sublocality', '')
postal_town = value.get('postal_town', '')
postal_code = value.get('postal_code', '')
street_number = value.get('street_number', '')
route = value.get('route', '')
Expand All @@ -47,6 +48,11 @@ def _to_python(value):
if not locality and sublocality:
locality = sublocality

# Fix issue with UK addresses with no locality
# (https://github.com/furious-luke/django-address/issues/114)
if not locality and postal_town:
locality = postal_town

# If we have an inconsistent set of value bail out now.
if (country or state or locality) and not (country and state and locality):
raise InconsistentDictError
Expand Down Expand Up @@ -301,7 +307,9 @@ class AddressField(models.ForeignKey):

def __init__(self, *args, **kwargs):
kwargs['to'] = 'address.Address'
kwargs['on_delete'] = models.CASCADE
# The address should be set to null when deleted if the relationship could be null
default_on_delete = models.SET_NULL if kwargs.get('null', False) else models.CASCADE
kwargs['on_delete'] = kwargs.get('on_delete', default_on_delete)
super(AddressField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name, virtual_only=False):
Expand Down
1 change: 1 addition & 0 deletions address/static/address/js/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $(function () {
'country_code',
'locality',
'postal_code',
'postal_town',
'route',
'street_number',
'state',
Expand Down
Loading

0 comments on commit a44165e

Please sign in to comment.