开发者

display first line of a file in a django admin form

开发者 https://www.devze.com 2023-02-17 16:10 出处:网络
I have a class : class Foo(models.Model): file = models.FileField(upload_to = \"/tmp/\")开发者_JAVA百科

I have a class :

class Foo(models.Model):
  file = models.FileField(upload_to = "/tmp/")开发者_JAVA百科
  attribute_1 = models.IntegerField()
  attribute_2 = models.IntegerField()

And in the admin:

class FooAdmin(admin.ModelAdmin):
  pass

admin.site.register(Foo,FooAdmin)

How can i display the N first lines of the file in the change form of FooAdmin. I'm talking of the page where you modify a specific object, not the one where you can see all different foo objects.

Thanks

Cheers


You could define a custom method to return the line, then display it as a readonly field:

class FooAdmin(admin.ModelAdmin):
    readonly_fields = ('display_file',)

    def display_file(self, obj):
        line = f.readlines()[0]
        return line
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号