Extending the File Model

Showing 1-10 of 10 messages
Extending the File Model Juergen Schackmann 6/29/11 12:17 PM
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.
Aw: Extending the File Model stefanfoulis 6/29/11 12:37 PM
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.

Aw: Extending the File Model Juergen Schackmann 6/29/11 1:19 PM
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?
Aw: Extending the File Model Juergen Schackmann 7/5/11 3:15 PM
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???
Re: Extending the File Model Croepha 7/5/11 3:31 PM

On Jul 5, 5:15 pm, Juergen Schackmann
I am working on this issue in my fork of filer:
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
Aw: Re: Extending the File Model Juergen Schackmann 7/6/11 12:28 AM
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
Re: Aw: Re: Extending the File Model stefanfoulis 7/6/11 12:36 AM
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.

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.
Re: Extending the File Model stefanfoulis 7/6/11 12:39 AM
@Croepha: Your code looks pretty complete. Are you using this in production already? Would you mind making a pull-request?
Re: Extending the File Model Croepha 7/6/11 9:44 AM
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):

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


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

Aw: Re: Aw: Re: Extending the File Model Juergen Schackmann 8/7/11 3:06 AM
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