Herramientas de usuario

Herramientas del sitio


wiki2:python:django:admin

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:python:django:admin [2018/11/09 11:09]
alfred [List views]
wiki2:python:django:admin [2020/05/09 09:25] (actual)
Línea 76: Línea 76:
 </​code>​ </​code>​
  
 +:!: **Important**:​_ To tell Django we want to perform a join instead of fetching the names of the categories one by one,we can use list_select_related
 +<​code>​
 +@admin.register(models.Product)
 +class ProductAdmin(admin.ModelAdmin):​
 +    list_display = (
 +        '​id',​
 +        '​name',​
 +        '​category',​
 +    )
 +    list_select_related = (
 +        '​category',​
 +    )
 +</​code>​
 +
 +=== Add a link into the list ===
 +<code python>
 +from django.contrib import admin
 +from django.utils.html import format_html
 +
 +
 +class CourseAdmin(admin.ModelAdmin):​
 +    list_display = ('​title',​ '​my_url_field',​)
 +
 +    def my_url_field(self,​ obj):
 +        return format_html('<​a href="​%s%s">​%s</​a>'​ % ('​http://​url-to-prepend.com/',​ obj.title, obj.title))
 +    my_url_field.allow_tags = True
 +    my_url_field.short_description = '​Column description'​
 +</​code>​
 ==== Changing the layout ==== ==== Changing the layout ====
 To put several fields in a row: To put several fields in a row:
Línea 98: Línea 126:
         }),         }),
     )     )
 +</​code>​
 +
 +You can do this by declaring inlines, of type TabularInline (horizonal layout) or StackedInline (vertical layout, just like the default model layout).
 +<​code>​
 +class BooksInstanceInline(admin.TabularInline):​
 +    model = BookInstance
 +
 +@admin.register(Book)
 +class BookAdmin(admin.ModelAdmin):​
 +    list_display = ('​title',​ '​author',​ '​display_genre'​)
 +    inlines = [BooksInstanceInline]
 +</​code>​
 +
 +===== Admin templates =====
 +==== Overriding them ====
 +  * https://​docs.djangoproject.com/​en/​2.0/​ref/​contrib/​admin/#​admin-overriding-templates
 +
 +
 +===== JS y CSS en el Admin =====
 +
 +==== Usando jQuery en el Django Admin ====
 +<​code>​
 +(function($) {
 +    // < start of closure
 +    // within this block, $ = django.jQuery
 +    $(document).ready(function() {
 +      // Your JavaScript
 +    });
 +})(django.jQuery);​
 +</​code>​
 +
 +==== Add static snippets ====
 +<​code>​
 +# admin.py
 +class MyModelAdmin(admin.ModelAdmin):​
 +    # admin additions
 +
 +    class Media:
 +        css = {
 +            "​all":​ ("​css/​my_style.css",​)
 +        }
 +        js = ("​js/​my_script.js",​)
 </​code>​ </​code>​
 ===== Gotchas ===== ===== Gotchas =====
Línea 124: Línea 194:
 admin.site.unregister(Group) admin.site.unregister(Group)
 </​code>​ </​code>​
 +
 +=== Add new page to the admin ===
 +<code python>
 +class CourseAdmin(admin.ModelAdmin):​
 +    list_display = ('​title',​ '​my_url_field',​)
 +
 +    def get_urls(self):​
 +        urls = super().get_urls()
 +        custom_urls = [
 +            path(
 +                r'​attendee-list/',​
 +                self.admin_site.admin_view(self.attendee_list),​
 +                name='​attendee-list',​
 +            ),
 +        ]
 +        return custom_urls + urls
 +
 +    def my_url_field(self,​ obj):
 +        return format_html('<​a href="​%s"​ target="​_new">​Llista d\'​assistencia</​a>'​ % reverse('​admin:​attendee-list'​))
 +    my_url_field.allow_tags = True
 +    my_url_field.short_description = '​Column description'​
 +
 +    def attendee_list(self,​ request):
 +        import weasyprint
 +        import django.template.loader as loader
 +        temp = loader.get_template('​admin/​attendee_list.html'​)
 +        html = temp.render()
 +        pdf = weasyprint.HTML(string=html.encode('​utf-8'​))
 +        response = HttpResponse(pdf.write_pdf(),​ content_type='​application/​pdf'​)
 +        # response['​Content-Disposition'​] = '​attachment;​ filename="​llista_assistencia.pdf"'​
 +        response['​Content-Disposition'​] = '​filename="​llista_assistencia.pdf"'​
 +        return response
 +</​code>​
 +
 +=== Dynamically hide fields ===
 +<​code>​
 +class BookAdmin(admin.ModelAdmin):​
 +    list_display = ("​pk",​ "​get_title_or_nothing"​)
 +
 +    def get_form(self,​ request, obj=None, **kwargs):
 +        if obj.type == "​1":​
 +            self.exclude = ("​title",​ )
 +        form = super(BookAdmin,​ self).get_form(request,​ obj, **kwargs)
 +        return form
 +</​code>​
 +
 +
wiki2/python/django/admin.1541761794.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)