0

Models.py:

class Enterprise(models.Model):
    name = models.CharField(max_length = 100)
    def __str__(self):
        return f"{self.id}_{self.name}"

class Client(models.Model):
    name = models.CharField(max_length = 100)
    def __str__(self):
        return f"{self.id}_{self.name}"    

class Engagement(models.Model):
    name = models.CharField(max_length = 100    
    def __str__(self):
        return f"{self.id}_{self.name}"

class StockCount(models.Model):
    name = models.CharField(max_length = 100)    
    def __str__(self):
        return f"{self.name}"

class InventoryList(models.Model):
    UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = file_upload_location, validators=[validate_file_extension], max_length = 500)
    enterprise = models.ForeignKey('Enterprise', on_delete=models.CASCADE, related_name = 'inventorylists')
    client = models.ForeignKey('Client', on_delete=models.CASCADE, related_name = 'inventorylists')
    engagement = models.ForeignKey('Engagement', on_delete=models.CASCADE, related_name = 'inventorylists')
    stockcount = models.ForeignKey('StockCount', on_delete=models.CASCADE, related_name = 'inventorylists')

views.py:

def upload(request):   
    if request.method == 'POST':
        form = InventoryListForm(request.POST, request.FILES)
        if form.is_valid():    
            # file is saved
            list = form.save(commit = False)
            list.enterprise = Enterprise.objects.get(pk = 1)
            list.client = Client.objects.get(pk = 1)
            list.engagement = Engagement.objects.get(pk = 1) 
            list.stockcount = StockCount.objects.get(pk = 1)   
            list.save() 
            return HttpResponse(f'You have just made a post request - {list.id}')   
        else:
            return render(request, "observe/upload.html", {"form": form})
    else: 
        return render(request, "observe/upload.html", {"form": InventoryListForm()})

forms.py:

class InventoryListForm(ModelForm):
    class Meta:
        model = InventoryList
        exclude = ['enterprise', 'client', 'engagement', 'stockcount']
    def __init__(self, *args, **kwargs):
        super(InventoryListForm, self).__init__(*args, **kwargs)

upload_to callable function:

def file_upload_location(instance, filename):
    ext = filename.split('.')[-1]
    # return f"{instance.enterprise}/{instance.client}/{instance.engagement}/{instance.stockcount}/{filename}"
    # return f"{filename}"
    FileType = '\\Inventory List'
    name = str(filename)
    path = os.path.join(str(instance.enterprise), str(instance.client), str(instance.engagement), str(instance.stockcount))
    # return f"{path}/{filename}"
    # return path 
    print(f"The path is {path}")
    # return f"{path}/"
    # return '{0}/{1}/{2}/{3}/{4}'.format(str(instance.enterprise), str(instance.client), str(instance.engagement), str(instance.stockcount), filename)
    return os.path.join("%s" % str(instance.enterprise), "%s" % str(instance.client), "%s" % str(instance.engagement), "%s" % str(instance.stockcount), filename)

I have tried multiple variations of the callable functions (as can be seen above from the commented out portions of the function), but I still am getting this error. What is interesting is that when it is the first time I upload the file and submit the form, it works. However, the second time I try, it gives me this errno2 error.

The full traceback is as follows:

[Errno 2] No such file or directory: 'C:\Users\bilal\Desktop\Inventory Observation Mobile Responsive Web Application\Inventory-observation-mobile-responsive-web-application\inventoryobservation\files\1_RSM Canada\PK752_Nuvera Corp\FY 2021_Prospectus work\London Count\Monthly_Sales_Reporting_Template_Tool_Start.xlsm'

It appears as though my actual solutions works for some excel files and not for others, and based on further inspection of the excel files, there doesn't appear to be anything different about the excel files that I am able to successfully upload and the ones that I am not able to upload.

1 Answers1

0

Try to Use this :-

  UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = 'file_upload_location', validators=[validate_file_extension], max_length = 500)

instead of :-

  UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = file_upload_location, validators=[validate_file_extension], max_length = 500)

What i've edited :- I have added comas on 'file_upload_location'.

Try to Use it .

Lars
  • 1,234
  • 1
  • 8
  • 31
  • Thank you for your response, but unfortunately that didn't work. Your suggested solution basically ended up creating a folder called "file_to_upload". – Bilal Junaidy Dec 21 '20 at 04:11