Skip to content

FileNode inline form within django-admin #21

Open
@domguard

Description

@domguard

I think I just need some advice on how to integrate media_tree with my models admin.
I just created a derived model from FileNode to be able to link several documents to differents models.

from media_tree.models import FileNode

class Attachment(FileNode):

    content_type = models.ForeignKey(ContentType, blank=True, null=True, default=None)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        verbose_name = _(u'Attached file')
        verbose_name_plural = _(u'Attached files')
        app_label = 'coop_local'

Then I added this my admin.py file to use only the fields I needed in the attached files Inline:

from media_tree.forms import FileForm
from media_tree.models import FileNode

class MyFileForm(FileForm):
    class Meta:
        fields = ['parent','file','title']

class AttachmentsInline(GenericTabularInline):
    model = Attachment
    form = MyFileForm
    extra = 1

But when saving I have an error about the node_type column being NOT NULL so I have to do something about that.
I'm unable to override MyFileForm init method to set up a initial value like FileNode.FILE because the node_type field has the editable = False flag.

I ended overriding my model save() method:

    def save(self, *args, **kwargs):
        if not self.node_type:
            self.node_type = FileNode.FILE
        super(Attachment, self).save(*args, **kwargs)

What would have been the right way to do this ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions