i have model like
class infofromfile(models.model): name = models.charfield() title = models.charfield(max_length=200) date_from_file = models.datefield() file = models.filefield() file_hash = models.charfield(unique=true) with corresponding admin class
class infofromfileadmin(admin.modeladmin): fieldsets = [ ('information source', {'fields': ['file']}), ] def save_model(self, request, obj, form, change): # custom action here extract info file # before creating , saving model i have separate function creating hash of file.
so, input field in form file upload. want prevent duplicate files uploaded @ all, is, before saving model part of form/model validation. if model attempted saved , hash exists, integrityerror thrown.
i know model.validate_unique() method, don't know how apply in modeladmin.save_model(*) method.
to summarize, question is:
how can validate uniqueness in different field 1 provided model form? should make use of javascript create hash of file before admin-user hits save button? or make use of other hidden fields?
edit: future expansion site allow users upload such files (or same processing resulting in output pdf-file, possibly without saving user's file), albeit not needed now.
i think need custom form. can validate whatever want in clean_<field> method of form. should similar this:
class infofromfileform(modelform): def clean_file(self): file_obj = self.cleaned_data['file'] hash = get_hash() # whatever use file hash if infofromfile.objects.filter(file_hash=hash).exists(): raise validationerror(_("the hash must unique.") return file_obj then in admin class
class infofromfileadmin(admin.modeladmin): # things form = infofromfileform # more things
Comments
Post a Comment