83751

Using django-multiupload within a ModelForm

Question:

There are two models, a main one containing information on companies and a ForeignKey model that links files to the company model. I now want to integrate a MultiFileField on my form that allows for uploading multiple files at once (using <a href="http://chive.ch/django-multiupload/" rel="nofollow">django-multiupload</a>).

Unfortunately, whenever I send the form, I get a "This field is required." error for my MultiFileField.

Here is my code:

# models.py class Company(models.Model): name = models.CharField(max_length=200) note = models.TextField() class CompanyData(models.Model): company = models.ForeignKey(Company, on_delete=models.CASCADE) document = models.FileField(upload_to="attachments") # forms.py class CompanyForm(forms.ModelForm): attachments = MultiFileField(min_num=1, max_num=3, max_file_size=1024*1024*5) class Meta: model = Company fields = '__all__' def save(self, commit=True): instance = super(CompanyForm, self).save(commit) for each in self.cleaned_data['attachments']: CompanyData.objects.create(document=each, company=instance) return instance # views.py def company_edit(request, pk): current_company = get_object_or_404(Company, pk=pk) if request.method == "POST": form = CompanyForm(request.POST, instance=current_company) if form.is_valid(): current_company = form.save(commit=False) current_company.save() return redirect('company_detail', pk=current_company.pk) else: form = CompanyForm(instance=current_company) contexts = { 'form': form, } return render(request, 'company_edit.html', contexts) # company_edit.html <form method="POST" class="post-form" enctype="multipart/form-data"> {% csrf_token %} {{ form }} <button type="submit" class="save">Save</button> </form>

Answer1:

In case fellow django-newbies come across a similar problem: I was just missing request.FILES in

form = CompanyForm(request.POST, request.FILES, instance=current_company)

Recommend

  • JavaScript - Sum of values in same rows and columns
  • gedit plugin error - plugin loader 'python3' was not found
  • Find when google places autocomplete returns zero results
  • How to import JSON file on Jest tests?
  • Getting Unable to determine application identity of the caller Error
  • ServiceStack JsonServiceClient - Custom HTTP Headers not sent
  • WPF ToolBar Separator shrinks to nothing when inside a StackPanel
  • How can I submit iOS apps to the App Store with MacBook A1181?
  • How to include prebuilt APK into AOSP with platform privileges
  • How to use apoc.load.csv in conjunction with apoc.create.node
  • How can I send data in text field using Selenium?
  • How to apply CSS to document.write()?
  • PushPlugin not found, or is not a CDVPlugin
  • Transfer file trough SSH tunnel with Java
  • Difference between assigning instantiation to parent class and derived class
  • Spotify Apps API: get other users' playlists
  • How to layout? (JFrame, JPanel, etc.)
  • JQuery Validate plugin submitting when it shouldn't
  • Planned Contrasts on glmmTMB
  • Laravel 5 - Cache remember doesn't work
  • SELECT on JSONField with Django
  • didSelectItemAtIndexPath of UICollectionView inside of a UIScrollView is not getting called
  • LINQ to populate treeview based upon grouping
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Codeigniniter insert data through models and controller
  • Passing value from popup window to parent form's TextBox
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • PHP Permalinks.. how to change?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?