Sorry for long delay on this thread. Back at the problem now.

On Mar 25, 1:22 am, Sam Lai <samuel....@gmail.com> wrote:
> On 25 March 2010 09:25, Scot Hacker <shac...@berkeley.edu> wrote:
>
>
>
> > <snip>
> > It sounds like you're saying that the model method is not
> > attaching to an instance like it usually does (which is why "self"
> > doesn't work). But I'm no closer to figuring out how why it's
> > different from model methods accessed from within views, or how I can
> > fix it.
>
> It's different because the Media class is a class itself, even though
> its definition is nested within the model/form class. It only has
> access to the methods/properties/variables it contains (unless it is
> given something else when constructed).

Ah! Now I understand this distinction, thanks for that.


> The solution to your problem is here 
> -http://docs.djangoproject.com/en/dev/topics/forms/media/#media-as-a-d...
>
> Admin media is based on Forms media, so the same principles apply.
>
> Sidenote - what's you've implemented is documented at the top of that
> link, and as stated, it is designed for static media. follow the
> 'media as a dynamic property' section and you should be able to
> achieve what you want.

That gets me part of the way there,  but not quite.

>
> Here's an example from my CkWidget class (no if blocks at the moment,
> but you can easily add them there as it is just a normal instance
> method inside your class):
>
> def _media(self):
>         # add CKEditor JS to page. It is assumed that it is located at
>         # MEDIA_URL + 'js/ckeditor/ckeditor.js' unless base path is overridden
>         # in settings
>         js = [settings.CKEDITOR_BASEPATH + 'ckeditor.js']
>
>         return widgets.Media(js=js)
>
> media = property(_media)

OK I've got something similar now, using _media rather than "class
Media:" Now all that remains is to do a conditional on the js= line.
That would be easy IF I could access fields on the model it references
to see what the boolean is currently set to. I've been reading
everything I can find, including
http://docs.djangoproject.com/en/dev/topics/forms/media/#media-as-a-dynamic-property
but still can't see how to access a field value on a model from the
admin.

To boil it down, is it possible to get something similar to the print
statement below to work (once I can print it out I can use it in the
conditional).

# models.py
class Tutorial(models.Model):
     use_visual_editor = models.BooleanField()

# admin.py
class TutorialAdmin(admin.ModelAdmin):
    def _media(self):
            print [current value of use_visual_editor for the
instance]
            js = ['/paths/to/media',]
            return forms.Media(js=js)

    media = property(_media)

admin.site.register(Tutorial, TutorialAdmin)

I appreciate your help - this has turned out to be one of those
problems that looks trivial but turns out hair-pulling. Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to