gpt4 book ai didi

python - get_or_create 方法上的 django-phone-number-field 错误

转载 作者:行者123 更新时间:2023-11-28 18:22:12 24 4
gpt4 key购买 nike

我在包含 PhoneNumberField(django-phonenumber-field==1.3.0) 字段的 Django 模型的 get_or_create 方法上收到错误。

它之前工作正常,因此我尝试了几个较旧的 django-phonenumber-field(1.2.0, 1.1.0, 1.0.0) 版本,但所有版本的结果都一样.

努力去做

TestModel.objects.get_or_create(name='测试名称')

模型.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...')
)

回溯

<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.

最佳答案

Django 1.11.2 版本出现错误。 Django 1.11.1 工作正常。

关于python - get_or_create 方法上的 django-phone-number-field 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44355600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com