3

I am getting an error on get_or_create method of the Django model that contains PhoneNumberField(django-phonenumber-field==1.3.0) field.

It was working fine before, therefore I tried several older django-phonenumber-field(1.2.0, 1.1.0, 1.0.0) versions but the result was the same on all of them.

trying to do

TestModel.objects.get_or_create(name='Test name')

models.py

from django.utils.translation import get_language, ugettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from django.db import models

class TestModel(models.Model):
    name = models.CharField(max_length=20, blank=True,
                            null=True)
    phone = PhoneNumberField(
        _('Phone'), blank=True, unique=True, null=True,
          help_text=_('Enter phone number...')
    )

traceback

<ipython-input-3-03bfe9e47b71> in <module>()
----> 1 TestModel.objects.get_or_create(name='test')

/usr/local/lib/python3.5/site-packages/django/db/models/manager.py in manager_method(self, *args, **kwargs)
     83         def create_method(name, method):
     84             def manager_method(self, *args, **kwargs):
---> 85                 return getattr(self.get_queryset(), name)(*args, **kwargs)
     86             manager_method.__name__ = method.__name__
     87             manager_method.__doc__ = method.__doc__

/usr/local/lib/python3.5/site-packages/django/db/models/query.py in get_or_create(self, defaults, **kwargs)
    457         specifying whether an object was created.
    458         """
--> 459         lookup, params = self._extract_model_params(defaults, **kwargs)
    460         # The get() needs to be targeted at the write database in order
    461         # to avoid potential transaction consistency problems.

/usr/local/lib/python3.5/site-packages/django/db/models/query.py in _extract_model_params(self, defaults, **kwargs)
    519         params = {k: v for k, v in kwargs.items() if LOOKUP_SEP not in k}
    520         params.update(defaults)
--> 521         property_names = self.model._meta._property_names
    522         invalid_params = []
    523         for param in params:

/usr/local/lib/python3.5/site-packages/django/utils/functional.py in __get__(self, instance, cls)
     33         if instance is None:
     34             return self
---> 35         res = instance.__dict__[self.name] = self.func(instance)
     36         return res
     37 

/usr/local/lib/python3.5/site-packages/django/db/models/options.py in _property_names(self)
    885         return frozenset({
    886             attr for attr in
--> 887             dir(self.model) if isinstance(getattr(self.model, attr), property)
    888         })

/usr/local/lib/python3.5/site-packages/django/db/models/options.py in <setcomp>(.0)
    885         return frozenset({
    886             attr for attr in
--> 887             dir(self.model) if isinstance(getattr(self.model, attr), property)
    888         })

/usr/local/lib/python3.5/site-packages/phonenumber_field/modelfields.py in __get__(self, instance, owner)
     31             raise AttributeError(
     32                 "The '%s' attribute can only be accessed from %s instances."
---> 33                 % (self.field.name, owner.__name__))
     34         return instance.__dict__[self.field.name]
     35 

AttributeError: The 'phone' attribute can only be accessed from TestModel instances.
The Hog
  • 889
  • 10
  • 26
  • What is the first argument `_('Phone')` in the `phone` field supposed to be? You aren't assigning it to a property like `label` or `help_text`; it's just there on its own. – John Gordon Jun 04 '17 at 15:36
  • 1
    Could you paste django version? – wpedrak Jun 04 '17 at 15:39
  • Django version 1.11.2. Production works and it uses Django 1.11.1, I am going to test it on local – The Hog Jun 04 '17 at 15:45
  • _('Phone') is a name of the field, I just tried to get_or_create without it and the result is the same – The Hog Jun 04 '17 at 15:47
  • So maybe it have something in common with red cross near django=1.11, python=3.5 here https://travis-ci.org/stefanfoulis/django-phonenumber-field (I'm rather asking than answering) – wpedrak Jun 04 '17 at 15:57

1 Answers1

1

Error occurs on Django version 1.11.2. Django 1.11.1 works fine.

The Hog
  • 889
  • 10
  • 26