Ich habe ein Modell, das ich überschreiben möchte, speichern, aber wenn ich die Informationen übergeben, denkt es, dass die Zeichenfolge ein Tupel ist, und speichert die Informationen nicht das Model. Das bestimmte Feld, das ich einziehe, ist eine einzelne Zeilenadresse, die wie folgt aussieht: 3107 Eric Motorway, Lake William, GU 71954
, aber mein Modell denkt, dass die Kommas ein Tupel sind. Ich habe versucht, es zu einer Schnur und alles zu werfen.Modell denkt, dass meine Zeichenfolge Tupel ist und nicht speichern
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
Zurückverfolgungs Info:
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)
Fehler gestoppt, aber es ist nichts zu retten. –
Es hat funktioniert ... Ich habe meine Super-Anweisung am Ende –
vergessen die '__str__' eingebaute Methode erfordert kein' Super', nicht speichert es Objekte –