How do I get UUID FIELD value in Django ModelFORM?

Viewed 286

I am doing something fairly straightforward...I am creating a record with a unique identifier using the UUID field in my POSTRGRESQL database. It's working fine. I'm trying to use that value and do a compare when the user is updating records. However, even though the value is in the database, when I try to get it via a ModelFORM in my CLEAN it is showing up as NONE. I'm guessing there's a special way to get this value but I can't figure out what it is. I tried to use this as a reference....https://stackoverflow.com/questions/54914961/django-form-with-a-modelchoicefield-on-a-uuid-do-not-show-its-content-in-cleaned but I can't work it out.

My Model...

class Model(models.Model):


unique_identifier = models.UUIDField(primary_key=False, default=uuid.uuid4, editable=False)

My FORM...

class FORM(forms.ModelForm):

    class Meta:
        model = Model

    def clean(self):
        cleaned_data = super(UpdateSavedNewProcedureForm, self).clean()  
        unique_identifier = cleaned_data.get('unique_identifier')
        print(unique_identifier) 

When I print the unique_identifier it comes back as NONE even though I can see the value in the database. If I store the value as a CHARFIELD I can get the value...but I'm trying to avoid duplicating this field just for readability. I'm certain there's a special way to get this value but I'm stumped at the moment.

Thanks in advance for any thoughts.

1 Answers

My final answer thanks to @Willem Van Onsem

My Model...

class Model(models.Model):

    unique_identifier = models.UUIDField(primary_key=False, 
    default=uuid.uuid4, editable=False)

My Form...

class FORM(forms.ModelForm):

    class Meta:
        model = Model

    def clean(self):
        cleaned_data = super(UpdateSavedNewProcedureForm, self).clean()  
        unique_identifier = self.instance.unique_identifier        

    If unique_identifier:
        Do Something Cool.....
Related