0
我有一個模型,我試圖重寫保存,但是當我通過它認爲該字符串是一個元組的信息,並不會保存信息該模型。我正在使用的特定字段是單行地址,如下所示:3107 Eric Motorway, Lake William, GU 71954
,但我的模型認爲逗號是一個元組。我試過把它轉換成一個字符串和一切。模型認爲我的字符串是元組,並不會保存
Models.py:
class AddressBook(models.Model):
address = models.CharField(max_length=250, unique=True)
name = models.CharField(max_length=250)
def __str__(self):
return self.address
class Freight(models.Model):
pu_location = models.OneToOneField(AddressBook, to_field='address', related_name='pu_location')
pu_customer = models.CharField(max_length=200)
pu_appt_time = models.DateTimeField(default=datetime.now)
po_number = models.CharField(max_length=20)
load_number = models.CharField(max_length=10, blank=True, null=True)
pallet_count = models.IntegerField(validators=[MaxValueValidator(999)])
content_type = models.CharField(max_length=20, choices=[('Frozen', 'Frozen'), ('Chilled', 'Chilled'), ('Dry', 'Dry')])
cases_count = models.IntegerField(validators=[MaxValueValidator(9999)])
weight = models.IntegerField(validators=[MaxValueValidator(99999)])
del_customer = models.CharField(max_length=200)
del_location = models.OneToOneField(AddressBook, to_field='address', related_name='del_location')
del_city = models.CharField(max_length=50, blank=True, null=True)
del_state = models.CharField(max_length=2, blank=True, null=True)
del_appt_time = models.DateTimeField(default=datetime.now)
invoice_amount = models.FloatField(default=0.00, validators=[MinValueValidator(0.00), MaxValueValidator(999999.00)])
def save(self, *args, **kwargs):
reg = re.compile(r',+?(?P<city>[^,]+),\s+(?P<state>[A-Za-z]{2})')
print(self.del_location)
extract = reg.search(self.del_location)
city = extract.group('city')
state = extract.group('state')
if not self.del_state:
self.del_city = city
if not self.del_city:
self.del_state = state
def __str__(self):
return self.po_number, self.pu_customer, self.del_location
回溯信息:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/dispatch/freight/add/
Django Version: 1.11.2
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'imagekit',
'rolepermissions',
'django_messages',
'taggit',
'dispatch']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'dispatch.activeuser_middleware.ActiveUserMiddleware']
Traceback:
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
551. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
224. return view(request, *args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view
1508. return self.changeform_view(request, None, form_url, extra_context)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view
1408. return self._changeform_view(request, object_id, form_url, extra_context)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view
1452. self.log_addition(request, new_object, change_message)
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/contrib/admin/options.py" in log_addition
724. object_repr=force_text(object),
File "/home/jboucher/anaconda3/envs/openroad/lib/python3.6/site-packages/django/utils/encoding.py" in force_text
76. s = six.text_type(s)
Exception Type: TypeError at /admin/dispatch/freight/add/
Exception Value: __str__ returned non-string (type tuple)
錯誤停止,但它不保存任何內容。 –
它的工作...我忘記了我的超級聲明底部 –
'__str__'內置方法不需要'超',不是它保存對象雖然 –