Reputation: 391
In my application I have a script, tied to a button onclick event, that generates a text file dynamically. These files are not too big (~3000 lines), but the influx of data can be slow (sometimes 30 minutes to capture all the streaming data). Once the process is done, I need to store this file so that the user can download it. I will also have text files generated from previous instances of this click event as well, and need to differentiate between them.
What is the best method to approach this? Can I store a text file of this size in the database, and then retrieve it? Should I instead just store a path to the text file that exists somewhere else, but in that case, where would it be stored and how can it be retrieved? If stored in a database, would a primary key be sufficient for pulling the text file I want?
Thanks!
Upvotes: 3
Views: 3513
Reputation: 7555
If you want to be able to search the contents of these files, then storing them in the database isn't a terrible idea, but otherwise you should probably go for storing them in the filesystem.
In either case you would want to create a model to hold information about the files:
class TextFile(models.Model):
user = models.ForeignKey(User, related_name='text_files')
# if storing in database
content = models.TextField()
# if storing in filesystem
content = models.FileField(storage=FileSystemStorage(location='/path/to/files'))
You can then have file list view for a particular user like this:
@login_required
def file_list(request):
files = TextFile.objects.filter(request.user)
return render(request, 'file_list.html', {'files': files})
And a view to serve the files themselves:
@login_required
def servr_file(request, file_id):
file = get_object_or_404(TextFile, user=request.user, pk=file_id)
# if storing in database
return HttpResponse(file.content)
# if storing in filesystem
return HttpResponse(file.content.read())
Upvotes: 5