Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileNode inline form within django-admin #21

Open
domguard opened this issue Jan 30, 2013 · 0 comments
Open

FileNode inline form within django-admin #21

domguard opened this issue Jan 30, 2013 · 0 comments

Comments

@domguard
Copy link

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant