Extending the File Model

301 views
Skip to first unread message

Juergen Schackmann

unread,
Jun 29, 2011, 3:17:01 PM6/29/11
to django...@googlegroups.com
Hi all,
I would like to extend the File Model with additional fields, e.g. status (draft, published, ...). This can be easily done via an inherhited model. What I am wondering is however, what do I need to to so that the File Field is refering to this new model?
Any hints welcome.
Thanks,
Juergen

stefanfoulis

unread,
Jun 29, 2011, 3:37:36 PM6/29/11
to django...@googlegroups.com
Hi Juergen

It's slightly more tricky than subclassing File, because django-filer already uses subclassing to support multiple different filetypes (currently only generic files and images).

Instead I'd recommend you make a Model with your custom extra fields and have a OneToOne Relation to the File Model.

e.g: file = models.OneToOneField(File, related_name='workflow')

For your fields to show up in admin, you could unregister and re-register the File and Image admins with your own subclasses of FileAdmin that add an additional inline with your extra fields.

I'm not sure if I'm describing this very well. Don't hesitate to ask if my ramblings were confusing ;-)

There are plans to make it easier to extend files with custom fields. But so far I've not found an elegant solution/api.

--stefanfoulis

Juergen Schackmann

unread,
Jun 29, 2011, 4:19:02 PM6/29/11
to django...@googlegroups.com
Hi Stefan,
thanks for the explainations, they are very clear to me, although not the answer I was hoping for.
3 ideas that I had while looking at it:
1. Subclassing the file field with the default_model_class = NewFileModel
2. Changing the file field that it accepts the model_class as an parameter in __init__
3. Create a settings entry, where you can define the model class to use
What do you think?
Regards,
Juergen

Juergen Schackmann

unread,
Jul 5, 2011, 6:15:29 PM7/5/11
to django...@googlegroups.com
are my ideas that weird?
option 1 could be done as required for specific project, but 2 and 3 would requirec change of the django-filer app???

Croepha

unread,
Jul 5, 2011, 6:31:50 PM7/5/11
to django-filer


On Jul 5, 5:15 pm, Juergen Schackmann
I am working on this issue in my fork of filer:
https://github.com/croepha/django-filer/compare/develop...experimental%2Fmodular-file-types#diff-3
it allows you to subclass filemodels.File however you want, and then
in settings.py you specify a list of Models for Filer to try when
uploading.

Juergen Schackmann

unread,
Jul 6, 2011, 3:28:51 AM7/6/11
to django...@googlegroups.com
that sounds good, I think this is my options 3 (which I think is the soundest anyway).
i just do not understand what you mean with "list of Models for Filer to try". what will be TRIED

stefanfoulis

unread,
Jul 6, 2011, 3:36:13 AM7/6/11
to django...@googlegroups.com
@Juergen:
1. and 2.) would only work on specific fields. so any existing apps that use file fields would continue to use the original model
3.) would globally change the model, but that would also break South support :-(

I am very interested in a solution to make the file model extendable, but breaking South support is not really an option.
With the solution of extending by OnoToOne Relationship would also allow multiple apps to extend the model and not just one.
E.g one app could extend Filer to have a moderation workflow and another app could extend it to have advanced copyright information. With the full on replace the File model approach there could only be one of those.

@croepha:
I'll take a look at your fork soon. That looks like something that should be integrated in filer and was actually the base idea from beginning on. Unfortunately I never got around to code a nice api to it.
I'm not sure if it solves Juergens problem though. As far as I understood him, he wants to extend all files with extra fields. Your fork allows for added functionality for specific file types (e.g a PDF-Filetype that has extra pdf specific stuff) much like the currently hardcoded Image file type.

stefanfoulis

unread,
Jul 6, 2011, 3:39:24 AM7/6/11
to django...@googlegroups.com
@Croepha: Your code looks pretty complete. Are you using this in production already? Would you mind making a pull-request?

Croepha

unread,
Jul 6, 2011, 12:44:48 PM7/6/11
to django...@googlegroups.com
If you wanted to add your own fields to the File and Image models, you
should be able to do this:

---------------------- myapp.models.py ---------------------------

class MyFile(filer.models.filemodels.File):
myField = Field()

class MyImage(filer.models.imagemodels.Image, MyFile):
pass


---------------------- myapp.settings.py ---------------------------

FILER_FILE_MODELS=(
'myapp.models.MyImage',
'myapp.models.MyFile',
)

@stefanfoulis: I have made a pull request, and I have only used this
code in development/testing deployments thus far...

Juergen Schackmann

unread,
Aug 7, 2011, 6:06:29 AM8/7/11
to django...@googlegroups.com
stefan,
sorry for coming back only now, but i was distracted lately :-)
i think croepha's solution would work fine for me, will look into it
thanks a lot
juergen
Reply all
Reply to author
Forward
0 new messages