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

  • SEARCH
  • Home
  • 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 9226707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:53:40+00:00 2026-06-18T04:53:40+00:00

I have a Django form that references a model called AutoPart which has a

  • 0

I have a Django form that references a model called AutoPart which has a field for storing a text file.

I’m then trying to use this form to upload an archive of text files, rather than just one text file. My script unzips the archive, and then adds each text file to the database.

My problem is that only ONE text file in the whole archive is being saved, rather than all of them. I’m wondering if this is because I’m overwriting something?

Here’s my code:

In views.py

def mass_upload(request):
    tmpdir = tempfile.mkdtemp() 
    if request.method == "POST":
        formtoadd = massform(request.POST, request.FILES)
        if formtoaddpart.is_valid():
            zipped = zipfile.ZipFile(request.FILES['content'], 'r')
            zipped = zipped.extractall(tmpdir)
            for (dirpath, dirnames, filenames) in os.walk(tmpdir):
                if "__MACOSX" in dirnames:
                    dirnames.remove("__MACOSX")
                for filename in filenames:     
                    new_model = formtoaddpart.save(commit=False)                    
                    file = open(dirpath + "/" + filename, 'rb')                                                                                                      
                    filecontent = file.read()                                                                                                                              
                    file.seek(0)                                                                                                                                           
                    new_model.modelname = os.path.splitext(filename)[0]#.replace(" ", "")                                                                                      
                    modelname = new_model.modelname                                                                                                                          
                    manufacturer = new_model.manufacturer                                                                                                                  
                    new_model.adder = request.user                                                                                                                         
                    filetype = new_model.type                                                                                                                                  
                    format = new_model.format                                                                                                                              
                    adder_id = new_model.adder.id                                                                                                                          
                    new_model.content=store_in_s3(filename, filecontent, filetype, modelname, format, manufacturer, adder_id)                                                  
                    new_model.save()                                                   

In forms.py

class massform(ModelForm):
    def __init__(self, *args, **kwargs):
        super(massform, self).__init__(*args,**kwargs)
        self.is_update=False
        choices = UniPart.objects.all().values('manufacturer').distinct()
    modelname = forms.CharField (label="AutoPart", max_length=80, required= False)
    manufacturer = forms.CharField (label="Manufacturer", max_length=80, required= False)
    type = forms.TypedChoiceField (label="Type", choices = (("type1", "type1"), ("type2", "type2")), widget = forms.RadioSelect, required= True)
    format = forms.TypedChoiceField (label="Format", choices = (("format1", "format1"), ("format2", "format2")),widget = forms.RadioSelect, required= True)
    content = forms.FileField()

def __init__(self, *args, **kwargs):
    super(massform, self).__init__(*args, **kwargs)
    self.is_update = False
    # self.fields['mychoicefield'].choices = \
    #   list(self.fields['mychoicefield'].choices) + [('new stuff', 'new')]


def clean(self):
    if self.cleaned_data and 'modelname' not in self.cleaned_data:
        raise forms.ValidationError("Some error message")
        if not self.is_update:
            return self.cleaned_data
        return self.cleaned_data
    if not self.is_update:
        return self.cleaned_data
class Meta:
    model = AutoPart
  • 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-06-18T04:53:41+00:00Added an answer on June 18, 2026 at 4:53 am

    I’m pretty sure the line:

    new_model = formtoaddpart.save(commit=False)
    

    doesn’t return a new python instance of the model class.

    Your best bet would be to output the newmodel.pk in the loop and see if it changes.

    From experience, I know that if you want to edit an existing instance and save it as a new one, you can just do newmodel.pk = None and Django will create a new instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django form that uses an integer field to lookup a model
I have a django model and model form that look like this: -models.py class
I have a Django model with a ManyToMany field called images , and a
In Django I have form that is using the SplitDateTimeField which is set to
I need set field value, not passed to Django Form constructor. I have model
I have a Django form that uses a different number of fields based on
I have a Django form that needs to do some client-side validation before submitting
I have a factory method that generates django form classes like so: def get_indicator_form(indicator,
I have a form in Django with a TextField that receives comma separated input
I have a Django form using textareas and TinyMCE for text entry. I would

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.