I have a model called Document that has a FileField for uploading files.

class Document(models.Model):
    document_id = models.AutoField(primary_key=True)
    title = models.CharField('title', max_length=200)
    description = models.TextField('description')
    storage_file_name = models.FileField('File name',
upload_to=unique_file_path)
    computed_sha256 = models.CharField(editable=False, max_length=64)
    .....

I am trying to validate the storage_file_name field in the Admin form. The
validation logic is as follows:

If there is another Document with the same computed_sha256, and this is a
new Document, then raise a ValidationError.

The tricky part is the "and this is a new Document".

In the admin.py, I have a ModelForm called DocumentForm.

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document
        fields = "__all__"

    def clean_storage_file_name(self):
        cleaned_data = self.cleaned_data
        computed_sha256 =
compute_sha256(cleaned_data.get("storage_file_name"))
        duplicates =
Document.objects.filter(computed_sha256__exact=computed_sha256)
        current_doc_id = cleaned_data.get('document_id')
        for d in duplicates:
            if (existing_doc_id != d.document_id):
                raise forms.ValidationError("The image '%s' is already in
the database" % (cleaned_data.get("storage_file_name")))
        return self.cleaned_data['storage_file_name']

The current_doc_id is always None, whether I am uploading a new file or I
am editing a current file already in the database. I can read all the other
document fields, but not the document_id field. If I am updating an
existing document, then the current_document_id should exist and be equal
to the d.document_id (the id of the document found in the search).

I can't figure out why the current_document_id is always None. What am I
missing here?

How else can I block the ValidationError when I am merely updating another
Document field, because that also returns a duplicate image based on the
computed_sha256.

Thanks!

Mark

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEqej2OZLydOuN9xbdrk6LtMC%3DMxdy4hbL7CsT-_pCt9bgKUYA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to