Django FilePathField raises file not found error - django

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.

Related

Changing the link for the ModelAdmin list view in Django to apply a default filter?

I want to apply a default filter value on a field ModelAdmin in django.
I have a Model admin for the User model, which displays users. The user has a m2m to an Account model, so I am adding in the ModelAdmin:
class CustomUserAdmin(UserAdmin):
list_filters = ('accounts')
In the filter I want it to give a default selected value, if nothing is selected. However I still want to give the user the option to revert to the default All option.
So far all the solutions that I found, prevent reverting to the All option. For example this answer.
I am thinking, that maybe chaining the link on the side menu to include the desired filter option in the query parameters. Is this doable? I can see in the source code for admin/app_list.html (source code), that the URL comes from model.admin_link, but I cant find any documentation for changing it.

Django viewable or clickable filepath field

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?

how to display my own custome message in the top of admin page

want to display my own custom message and link in the top of localhost/admin/module/field before the select fields to change page. I tried with link_display that for displaying each instance obj itself, and i don't want that.
You need to override the change_list.html template for your app, see the documentation.
Money quote for your problem:
For example, if we wanted to add a tool to the change list view for
all the models in an app named my_app, we would copy
contrib/admin/templates/admin/change_list.html to the
templates/admin/my_app/ directory of our project, and make any
necessary changes.

How should I extend the Django FeinCMS MediaLibrary?

We would like to extend the MediaLibrary of the Django FeinCMS without editing the module code itself.
We want
a few custom fields
and to use a library for individual cropping that we have already.
How should we put everything together?
The simplest approach would be to create an extensions model with a ForeignKey to the MediaLibrary, and to register a custom Admin site with an inline admin for the extensions model. But maybe there's a cleaner and better way.
As Hedde said, the media file model supports registering extensions the same way as the page module allows, using MediaFile.register_extensions.
The method which is used for generating thumbnails in the media library should always be FEINCMS_MEDIALIBRARY_THUMBNAIL. The default value of this setting is feincms.module.medialibrary.thumbnail.default_admin_thumbnail which is a method that receives a media file object and returns the URL to a thumbnail or None.

Django: How to dynamically add tag field to third party apps without touching app's source code

Scenario: large project with many third party apps. Want to add tagging to those apps without having to modify the apps' source.
My first thought was to first specify a list of models in settings.py (like ['appname.modelname',], and call django-tagging's register function on each of them. The register function adds a TagField and a custom manager to the specified model. The problem with that approach is that the function needs to run BEFORE the DB schema is generated.
I tried running the register function directly in settings.py, but I need django.db.models.get_model to get the actual model reference from only a string, and I can't seem to import that from settings.py - no matter what I try I get an ImportError. The tagging.register function imports OK however.
So I changed tactics and wrote a custom management command in an otherwise empty app. The problem there is that the only signal which hooks into syncdb is post_syncdb which is useless to me since it fires after the DB schema has been generated.
The only other approach I can think of at the moment is to generate and run a 'south' like database schema migration. This seems more like a hack than a solution.
This seems like it should be a pretty common need, but I haven't been able to find a clean solution.
So my question is: Is it possible to dynamically add fields to a model BEFORE the schema is generated, but more specifically, is it possible to add tagging to a third party model without editing it's source.
To clarify, I know it is possible to create and store Tags without having a TagField on the model, but there is a major flaw in that approach in that it is difficult to simultaneously create and tag a new model.
From the docs:
You don't have to register your models
in order to use them with the tagging
application - many of the features
added by registration are just
convenience wrappers around the
tagging API provided by the Tag and
TaggedItem models and their managers,
as documented further below.
Take a look at the API documentation and the examples that follow for how you can add tags to any arbitrary object in the system.
http://api.rst2a.com/1.0/rst2/html?uri=http://django-tagging.googlecode.com/svn/trunk/docs/overview.txt#tags
Updated
#views.py
def tag_model_view(request, model_id):
instance_to_tag = SomeModel.objects.get(pk=model_id)
setattr(instance_to_tag, 'tags_for_instance', request.POST['tags'])
...
instance_to_tag.save()
...returns response
#models.py
#this is the post_save signal receiver
def tagging_post_save_handler(sender, instance, created):
if hasattr(instance, 'tags_for_instance'):
Tag.objects.update_tags(instance, instance.tags_for_instance)