如何在django中允许空表单字段

时间:2016-06-22 01:11:41

标签: python django forms

我目前正在处理我正在处理的问题。我想允许空的uuid字段保存到数据库。

这是我的代码

models.py

mission_id = models.UUIDField(null=True, blank=True)

forms.py

class SomeForm(ModelForm):    
    class Meta:    
        model = SomeModel
        widgets = {
            'mission_id': TextInput(),
        }

但我收到了错误消息

Enter a valid UUID.

1 个答案:

答案 0 :(得分:0)

由于UUID字段使用Python's UUID class,因此问题出现here

示例:

Public Shared Function GetAttribute(ByVal username As String, ByVal pwd As String) As UserInfo
        Dim objUserInfo As New UserInfo
        Dim ObjFirstName As String = ""
        Dim ObjLastName As String = String.Empty
        Dim ObjEmail As String = ""
        Dim objDepartment As String = ""
        Dim Success As Boolean = False

        Dim LDAPAddress As String = ConfigurationSettings.AppSettings.Get("LDAPAddress")
        Dim Entry As New System.DirectoryServices.DirectoryEntry(LDAPAddress, username, pwd)
        Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
        Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel
        Dim Filter As String = "(samAccountName=" & username & ")"
        Dim findUser As DirectorySearcher = New DirectorySearcher(Entry, Filter)
        Dim results As SearchResultCollection = findUser.FindAll


        Try
            Dim Resultsx As System.DirectoryServices.SearchResult = Searcher.FindOne
            Success = Not (Resultsx Is Nothing)

            findUser.PropertiesToLoad.Add("name")
            Dim name As String = DirectCast(Resultsx.Properties(name)(0), String)

            Dim de As System.DirectoryServices.DirectoryEntry = Resultsx.GetDirectoryEntry()
            Dim gg = de.Properties.PropertyNames()

            For Each Onn As String In gg
                Dim str As String = String.Format("{0}", Onn)
            Next

            Try
                ObjFirstName = de.Properties("GivenName").Value.ToString()


                ObjEmail = de.Properties("mail").Value.ToString()

                ObjLastName = de.Properties("sn").Value.ToString()
                objDepartment = de.Properties("department").Value.ToString()

            Catch ex As Exception
                ObjFirstName = de.Properties("DisplayName").Value.ToString()
            End Try

---&GT; UUID字段不能是>>> import uuid >>> uuid.UUID('') Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> uuid.UUID('') File "C:\Python27\lib\uuid.py", line 134, in __init__ raise ValueError('badly formed hexadecimal UUID string') ValueError: badly formed hexadecimal UUID string >>>

解决方案:1。将其设为另一种Field类型或2.将某些内容传递给Field