Can't override address model py

97 views
Skip to first unread message

Nuruddin Iminohunov

unread,
Apr 21, 2016, 9:45:44 AM4/21/16
to django-oscar
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 *


I want to override UserAddress as in above, however,  I am getting following error



[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']



William Harris

unread,
Apr 21, 2016, 9:54:38 AM4/21/16
to django-oscar
"AttributeError: 'NoneType' object has no attribute 'iso_3166_1_a2'"

This says to me that your country is not getting created as you expect.

What is the value for self.country before you call super().save()?

Will

ak...@kuwaitnet.com

unread,
Mar 21, 2017, 3:54:20 AM3/21/17
to django-oscar
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)
Reply all
Reply to author
Forward
0 new messages