简体   繁体   English

readonly_fields在Django内联模型中返回空值

[英]readonly_fields returns empty value in django inlined models

i am new in django framework, in my current try, i have two models (Client and Facture). 我是django框架的新手,在我目前的尝试中,我有两个模型(客户端和Facture)。 Facture is displayed as a TabularInline in client change view. 在客户端更改视图中,外观以TabularInline的形式显示。

i want display a link for each inlined facture object to download the file. 我想为每个内联的制造对象显示一个链接以下载文件。 so i added a custom view that download the facture file, but dont know how to link to it 所以我添加了一个自定义视图,该视图下载了制造文件,但是不知道如何链接到该文件

class Client(models.Model):
    ...

class Facture(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)
    numero = models.IntegerField(unique=True, default=rand_code)
    ...

and in the admin.py: 并在admin.py中:

class FactureInline(admin.TabularInline):

    model = Facture
    extra = 0

    readonly_fields = ('numero', 'dl_link')

    def DLFacture(self, request, obj):
        ...
        response.write(pdf)
        return response

    def get_urls(self):
        urls = super(FactureAdmin, self).get_urls()
        from django.conf.urls import url
        download_url = [
            url(r'^(?P<pk>\d+)/download/$', self.admin_site.admin_view(self.DLFacture), name="download"),
        ]
        return download_url + urls

    def dl_link(self, obj):
        from django.core.urlresolvers import reverse
        return reverse("admin:clients_facture_download", args=[obj.pk])

admin.site.register(Facture, FactureAdmin)

class ClientAdmin(admin.ModelAdmin):
    inlines = [
        FactureInline,
    ]
admin.site.register(Client, ClientAdmin)

i get the following error: 我收到以下错误:

Reverse for 'clients_facture_download' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

all works fine when i change the reverse url to 当我将反向网址更改为时,一切正常

reverse("admin:clients_facture_change", args=[obj.pk])

so any one could help me know how to reverse the download view and if i am doing thinks right ? 所以任何人都可以帮助我知道如何反转下载视图,并且如果我在做的话认为正确吗?

thanks for any help 谢谢你的帮助

I would think you need to reverse the order in the url: 我认为您需要颠倒网址中的顺序:

url(r'^download/(?P<pk>\d+)$', self.admin_site.admin_view(self.DLFacture), name="download"),
    ]

Firstly, you are using name='download' , but trying to reverse clients_facture_download . 首先,您使用的是name='download' ,但尝试撤消clients_facture_download

I would try changing the url from 我会尝试从更改网址

url(r'^(?P<pk>\d+)/download/$', self.admin_site.admin_view(self.DLFacture), name="download"),

to

url(r'^(?P<pk>\d+)/download/$', self.admin_site.admin_view(self.DLFacture), name="clients_fracture_download"),

Secondly, InlineModelAdmin does not have a get_urls method. 其次,InlineModelAdmin 没有 get_urls方法。 You should move it to your ClientAdmin class. 您应该将其移至ClientAdmin类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM