Python Django主键:格式错误的十六进制UUID字符串
我试图使用Python Django主键:格式错误的十六进制UUID字符串,python,django,django-models,django-views,Python,Django,Django Models,Django Views,我试图使用UUIDField作为模型的主键。我正在使用CreateView为这个模型创建对象 每当我浏览到用于创建其中一个对象的url时,我都会收到错误: 格式错误的十六进制UUID字符串 堆栈跟踪显示此处发生的错误,其中创建了值: /home/conor/django/venv2/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_db_prep_value return n
UUIDField
作为模型的主键。我正在使用CreateView
为这个模型创建对象
每当我浏览到用于创建其中一个对象的url时,我都会收到错误:
格式错误的十六进制UUID字符串
堆栈跟踪显示此处发生的错误,其中创建了值:
/home/conor/django/venv2/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_db_prep_value
return name, path, args, kwargs
def get_internal_type(self):
return "UUIDField"
def get_db_prep_value(self, value, connection, prepared=False):
if isinstance(value, six.string_types):
value = uuid.UUID(value.replace('-', ''))
...
if isinstance(value, uuid.UUID):
if connection.features.has_native_uuid_field:
return value
return value.hex
return value
这是我的模型:
class InvoicePayment(models.Model):
invoice_payment_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
invoice = models.ForeignKey('Invoice')
date_paid = models.DateField(auto_now=False, auto_now_add=False, blank=False, null=True)
payment_type = models.CharField(max_length=100)
amount = models.DecimalField(max_digits=9, decimal_places=3)
balance = models.DecimalField(max_digits=9, decimal_places=3)
def __str__(self):
return '%s' % (self.invoice_payment_id)
class Meta:
verbose_name = _("Invoice Payment")
verbose_name_plural = _("Invoice Payments")
我的看法是:
class InvoicePaymentCreateView(CreateView):
model = InvoicePayment
form_class = AddInvoicePaymentForm
template_name = 'payment_add.html'
和我的表格(我用的是脆表格):
class AddInvoicePaymentForm(ModelForm):
helper=FormHelper()
helper.layout=布局(
HTML(“发票付款”),
Div(
部门(‘发票付款’、‘付款类型’,
“金额”、“余额”,
字段('date_paid',css_class='datepicker'),
css_class='col-md-6 col-md-offset-1',
css_class='row'),
形态(
提交(“保存”、“保存更改”),
按钮(“取消”、“取消”)
),
)
类元:
型号=发票付款
字段='\uuuu所有\uuuu'
我所尝试的:
- 我想这可能是一个旧的模式问题:重新创建数据库
- Python版本。最初使用的是Python3.4.0,但问题在Python2.7中仍然存在
- 在django shell中创建对象时,出现相同的uuid错误
- 将我的Django从1.8.3更新到1.8.4
class AddInvoicePaymentForm(ModelForm):
helper = FormHelper()
helper.layout = Layout(
HTML("<legend>Invoice Payment</legend>"),
Div(
Div('invoice_payment', 'payment_type',
'amount', 'balance',
Field('date_paid', css_class='datepicker'),
css_class='col-md-6 col-md-offset-1'),
css_class='row'),
FormActions(
Submit('save', 'Save changes'),
Button('cancel', 'Cancel')
),
)
class Meta:
model = InvoicePayment
fields = '__all__'