Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 358423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:17:53+00:00 2026-05-12T12:17:53+00:00

i’m experimenting with django and the builtin admin interface. I basically want to have

  • 0

i’m experimenting with django and the builtin admin interface.

I basically want to have a field that is a drop down in the admin UI. The drop down choices should be all the directories available in a specified directory.
If i define a field like this:

test_folder_list = models.FilePathField(path=/some/file/path)

it shows me all the files in the directory, but not the directories.

Does anyone know how i can display the folders?

also i tried doing

test_folder_list = models.charField(max_length=100, choices=SOME_LIST)

where SOME_LIST is a list i populate using some custom code to read the folders in a directory. This works but it doesn’t refresh. i.e. the choice list is limited to a snapshot of whatever was there when running the app for the first time.

thanks in advance.


update:
after some thinking and research i discovered what i want may be to either
1. create my own widget that is based on forms.ChoiceField
or
2. pass my list of folders to the choice list when it is rendered to the client

for 1. i tried a custom widget.
my model looks like

class Test1(models.Model):
    test_folder_ddl  = models.CharField(max_length=100)

then this is my custom widget:

class FolderListDropDown(forms.Select):
 def __init__(self, attrs=None, target_path):
  target_folder = '/some/file/path'
  dir_contents = os.listdir(target_folder)
  directories = []
  for item in dir_contents:
   if os.path.isdir(''.join((target_folder,item,))):
    directories.append((item, item),)
  folder_list = tuple(directories)
  super(FolderListDropDown, self).__init__(attrs=attrs, choices=folder_list)

then i did this in my modelForm

class test1Form(ModelForm):
    test_folder_ddl  = forms.CharField(widget=FolderListDropDown())

and it didn’t seem to work.What i mean by that is django didn’t want to use my widget and instead rendered the default textinput you get when you use a CharField.

for 2. I tried this in my ModelForm

class test1Form(ModelForm):
    test_folder_ddl = forms.CharField(widget=FolderListDropDown()) 
    test_folder_ddl.choices = {some list}

I also tried
class test1Form(ModelForm):
test_folder_ddl = forms.ChoiceField(choices={some list})

and it would still render the default char field widget.
Anyone know what i’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:17:54+00:00Added an answer on May 12, 2026 at 12:17 pm

    Yay solved. after beating my head all day and going through all sorts of examples by people i got this to work.

    basically i had the right idea with #2. The steps are
    – Create a ModelForm of our model
    – override the default form field user for a models.CharField. i.e. we want to explcitly say use a choiceField.
    – Then we have to override how the form is instantiated so that we call the thing we want to use to generate our dynamic list of choices
    – then in our ModelAdmin make sure we explicitly tell the admin to use our ModelForm

    class Test1(models.Model):
        test_folder_ddl  = models.CharField(max_length=100)
    
    
    class Test1Form(ModelForm):
        test_folder_ddl = forms.choiceField()
    
        def __init__(self, *args, **kwargs):
           super(Test1Form, self).__init__(*args, **kwargs)
           self.fields['test_folder_ddl'].choices = utility.get_folder_list()
    
    class Test1Admin(admin.ModelAdmin):
        form = Test1Form
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You actually shouldn't iterate over the histogram and for every… May 13, 2026 at 1:20 am
  • Editorial Team
    Editorial Team added an answer With ./image.0, you tell the shell to execute the image.… May 13, 2026 at 1:20 am
  • Editorial Team
    Editorial Team added an answer You could start with something like this: class TrackedFile(file): def… May 13, 2026 at 1:20 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.