我正在尝试使用UUIDField
作为模型的主键。我正在使用CreateView
为此模型创建对象。
我随时浏览到创建其中一个对象的网址时出错:
badly formed hexadecimal UUID string
堆栈跟踪显示此处发生的错误,其中创建了值:
/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 = 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__'
我尝试了什么:
这些都不起作用。救命啊!