from django.db import models
from django.utils.translation import ugettext as _
from oscar.apps.address.abstract_models import AbstractUserAddress# noqa
class UserAddress(AbstractUserAddress):
def save(self, *args, **kwargs):
print('=========================================')
try:
self.country= Country.objects.get(pk='UZ')
except:
self.country = Country.objects.get_or_create(pk='UZ',iso_3166_1_a2='UZ',printable_name='Uzbekistan',name="Uzbekistan",is_shipping_country=True )
super(UserAddress, self).save(*args, **kwargs)
from oscar.apps.address.models import *
[21/Apr/2016 12:14:41] "POST /ru/accounts/addresses/add/ HTTP/1.1" 500 178848["AttributeError: 'NoneType' object has no attribute 'iso_3166_1_a2'",
' File "django/core/handlers/base.py", line 132, in get_response', '
File "python3.5/contextlib.py", line 30, in inner', '
File "django/contrib/auth/decorators.py", line 22, in _wrapped_view', '
File "django/views/generic/base.py", line 71, in view', '
File "django/views/generic/base.py", line 89, in dispatch', '
File "django/views/generic/edit.py", line 249, in post', '
File "django/views/generic/edit.py", line 214, in post', '
File "django/forms/forms.py", line 184, in is_valid', '
File "django/forms/forms.py", line 176, in errors', '
File "django/forms/forms.py", line 392, in full_clean', '
File "django/forms/forms.py", line 410, in _clean_fields', '
File "oscar/views/generic.py", line 185, in clean_phone_number', '
File "oscar/views/generic.py", line 170, in get_region_code']
This might solve your problem. Assign country value in init and save as well.
def __init__(self, *args, **kwargs):
super(MySecretForm, self).__init__(*args, **kwargs)self.instance.country = Country.objects.get(iso_3166_1_a2='Country_code')
def save(self, *args, **kwargs):
self.instance.country = Country.objects.get(iso_3166_1_a2='Country_code')
return super(MySecretForm, self).save(*args, **kwargs)