模型认为我的字符串是元组并且不会保存

时间:2017-06-29 21:56:48

标签: django django-models

我有一个模型,我试图覆盖保存,但当我传递信息时,它认为字符串是一个元组,并且不会将信息保存在模型中。我所引入的特定字段是单行地址,如下所示: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)

1 个答案:

答案 0 :(得分:1)

您的__str__是一个元组。你的版本:

def __str__(self):
    return self.po_number, self.pu_customer, self.del_location

您可以尝试类似

的内容
def __str__(self):
    return "%s %s %s" % (self.po_number, self.pu_customer, self.del_location)

它应该可以正常工作:)