0

我用 sql-script 插入了这些数据:

INSERT INTO "siteDB_accounttype" VALUES
(1,'Micro'),
(2,'Standart'),
(3,'МТ-ECN');

并在 Django 中有此代码(使用 Python 2.7):
1)views.py

def registration(request):
    reg_form = RegisterForm()
    title = 'Регистрация клиента'
    return render_to_response('registration.html', locals())

2)forms.py

class RegisterForm(forms.Form):
    ids = AccountType.objects.values_list('Name', flat=True)
    acc_types_lst = AccountType.objects.filter(pk__in=set(ids))
    Name = forms.CharField(max_length=255, required=True, label='Имя')
    Surname = forms.CharField(max_length=255, required=True, label='Фамилия')
    DateOfBirth = forms.DateField(required=True, label='Дата рождения')
    Address = forms.CharField(max_length=255, required=True, label='Адрес',
                          widget=forms.Textarea(attrs={'cols': 55, 'rows': 5}))
    Postcode = forms.IntegerField(min_value=100000, max_value=999999, required=True,  label='Почтовый индекс')
    Phone = forms.CharField(max_length=50, required=True, label='Телефон')
    PassportID = forms.CharField(max_length=50, required=True, label='Номер паспорта')
    AccountType = forms.ChoiceField(choices=[AccType for AccType in acc_types_lst],
                                required=True, label='Тип счета')
    DepositeSize = forms.IntegerField(min_value=500, required=True, label='Начальный вклад')

3) 模型.py

from django.utils.encoding import smart_unicode  
class AccountType(models.Model):
    ID = models.IntegerField(primary_key=True, blank=False)
    Name = models.CharField(max_length=50, unique=True, blank=False)

    def __unicode__(self):
        return smart_unicode(self.Name)

但是在我的浏览器上:

ValueError at /registration/
invalid literal for int() with base 10: 'Micro'

完整追溯:

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  92.                     response = middleware_method(request)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/locale.py" in process_request
  21.         check_path = self.is_language_prefix_patterns_used()
File "/usr/local/lib/python2.7/dist-packages/django/middleware/locale.py" in is_language_prefix_patterns_used
  56.         for url_pattern in get_resolver(None).url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/relrin/code/CourseDB/CourseDB/urls.py" in <module>
  2. from siteDB.views import *
File "/home/relrin/code/CourseDB/siteDB/views.py" in <module>
  5. from siteDB.forms import *
File "/home/relrin/code/CourseDB/siteDB/forms.py" in <module>
  6. class RegisterForm(forms.Form):
File "/home/relrin/code/CourseDB/siteDB/forms.py" in RegisterForm
  8.     acc_types_lst = AccountType.objects.filter(pk__in=set(ids))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in filter
  155.         return self.get_query_set().filter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in filter
  655.         return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _filter_or_exclude
  673.             clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_q
  1266.                             can_reuse=used_aliases, force_having=force_having)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_filter
  1197.                 connector)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/where.py" in add
  71.             value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/where.py" in prepare
  339.             return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  997.         return super(IntegerField, self).get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  324.             return [self.get_prep_value(v) for v in value]
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_value
  991.         return int(value)

Exception Type: ValueError at /
Exception Value: invalid literal for int() with base 10: 'MT-ECN'

我该如何解决这个问题?

4

1 回答 1

2

您的代码中似乎有错字,请更改

class RegisterForm(forms.Form):
     ids = AccountType.objects.values_list('Name', flat=True)

class RegisterForm(forms.Form):
     ids = AccountType.objects.values_list('id', flat=True)

由于打字错误,您稍后按Names(字符串)而不是IDs(整数)过滤帐户类型列表,

acc_types_lst = AccountType.objects.filter(pk__in=set(ids))

因此错误。

于 2013-10-26T18:27:07.033 回答