How can I allow users to download PDF files from database? The PDF file is stored as a longblob in the MySQL database.
What I have so far:
views.py:
reports = Report.objects.all()
However doing so throws the following expcetion: <method 'fetch_row' of '_mysql_connector.MySQL' objects> returned a result with an error set
models.py:
class Report(models.Model):
studentid = models.IntegerField(db_column='studentId') # Field name made lowercase.
studentname = models.TextField(db_column='studentName') # Field name made lowercase.
year = models.CharField(max_length=4)
term = models.CharField(max_length=6)
report = models.TextField() #PDF should get stored here.
What can I do to allow user to see the files to download? I've been looking for ways to solve this problem however can't quite seem to figure it out. Any help is appreciated!
Thank you
Related
Gday, I'm working on a section of my data management project where users will be able to upload premade data for it to be parsed and inputted into the database. I am currently stuck on uploading files. The upload field will be on the main page for the specific dataset, which is navigated to by using the dataset id. What I would like is for any files uploaded in on that page to be saved in a directory such as "/projectroot/uploads/dataset_name". is this possible?
I think you are looking for something like this.
def my_memory_file_name(instance, filename):
return '/'.join(['my_memory', instance.user.username, filename])
class MyMemory(models.Model):
title = models.CharField(max_length=150, null=True, blank=True)
situation = models.TextField(help_text="Explain the situation of screenshot in few words")
date = models.DateField(null=True, blank=True, help_text="Date is not a required field")
screenshot = models.FileField(upload_to=my_memory_file_name)
I am working on one of my first Django projects and on one of the models, I needed a field that returns a list that I can loop through and use in my templates. I have been trying to make it happen right there in models.py because I learned from the tutorials that it is best to perform all data manipulation or logic in the models file and nothing of such should be done in the templates as the template is just for rendering already parsed data.
I tried:
class Profile():
'''More user information.'''
user = models.ForeignKey(User, on_delete=models.CASCADE)
user.first_name = models.CharField(max_length=25)
user.last_name = models.CharField(max_length=25)
interests = models.CharField(max_length=200)
user.interests = [interest.strip() for interest in interests.split(',')]
In the code snippet above, I tried splitting comma separated values input by the user to try and make a list of it but I got the error:AttributeError: 'TextField' object has no attribute 'split'. Since the models.Charfield() and models.TextField() don't return a string, how do I go about this?
I use the sqlite db on my local.
The user model already has first_name and last_name so you can remove
If you want your user model to have the property interest you need to user custom user model
For performing an action on a field before saving it example you splitting the interests you can override the save method for the model
I have two database which are OneToManyRelationship as shown below.
I am preparing interface for Data to be uploaded.
I want to specify the project attribute in Data model by inputting the url of the Project path like http://project/13.
Does anyone know how I can construct the relationship from url input of parent data?
Models.py
class Project(models.Model):
project = models.CharField(max_length=50, blank=True)
version = models.IntegerField(default=0)
class Data(models.Model):
project=models.ForeignKey(project)
csv=models.FileField(upload_to=dir_path)
If url in Project model sits in project field then you can do something like
# since project attribute is not unique, several ones can match so we pick first
project_object = Project.objects.filter(project=url).first()
if not project_object:
# error cant find it
Data.objects.create(
project=project_object,
csv=...,
)
I have a model for Document like :
class Document(models.Model):
document_name = models.CharField(max_length=64)
author = models.ForeignKey(Client, null=False, default=1)
size = models.IntegerField(default=0)
version = models.CharField(max_length=64)
file = models.FileField(upload_to='documents/%Y/%m/%d')
but I also want to upload that file. My form looks like
class UploadDocumentForm(ModelForm):
class Meta:
model = Document
fields = ('document_name', 'author', 'size', 'version', 'file',)
def __init__(self, *args, **kwargs):
super(UploadDocumentForm, self).__init__(*args, **kwargs)
Now obviously, I want my file to be saved in a folder and not have its own column i nthe db. In db I only want the other data. Problem is, django tries to persist it and crashes with
The above exception (table document has no column named file)
Is there a way to make file a non persisted field or something?
Im following this tutorial, https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html
I tried removing file form the model and adding it just in the form somehow but it doesn't work. It is not recognized liek that. I just want it to not be persisted in the sqlite database. Any ideas?
I thought about making a second model without the file and create somehow both of them or override the save method.. Im not sure how to make it work.
This is a strange one. Maybe I missed it but I have search through all Django documentation and SF but could not find an answer for this. I have a table with about 30 columns. The table looks like this...
Class Customer (models.Model):
customer_id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=35)
last_name = models.CharField(max_length=35)
middle_initial = models.CharField(max_length=2)
mail_addr1 = models.CharField(max_length=50)
mail_addr2 = models.CharField(max_length=50)
mail_city
mail_state
mail_zip
bill_addr1
bill_addr2
...
...
active_yn = models.ForeignKey('Status', models.DO_NOTHING) # <-- This one
...
...
home
mobile
The offending field is "active_yn". Django keeps spitting out an error saying that it is now a valid field.
Here's the things I am sure of:
The table definitely have this field in the correct DB, schema, table, etc
It is not the last field on the table.
inspectdb for this table is also missing this field.
I drop and re-add this column and it is still not showing.
The field is a TINYINT(3) - referencing a table Django recognized.
I am using MySQL
I have been trying to debug this for days now. Any ideas?
Thank you all for helping. I found the issue. The problem is on the DB side. I am using root user connecting directly to the DB. I have the settings file connecting with a different user with tables and fields granted to it. This table has this one field not in the grant to this user (Not my doing... urgh). Anyway, once I added this field, everything works as expected.
I appreciate all your help. Hopefully the next guy will find this useful.
Cheers!