如何在django中显示raw_id_fields的值?

时间:2014-10-07 01:32:00

标签: django

我需要从外键获取一个值,这是我计划使用javascript显示的url。问题出在创建之后:

class SplashScreenAdmin(admin.ModelAdmin):
    list_display = ('title', 'text', 'background', 'button', )
    search_fields = ('title', 'text')
    list_filter = ('background', 'button')
    raw_id_fields = ('picture',)

admin.site.register(SplashScreen, SplashScreenAdmin)

我无法从中获取数据。至少我需要它在raw_id_fields旁边显示标题。有没有简单的方法来实现这个目标?

What I need to do

1 个答案:

答案 0 :(得分:1)

在这里和那里发生了一些小黑客后,我终于能够让它发挥作用了。

Models.py

class ImageMedia(models.Model):
    caption = models.CharField(max_length=64)
    picture = models.ImageField(upload_to='image_media', blank=True)

class SplashScreen(models.Model):
    title = models.CharField(max_length=25)
    text = models.TextField()
    background = models.ForeignKey(Background)
    button = models.ForeignKey(Button)
    url = models.URLField()
    picture = models.ForeignKey(ImageMedia)

    def splash_image(self):
        return '<img id="media_image" src="{0}{1}" height="150px"/><p id="media_caption">Caption</p> '.format(
            settings.MEDIA_URL, '')

    splash_image.allow_tags = True

    def __str__(self):
        return self.title

views.py

from django.contrib.auth.decorators import login_required
import json

@login_required
def get_picture_media(request):
    if request.method == 'GET':
        media_id = request.GET['media_id']

        if media_id:
            media = ImageMedia.objects.get(id=int(media_id))
            if media:
                media_data = {'media_url': media.picture.url, 'media_caption': media.caption}
                return HttpResponse(json.dumps(media_data))

Admin.py

class SplashScreenAdmin(admin.ModelAdmin):
    list_display = ('title', 'text', 'background', 'button', )
    search_fields = ('title', 'text')
    list_filter = ('background', 'button')
    raw_id_fields = ('picture',)
    fields = ('title', 'text', 'background', 'button', 'url', 'picture', 'splash_image')
    readonly_fields = ['splash_image']

    class Media:
        js = ("js/media-render.js",)

admin.site.register(SplashScreen, SplashScreenAdmin)

JS /媒体render.js

function media_render() {
    var id_picture = document.getElementById('id_picture').value;
                    var media_url = '';
                    var media_caption = '';
                    $.get(document.location.origin + '/get_picture_media/', {media_id: id_picture},
                                        function(data){
                                            data = JSON.parse(data);
                                            media_url = data['media_url'];
                                            media_caption = data['media_caption'];
                                            document.getElementById('media_image').src = media_url;
                                            document.getElementById('media_caption').innerHTML = media_caption;});
                    };
window.onfocus = media_render;
document.getElementById('id_picture').onchange = media_render;

这样,每当我更改原始ID时,我都可以随时更新我的​​图像,并且我也可以获得标题。使用schillingt,我将js分开一点,所以代码现在有点整洁了

最终结果:

Final result