Django forms - is there a way to dynamically edit a FilePathField?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a django form which I am using to access a directory. However I would like to access two different directories based on a given input, but have one FilePathField.



As an example - I have two panels - 'panel1' and 'panel2'. The directory I would like to access is the analysis directory of each of these panels as such:



/path/destination/panel1/analysis/
/path/destination/panel2/analysis/


In each of these analysis directories are directories starting with "Experiment" which I would like a user to be able to choose to obtain some results.



I have my form:



class RunUploadForm(forms.Form):

directory_path = forms.FilePathField(
path='/path/destination/panel1/analysis',
required=True,
allow_folders=True,
allow_files=False,
recursive=True,
match="Experiment*",
label="Pick folder for results:"
)


class Meta:
fields = ('directory_path',)


This only allows the user to access panel1 directory as it is hardcoded into the path arg. Is there a way to dynamically change this path argument, maybe with a choicefield?










share|improve this question





























    0















    I have a django form which I am using to access a directory. However I would like to access two different directories based on a given input, but have one FilePathField.



    As an example - I have two panels - 'panel1' and 'panel2'. The directory I would like to access is the analysis directory of each of these panels as such:



    /path/destination/panel1/analysis/
    /path/destination/panel2/analysis/


    In each of these analysis directories are directories starting with "Experiment" which I would like a user to be able to choose to obtain some results.



    I have my form:



    class RunUploadForm(forms.Form):

    directory_path = forms.FilePathField(
    path='/path/destination/panel1/analysis',
    required=True,
    allow_folders=True,
    allow_files=False,
    recursive=True,
    match="Experiment*",
    label="Pick folder for results:"
    )


    class Meta:
    fields = ('directory_path',)


    This only allows the user to access panel1 directory as it is hardcoded into the path arg. Is there a way to dynamically change this path argument, maybe with a choicefield?










    share|improve this question

























      0












      0








      0


      1






      I have a django form which I am using to access a directory. However I would like to access two different directories based on a given input, but have one FilePathField.



      As an example - I have two panels - 'panel1' and 'panel2'. The directory I would like to access is the analysis directory of each of these panels as such:



      /path/destination/panel1/analysis/
      /path/destination/panel2/analysis/


      In each of these analysis directories are directories starting with "Experiment" which I would like a user to be able to choose to obtain some results.



      I have my form:



      class RunUploadForm(forms.Form):

      directory_path = forms.FilePathField(
      path='/path/destination/panel1/analysis',
      required=True,
      allow_folders=True,
      allow_files=False,
      recursive=True,
      match="Experiment*",
      label="Pick folder for results:"
      )


      class Meta:
      fields = ('directory_path',)


      This only allows the user to access panel1 directory as it is hardcoded into the path arg. Is there a way to dynamically change this path argument, maybe with a choicefield?










      share|improve this question














      I have a django form which I am using to access a directory. However I would like to access two different directories based on a given input, but have one FilePathField.



      As an example - I have two panels - 'panel1' and 'panel2'. The directory I would like to access is the analysis directory of each of these panels as such:



      /path/destination/panel1/analysis/
      /path/destination/panel2/analysis/


      In each of these analysis directories are directories starting with "Experiment" which I would like a user to be able to choose to obtain some results.



      I have my form:



      class RunUploadForm(forms.Form):

      directory_path = forms.FilePathField(
      path='/path/destination/panel1/analysis',
      required=True,
      allow_folders=True,
      allow_files=False,
      recursive=True,
      match="Experiment*",
      label="Pick folder for results:"
      )


      class Meta:
      fields = ('directory_path',)


      This only allows the user to access panel1 directory as it is hardcoded into the path arg. Is there a way to dynamically change this path argument, maybe with a choicefield?







      django forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 at 16:02









      trouselifetrouselife

      382318




      382318
























          1 Answer
          1






          active

          oldest

          votes


















          0














          One way of doing so would be to pass the path as an argument to the __init__ method of the form, for example:



          class RunUploadForm(forms.Form):

          directory_path = forms.FilePathField(
          path='/path/destination/panel1/analysis',
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          class Meta:
          fields = ('directory_path',)

          def __init__(self, *args, **kwargs):
          path = kwargs.pop('path', 'somedefaultvalue')
          super().__init__(*args, **kwargs)
          self.fields['directory_path'] = forms.FilePathField(
          path=path,
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          You need to crate a new instance of FilePathField because choices for this kind of field are generated on __init__






          share|improve this answer
























          • This is just overwriting the path in init(), not changing the path dynamically. I'm trying to have a way of accessing more than one directory, based on an additional input (This could be all entries from my Panel model...)

            – trouselife
            Jan 4 at 16:36











          • As in, have some kind of radio button within the form which changes the path value.

            – trouselife
            Jan 4 at 16:37











          • Well, you can POST the value of that choice and as a response to that POST return the appropriate instace of the form. Other way would be to do that entirely in the frontend, for example having two different FilePathFields in the form and showing/hiding as you need.

            – ivissani
            Jan 4 at 16:40











          • Yes that could work thanks! Its difficult because when the post is submitted, i only want ONE value being used, but there doesnt seem to be a way of having a default null value..

            – trouselife
            Jan 4 at 17:09












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042363%2fdjango-forms-is-there-a-way-to-dynamically-edit-a-filepathfield%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          One way of doing so would be to pass the path as an argument to the __init__ method of the form, for example:



          class RunUploadForm(forms.Form):

          directory_path = forms.FilePathField(
          path='/path/destination/panel1/analysis',
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          class Meta:
          fields = ('directory_path',)

          def __init__(self, *args, **kwargs):
          path = kwargs.pop('path', 'somedefaultvalue')
          super().__init__(*args, **kwargs)
          self.fields['directory_path'] = forms.FilePathField(
          path=path,
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          You need to crate a new instance of FilePathField because choices for this kind of field are generated on __init__






          share|improve this answer
























          • This is just overwriting the path in init(), not changing the path dynamically. I'm trying to have a way of accessing more than one directory, based on an additional input (This could be all entries from my Panel model...)

            – trouselife
            Jan 4 at 16:36











          • As in, have some kind of radio button within the form which changes the path value.

            – trouselife
            Jan 4 at 16:37











          • Well, you can POST the value of that choice and as a response to that POST return the appropriate instace of the form. Other way would be to do that entirely in the frontend, for example having two different FilePathFields in the form and showing/hiding as you need.

            – ivissani
            Jan 4 at 16:40











          • Yes that could work thanks! Its difficult because when the post is submitted, i only want ONE value being used, but there doesnt seem to be a way of having a default null value..

            – trouselife
            Jan 4 at 17:09
















          0














          One way of doing so would be to pass the path as an argument to the __init__ method of the form, for example:



          class RunUploadForm(forms.Form):

          directory_path = forms.FilePathField(
          path='/path/destination/panel1/analysis',
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          class Meta:
          fields = ('directory_path',)

          def __init__(self, *args, **kwargs):
          path = kwargs.pop('path', 'somedefaultvalue')
          super().__init__(*args, **kwargs)
          self.fields['directory_path'] = forms.FilePathField(
          path=path,
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          You need to crate a new instance of FilePathField because choices for this kind of field are generated on __init__






          share|improve this answer
























          • This is just overwriting the path in init(), not changing the path dynamically. I'm trying to have a way of accessing more than one directory, based on an additional input (This could be all entries from my Panel model...)

            – trouselife
            Jan 4 at 16:36











          • As in, have some kind of radio button within the form which changes the path value.

            – trouselife
            Jan 4 at 16:37











          • Well, you can POST the value of that choice and as a response to that POST return the appropriate instace of the form. Other way would be to do that entirely in the frontend, for example having two different FilePathFields in the form and showing/hiding as you need.

            – ivissani
            Jan 4 at 16:40











          • Yes that could work thanks! Its difficult because when the post is submitted, i only want ONE value being used, but there doesnt seem to be a way of having a default null value..

            – trouselife
            Jan 4 at 17:09














          0












          0








          0







          One way of doing so would be to pass the path as an argument to the __init__ method of the form, for example:



          class RunUploadForm(forms.Form):

          directory_path = forms.FilePathField(
          path='/path/destination/panel1/analysis',
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          class Meta:
          fields = ('directory_path',)

          def __init__(self, *args, **kwargs):
          path = kwargs.pop('path', 'somedefaultvalue')
          super().__init__(*args, **kwargs)
          self.fields['directory_path'] = forms.FilePathField(
          path=path,
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          You need to crate a new instance of FilePathField because choices for this kind of field are generated on __init__






          share|improve this answer













          One way of doing so would be to pass the path as an argument to the __init__ method of the form, for example:



          class RunUploadForm(forms.Form):

          directory_path = forms.FilePathField(
          path='/path/destination/panel1/analysis',
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          class Meta:
          fields = ('directory_path',)

          def __init__(self, *args, **kwargs):
          path = kwargs.pop('path', 'somedefaultvalue')
          super().__init__(*args, **kwargs)
          self.fields['directory_path'] = forms.FilePathField(
          path=path,
          required=True,
          allow_folders=True,
          allow_files=False,
          recursive=True,
          match="Experiment*",
          label="Pick folder for results:"
          )


          You need to crate a new instance of FilePathField because choices for this kind of field are generated on __init__







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 16:28









          ivissaniivissani

          976610




          976610













          • This is just overwriting the path in init(), not changing the path dynamically. I'm trying to have a way of accessing more than one directory, based on an additional input (This could be all entries from my Panel model...)

            – trouselife
            Jan 4 at 16:36











          • As in, have some kind of radio button within the form which changes the path value.

            – trouselife
            Jan 4 at 16:37











          • Well, you can POST the value of that choice and as a response to that POST return the appropriate instace of the form. Other way would be to do that entirely in the frontend, for example having two different FilePathFields in the form and showing/hiding as you need.

            – ivissani
            Jan 4 at 16:40











          • Yes that could work thanks! Its difficult because when the post is submitted, i only want ONE value being used, but there doesnt seem to be a way of having a default null value..

            – trouselife
            Jan 4 at 17:09



















          • This is just overwriting the path in init(), not changing the path dynamically. I'm trying to have a way of accessing more than one directory, based on an additional input (This could be all entries from my Panel model...)

            – trouselife
            Jan 4 at 16:36











          • As in, have some kind of radio button within the form which changes the path value.

            – trouselife
            Jan 4 at 16:37











          • Well, you can POST the value of that choice and as a response to that POST return the appropriate instace of the form. Other way would be to do that entirely in the frontend, for example having two different FilePathFields in the form and showing/hiding as you need.

            – ivissani
            Jan 4 at 16:40











          • Yes that could work thanks! Its difficult because when the post is submitted, i only want ONE value being used, but there doesnt seem to be a way of having a default null value..

            – trouselife
            Jan 4 at 17:09

















          This is just overwriting the path in init(), not changing the path dynamically. I'm trying to have a way of accessing more than one directory, based on an additional input (This could be all entries from my Panel model...)

          – trouselife
          Jan 4 at 16:36





          This is just overwriting the path in init(), not changing the path dynamically. I'm trying to have a way of accessing more than one directory, based on an additional input (This could be all entries from my Panel model...)

          – trouselife
          Jan 4 at 16:36













          As in, have some kind of radio button within the form which changes the path value.

          – trouselife
          Jan 4 at 16:37





          As in, have some kind of radio button within the form which changes the path value.

          – trouselife
          Jan 4 at 16:37













          Well, you can POST the value of that choice and as a response to that POST return the appropriate instace of the form. Other way would be to do that entirely in the frontend, for example having two different FilePathFields in the form and showing/hiding as you need.

          – ivissani
          Jan 4 at 16:40





          Well, you can POST the value of that choice and as a response to that POST return the appropriate instace of the form. Other way would be to do that entirely in the frontend, for example having two different FilePathFields in the form and showing/hiding as you need.

          – ivissani
          Jan 4 at 16:40













          Yes that could work thanks! Its difficult because when the post is submitted, i only want ONE value being used, but there doesnt seem to be a way of having a default null value..

          – trouselife
          Jan 4 at 17:09





          Yes that could work thanks! Its difficult because when the post is submitted, i only want ONE value being used, but there doesnt seem to be a way of having a default null value..

          – trouselife
          Jan 4 at 17:09




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042363%2fdjango-forms-is-there-a-way-to-dynamically-edit-a-filepathfield%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas