I am a beginner django developer. I am building my first serious application and I would like to use the mechanism of uploading files to the server. I have searched a large part of web, but I have not found an easy guide anywhere to deal with this issue in django2.
Is there any experienced django ninja that could provide me with a comprehensive process to design this solution, from creating the model through the form and the apparently view? I tried to work with official documentation, but I can not help it.
I will be very grateful! :D
I would like to know what gone wrong with documentation. You should be more specific when asking questions, that helps people who want to answer.
Let me try to answer your question.
You Model will simply contain an extra filefield like this
class MyModel(models.Model):
my_field = models.FileField(upload_to='your_location')
Your form will
Simply a model form which will contain this model name as class Meta
in the view part you will simply handle this as a post request but you have to add request.FILES
So it will be like
If request.method=='POST':
form = MyModelForm(request.POST, request.FILES)
Now let me come to the upload_to argument. You have to pass a string here. The file will be uploaded to that directory within media directory if the media root setup already done by you.
Related
I'm making a website with django where you can basically upload a file to the django admin and download it whenever you need to view it. Very simple (making it for my dad which wants to organize his excel files for his work).
This is the problem, I hard coded the download path to the file I uploaded, which makes me have to modify the path every time I upload a new file with the same name (since django adds a random string of digits and characters every time you upload a file with the same name as before). What i'm currently doing is really inefficient, and would like to change that as soon as possible.
My idea is to make the FileField name editable in the django admin. I've come across this:
list_editable = ['asd'].
I thought that might work and so I tried it. The results were interesting. I immediately saw a 'Save' button on the bottom of the admin site, which wasn't there before. But I still couldn't edit the FileField. I've searched this on multiple forums, the django documentation and more forums, only to not find anything useful.
This is a picture of how the admin page looks when I added the list_editable to my FileField.
So, I decided to look for an equivalent to see if that works. Again, I searched on multiple forums, but still found nothing.
Any type of answer or recommendation is very much appreciated.
Thanks in advance!!
I am currently digging into django form wizards and I am trying to upload multiple images. Unfortunately, the BaseStorage provided by django form wizard does not handle multiple files. It always assumes a dictionary to be passed. There is a fix posted here.
The problem is now, that the posted code breaks in the render_done step of the form wizard when the form_obj is reevaluated. FileFields do not validate when the file is turned from dict to a list.
Can anybody point me in the correct direction on how to fix this?
How do generic views deal with this issue? Or don't they have this issue at all?
I'm a little new to the inter-workings of Django and I would like to display a simple dynamic folder path field that opens to the given path when clicked so a user can view all the files in that path. I'm trying to do this in django admin site change form but am unclear and confused of how to do so. Below is my model.
class Order(models.Model):
order_number = models.IntegerField(verbose_name='LS #', unique=True)
order_name = models.ForeignKey(recs.RecipeControl, related_name='recipe')
# Something like this is I think what I want.
folder_path = models.FilePathField(path=get_path)
def get_path(self):
return str(self.order_number)+"_"+self.order_name
I'm puzzled as how to properly go about this because I can't seem to reference "self" to do this, especially if the record doesn't already exist. I've looked at a few other Q&A's but none of them dealt with the admin site and after a bit of reading I'm convinced that I may have to override one of the save methods but don't understand which one and where to place my method. Thanks in advance
EDIT
After reading through the comments recommended below I think what I want is different than what I had originally thought. The folder path still needs to be dynamic.
However, what I'm looking to do is check on new and existing records whether a folder exists in a given directory (MEDIA_ROOT?) based on model data, then create that directory or update it's name if it changes and save the folders path in the FilePathField. I'm pretty confident that this can be done by overriding that save_model method of the ModelAdmin, no?
In my model I have, for now, 3 classes:
Quest: Basics attributes of quest
GoToAndAnswer: Kinda type of a quest, which extends Quest
SeekAndAnswer: Same as above, extending Quest
I'm using Django to build an API so in my View I want to print the info about these Quests in JSON format. The problem is I don't know how to serialize the objects correctly (Im really new at Django and Python):
# Only get the attributes of the class Quest, obviously
serializers.serialize("json", Quest.objects.all())
# Only get the atributes of the GoToAndAnswer, nothing about the Quest attributes
serializers.serialize("json", GoToAndAnswer.objects.all())
So which is the best way to achieve this?
I have seen posts like this: JSON Serialization of a Django inherited model
But it didnt help...
Thanks in advance
You might want to take a look at Django REST Framework which solves all your problems.
That's a Django app with a excellent documentation and all the tools you need to create a API. http://www.django-rest-framework.org/
I have a model that inherits from Photologues 'ImageModel'. The user can upload photos and everything works fine, however the problem I am running into is when I am creating a form to edit a photo object. Since the ImageModel.image is a required field, and I can't prepopulate a FileField widget with a file already uploaded, if the user doesn't upload a new image to overwrite the old one they get an error. The error pops up in form.save() which I am using to get the rest of the fields updated right. Is there some way I can hook in and try say "since I know I am just editing an image, I know one has already been uploaded, so don't worry if the form field is empty".
Any thoughts?
You have a couple options. One, you can modify the Photologue source to make that field optional. The other, and if it will work for you the one I'd recommend, is to check out my newer library django-imagekit: http://bitbucket.org/jdriscoll/django-imagekit/wiki/Home
ImageKit is basically JUST the ImageModel part of Photologue but it's much more flexible and easier to work with. ImageKit's ImageModel works on top of the models that you define so fields can be configured how ever you please.