Django viewable or clickable filepath field - django

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?

Related

Django: keep log after deleting a foreignkey

My question is simple:
I have a Folder model, and I have a Log model. Here are the simplified examples:
class Folder(models.Model):
name = models.CharField(max_length=100)
class Log(models.Model):
folder = models.ForeignKey(Folder, on_delete="I NEED HELP HERE")
What this means is, A folder is created by a User, and each action done on the folder, is logged by using the Log model (user-edited folder, the user did this with folder, etc...)
What I want to do is simple: if some admin deletes the folder completely, I want to still keep the name of the folder. All actions in the on_delete attribute, do not let me do that.
Any ideas?
PS: those are not the real models, just an illustration of what I want to do. The real ones are 100+ lines of code :P
You can implement your own on_delete trigger, since the on_delete just expects a callable that will be called if the object to which it refers is deleted: Django does triggering management itself. But that will likely only make it more complicated.
I think it might be better to make Folder a "soft-deleted" model. Here in case you delete a model, it is not deleted from the database, but the database has a column with a boolean that specifies if the record is "deleted". If it is deleted, it will normally not be included in standard querysets on the model, but there are still ways to obtain the item to which it refers. Furthermore you can later recover a deleted object by "undeleting" it. You can for example make use of the django-softdelete package [GitHub].
I think I found an answer, all I did was to set a default value, then put on_delete=models.SET_DEFAULT.
The default value is a variable returned from a function, declared right above the model, taking the username and storing it in a different table.

Django FilePathField raises file not found error

I've defined a Django model which makes use of FilePathField(allow_files=False, allow_folders=True) which shall allow to define a required (not optional) file system directory in the model. If I try to add a new model in the admin interface I get a file not found error. All my other models registered the same way are just working fine. If I'd use CharField() instead of FilePathField() to hold the file system directory path I'd simply define a default value like CharField(default='').
How can I fix the model making use of the FilePathField field?
I have encountered same problem and the cause of the error is that django tries to scan the directory in order to get list of files that will be displayed as choices in html widget.
There are few solutions.
If you don't mind limiting what paths can be added by users just add path=/some/dir to FilePathField
If you don't want to limit paths but don't care about ability to update path trough admin just set editable=False in FilePathField
If you want both you can replace widget in admin for this particular field to classic text input.

Django 2.0.3 uploading files by models.FileField()

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.

Photologue ImageModel required field question (and how to override)

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.

Django - update a model won't delete the old FileField

I am implementing an application with django, which has a model with a FileField:
class Slideshow(models.Model):
name = models.CharField(max_length=30,unique=True)
thumbnail = models.FileField(max_length=1000,upload_to="images/app/slideshows/thumbnails")
and I have an admin backend where django manages the models. I just added the file admin.py and django manages everything for me
from django.contrib import admin
from apps.gallery.models import Slideshow
admin.site.register(Slideshow)
In the backend, it is possible to add, delete and update the slideshows. However, when I try to update a slideshow and change its attribute thumbnail [FileField], django does not delete the old file. Consequently, after several updates the server is filled with many files which are useless.
My question is: how can I make django delete those files automatically after an update?
I would really appreciate your help
I thought much about this problem, and eventually I find out a solution than works well for me. You can find all models in project and connect pre_save and post_delete signals to them.
At the end I made app, which sloves this problem - django-cleanup
I'm sure Django does this by design. It can't know, for example, whether any other models might be using that file. You would also be really surprised if you expected the file to remain and discovered that django deleted it!
However, there's also the issue that as soon as you change the file field, you lose the old file name.
There's an open ticket about that problem: http://code.djangoproject.com/ticket/11663
There's a patch in http://code.djangoproject.com/ticket/2983 which shows how to override __set__ to store the previous file name. Then your model's __save__ method can get access to the previous file name to delete it.