Django主键:形成错误的十六进制UUID字符串

时间:2015-09-19 00:39:26

标签: python django django-models django-views

我正在尝试使用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__'

我尝试了什么:

  • 认为它可能是一个旧的架构问题:重新创建的数据库
  • Python版本。最初使用Python 3.4.0,但问题仍然存在于Python 2.7
  • 在django shell中创建一个对象,同样的uuid错误
  • 将我的Django从1.8.3更新为1.8.4

这些都不起作用。救命啊!

0 个答案:

没有答案