how to add ManyToManyField Extension

30 views
Skip to first unread message

Sascha

unread,
Dec 23, 2014, 3:30:57 PM12/23/14
to django-...@googlegroups.com
Hi,
I'm trying to add a download list (pdf). but it doesn't work. I'm getting an error...

"Unknown field(s) (a, c, e, g, i, m, p, r, t, x, _) specified for Article. Check fields/fieldsets/exclude attributes of class ArticleAdmin"

models.py:

class Downloads(Extension):
    def handle_model(self):
        self.model.add_to_class(
            'downloads_pdf',
            models.ManyToManyField(MediaFile))
       
    def handle_modeladmin(self, modeladmin):
       modeladmin.raw_id_fields.append('downloads_pdf')
       modeladmin.add_extension_options(_('Downloads'), {
            'fields': ('downloads_pdf'),
      })

i have no idea...

all the best
Sascha

Sascha

unread,
Dec 24, 2014, 5:05:49 AM12/24/14
to django-...@googlegroups.com
it works now :D


class Downloads(Extension):
    def handle_model(self):
        self.model.add_to_class(
            'downloads_pdf',
            models.ManyToManyField(MediaFile))
       
    def handle_modeladmin(self, modeladmin):
       modeladmin.extend_list('filter_horizontal', ['downloads_pdf'])
       modeladmin.add_extension_options(_('Downloads'), {
            'fields': ('downloads_pdf',),
            'classes': ('collapse',),
      })

Sascha

unread,
Dec 28, 2014, 6:04:39 PM12/28/14
to django-...@googlegroups.com


Now I have a new problem. I try to create a list of available download files in the template. I'm getting only the „caption“ string from the MediaFileTranslation class. <li>{{ article.downloads.all|join:"</li><li>" }}</li>


I need a string like this: <a target="_blank" href="url" > caption </a> . I think I must create this in the extension class in my models.py...But I'm not sure and the most important thing is I don't know how to do it.


Sascha

unread,
Jan 5, 2015, 10:44:49 AM1/5/15
to django-...@googlegroups.com
class Downloads(Extension):
    def handle_model(self):
        self.model.add_to_class(
            'downloads_pdf',
            models.ManyToManyField(MediaFile, blank=True, null=True, related_name='+'))
       
     def handle_modeladmin(self, modeladmin):
       modeladmin.extend_list('filter_horizontal', ['downloads_pdf'])
       modeladmin.add_extension_options(_('Downloads'), {
            'fields': ('downloads_pdf',),
            'classes': ('collapse',),
      })


{% for downloads in article(or entry or page).downloads.all %}<li><a target="_blank" href={{ downloads.get_absolute_url }}> {{ downloads }} </a></li>{% endfor %}
is easy :D


Reply all
Reply to author
Forward
0 new messages