filefield

path in FileField django

path in FileField django Question: I prepared my model to create PDF filled by all the fields it includes and I try to to link generated file to the pdf = models.FileField(). However the path to the file seems to be ok I can’t reach the file through the view. models.py: class Lesson(models.Model): # fields …

Total answers: 1

How do I detect if my user has uploaded an ImageField or FileField?

How do I detect if my user has uploaded an ImageField or FileField? Question: I have the following model and function to set the upload paths for either image files or other files (which will be audio files). def upload_to(instance, filename): # Check for image or sound file and put in appropriate directory if isinstance(instance, …

Total answers: 1

Save base64 image in django file field

Save base64 image in django file field Question: I have following input “data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA7YAAAISCAIAAAB3YsSDAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAA5JxJREFUeNrsnQl4FEX6xqcJJEAS7ivhBkMAQTSJ4h0QEQ+I90rAc1cOL3QBXXV1AV1dVwmrsCqQ9VwJ6HoC7oon0T8iEkABwRC5IeE+kkAIkPT/nfmSmprunskk5CDw/p55hu7qOr76api8……..” I want to save this file in file field. What can I do? models.py class SomeModel(models.Model): file = models.FileField(upload_to=get_upload_report) created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) I’m trying to do this def get_file(data): from django.core.files import File return File(data) and save return file …

Total answers: 5

Django REST Framework and FileField absolute url

Django REST Framework and FileField absolute url Question: I’ve defined a simple Django app that includes the following model: class Project(models.Model): name = models.CharField(max_length=200) thumbnail = models.FileField(upload_to=’media’, null=True) (Technically yes, that could have been an ImageField.) In a template, it’s easy enough to include the MEDIA_URL value (duly coded in settings.py) as a prefix to …

Total answers: 11

What is the clean way to unittest FileField in django?

What is the clean way to unittest FileField in django? Question: I have a model with a FileField. I want to unittest it. django test framework has great ways to manage database and emails. Is there something similar for FileFields? How can I make sure that the unittests are not going to pollute the real …

Total answers: 6

How to assign a local file to the FileField in Django?

How to assign a local file to the FileField in Django? Question: I was trying to assign a file from my disk to the FileField, but I have this error: AttributeError: ‘str’ object has no attribute ‘open’ My python code: pdfImage = FileSaver() pdfImage.myfile.save(‘new’, open(‘mytest.pdf’).read()) and my models.py class FileSaver(models.Model): myfile = models.FileField(upload_to=”files/”) class Meta: …

Total answers: 4

Django test FileField using test fixtures

Django test FileField using test fixtures Question: I’m trying to build tests for some models that have a FileField. The model looks like this: class SolutionFile(models.Model): ”’ A file from a solution. ”’ solution = models.ForeignKey(Solution) file = models.FileField(upload_to=make_solution_file_path) I have encountered two problems: When saving data to a fixture using ./manage.py dumpdata, the file …

Total answers: 5