Uuidfield's experience with Django model

Source: Internet
Author: User
Tags django website

This afternoon when the database was imported from the old Library to the new library, it was completed that the Django website could not be opened, reported "ValueError, badly formed hexadecimal UUID string", Finally, the reason is that a value of 0 for a Uuidfield type field causes Django to fail to validate 0 as the UUID type, causing the ValueError exception to be thrown. The following is a summary of Uuidfield's experience with Django model.

A field of type Uuidfield in Django can be used as a primary key (the primary key is absolutely impossible to null), which is fine, but if the other non-primary key fields use the Uuidfield type, It is best to set the default value of this field to the None type in Python, the Default=none, which is set up as follows:

Uuidfield example of setting the primary key:

IDAPPASSWD = models. Uuidfield (Primary_key=true, Auto_created=true, Default=uuid.uuid4, Editable=false)

When the non-empty field type is Uuidfield, you must set the value of default= a UUID, either uuid4 () or another UUID value, as set out in the following example:

Appuuid = models. Uuidfield (Default=uuid.uuid4, Null=false, verbose_name=u ' app uuid ', he lp_text= "App uuid")

You can set an example of an empty Uuidfield field type:

Associatedappuuid = models.                                     Uuidfield (Default=none, Null=true, blank=true, verbose_name=u ' associated uuid ', help_text= "associated app uuid")


Code Note: In the above three lines of code, IDAPPASSWD is used as the primary key, Appuuid is the app's UUID cannot be empty, Associatedappuuid as the app's associated UUID, if there is no association, so can be empty.
Use Note:
1. In the MySQL database, the Uuidfield type must be a 32-bit char type, in the database model, the developer does not need to set the MAX_LENGTH=XXX, because the value of this max_length default must be 32.
2. If the type of a field is Uuidfield and is set to NULL, it is best to set it to Null=true, where the value of this field cannot be null ("') nor 0 (number 0), and it is recommended to set Default=none.
3. The above content has been tested on Django 1.10.3
Because Uuidfield's content is not none, it will be rigorously checked by Django (this should not be considered a bug or issue), and the Code for verification is as follows:
django/db/backends/mysql/operations.py 211 rows or so:

def convert_uuidfield_value (self, value, expression, connection, context): If value is not None:value = uuid. UUID (value) return value

If value is not none, the UUID is executed. The UUID () function, if the parameter value is not none, raise an exception valueerror badly formed hexadecimal UUID string in __init__ in the uuid.py module.
Summarize:
1. The main point of troubleshooting is to constantly reduce the scope of the problem, we must use the exclusion method, this must always bear in mind.
2. If the type of a field is Uuidfield and is set to NULL, it is best to set it to Null=true, and it is recommended to set Default=none.
Tag:django Uuidfield, Django valueerror, badly formed hexadecimal UUID string
--end--


This article from "Communication, my favorite" blog, declined to reprint!

Uuidfield's experience with Django model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.