On 3 April 2010 08:44, Scot Hacker <shac...@berkeley.edu> wrote:
> 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>
> 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.

Ah right. You want the JS to apply to the form, not to the admin class
(JS in the admin class is loaded even when you're just looking at the
data in list view; while JS in the form is only loaded when you're
editing).

So you need to define a form for your model using forms.ModelForm.

class TutorialAdminForm(forms.ModelForm):
     class Meta:
          model = Tutorial

That should suffice; the ins and outs of model forms are here -
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

Now instead of applying the above media code to the admin class, apply
it to your form class. Once in the form class, you have access to the
form's model instance via self.instance, e.g.

    def _media(self):
            print self.instance.use_visual_editor
            js = ['/paths/to/media',]
            return forms.Media(js=js)

    media = property(_media)

Once that's done, you need to tell Django admin to use your new form
class, so change your admin class to the following:

class TutorialAdmin(admin.ModelAdmin):
    form = TutorialAdminForm

And that should work. You need to make sure you have the right imports
for the various classes. I tend to define my forms in a separate
forms.py but that's up to you.

Hope that works.

-- 
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