dynamic FilePathField question

后端 未结 4 1755
臣服心动
臣服心动 2020-12-18 15:48

I have a model where the location of pdf directory I\'m pointing to with my FilePathField is based on the \"client\" and \"job_number\" fields.

class CCEntry         


        
4条回答
  •  一生所求
    2020-12-18 16:35

    Anyone interested in extending FilePathField to include this feature?

    I'd love to see this extension!

    Just for the record, this is the solution that worked for me (django 1.3):

        # models.py
        class Analysis(models.Model):
            run = models.ForeignKey(SampleRun)
            # Directory name depends on the foreign key 
            # (directory was created outside Django and gets filled by a script)
            bam_file = models.FilePathField(max_length=500, blank=True, null=True)  
    
        # admin.py
        class CustomAnalysisModelForm(forms.ModelForm):
            class Meta:
                model = Analysis
            def __init__(self, *args, **kwargs):
                super(CustomAnalysisModelForm, self).__init__(*args, **kwargs)
                # This is an update
                if self.instance.id:
                    # set dynamic path
                    mypath = settings.DATA_PATH + self.instance.run.sample.name
                    self.fields['bam_file'] = forms.FilePathField(path=mypath, match=".*bam$", recursive=True)
    
        class AnalysisAdmin(admin.ModelAdmin):
            form = CustomAnalysisModelForm
    

    Hope this helps somebody out there.

提交回复
热议问题