2

I'm trying to use UUIDField as a primary key for a model. I'm using CreateView for creating objects for this model.

Anytime I browse to the url for creating one of the objects I get the error:

badly formed hexadecimal UUID string

The stack trace shows the error occurs here, where value is created:

/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

Here is my model:

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")

My view:

class InvoicePaymentCreateView(CreateView):
    model = InvoicePayment
    form_class = AddInvoicePaymentForm
    template_name = 'payment_add.html'

And my form (I'm using crispy forms):

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__'

What I've tried:

  • Figured it could have been an old schema issue: recreated database
  • Python version. Initially used Python 3.4.0, but issue persists in Python 2.7
  • Creating an object in the django shell, same uuid error
  • updating my Django from 1.8.3 to 1.8.4

None of this worked. Help!

cbrad
  • 163
  • 1
  • 15

0 Answers0