Django: Show list of thumbnails in admin change view - python

I'm trying to do something a little bit different than other Django admin thumbnail questions here. I have two models that reference my Photograph model either via ForeignKey or ManyToMany and would like to be able to see the available photos in the admin interface as thumbnails instead of by their title. I've tried using an admin_thumbnail method that returns the appropriate HTML as a tag (I've used this elsewhere with success) but to no avail. The list of Photograph objects is using their __unicode__ return value to populate the list, so I tried having __unicode__ return mark_safe(the HTML string) but the list items all end up with empty strings. Any ideas? I'm bad with widgets and I'm hoping a custom widget isn't my only option here.

Related

Hide Objects List in Django Admin Delete Form

When deleting an item from the Django Admin page, the delete form shows:
Summary
Objects
The Objects list shows all the objects and their related objects. The list is pretty huge and difficult to navigate when deleting multiple records.
Is there a way to hide the "Objects" list from the delete form in Django?
I have looked around, but the only option I have found is to override the template. Is it possible to achieve this through ModelAdmin?
The options as you've found are to either override the template (and likely the view itself to avoid the additional queries) or disable the existing action and create your own version.

Group list items by field value in the Django admin

I'm trying to group the items in the Django admin app by a specific field (e.g. date).
So I've added to the queryset in admin.ModelAdmin.getQueryset() the following:
queryset = queryset.values('date').annotate(Sum('amount'))
But this doesn't work because in this case, a dict is returned instead of a queryset.
I started exploring what's inside the django/contrib/admin folder, and I think something need to be done before sending the object to the template change_list.html.
I'm not sure but I think the class in views/main.py (admin folder) might need some change.
Can anybody confirm that what I'm trying to do is achievable at all?
Please find below a representation of what I'm trying to achieve:
Follow the below example in URL. it's has great way to understand with override django admin with custom queryset and groupby data
https://medium.com/#hakibenita/how-to-turn-django-admin-into-a-lightweight-dashboard-a0e0bbf609ad
I found this useful: https://github.com/xacce/django_admin_grouper
You can simply define the group in ClassAdmin
class RecordAdmin(admin.ModelAdmin):
def regroup_by(self):
return 'category'
The repo overrides Django's change_list_results.html. If your RecordAdmin has method reggroup_by than it inserts a row with the name of the category. If reggroup_by is missing it works as usual.

Django modelform: Create new related object

I've been searching stack overflow and google for a solution for over an hour now, and I can't seem to find something that, in my opinion, should be easy to obtain (as it's a common use case).
I've checked this thread, and a few others, but I haven't been able to find a real, easy solution:
Django modelform: is inline adding related model possible?
Anyway, say I have a model with three related entities, two foreign keys and a many-to-many related class. Now, I have a ModelForm which displays these in comboboxes and lists, but what I need is that "+" button next to these elements (as seen in the admin interface).
I want the plus to take me to a new form, for that particular entity, allow me to submit the new information, create the database entry, take me back to my original form and have the newly added entity selected in the combobox. I'm really hoping the django ModelForm Meta class has an attribute that I can't seem to find which enables exactly this.
This isn't really a django question.
This has to do with presentation of a particular widget in an html document, and that is governed by either the HTML markup, CSS, or javascript.
Django is a server side application and is primarily responsible for creating a valid http response and receiving a valid http request (of course, there is a lot that happens in the interim and that is why django is so big) but it's not a "one toolkit to kill them all" app.
I think you want to look at bootstrap: http://getbootstrap.com/
Jquery UI: http://jqueryui.com/
Or some combination of the two.
You can also just mark up the document yourself with a stock img or something.
However, if you want to do it exactly how the admin does it, just go into django.contrib.admin and examin the code to figure out how the django developers did it. I believe they are just using Jquery UI and some manual markup to accomplish that.

Prepopulate initial values for fields in the Django Admin without slugifying

In the Django admin, I can set a slug field to fill in automatically using prepopulated_fields. How can I set a field to fill in using a different function, for example just basic concatenation instead of lowercase and spaces-to-hyphens ?
There is no out of the box support for this (assuming you're talking replacing the exact, live editing that preopulated_fields provides).
The slug function is written in JavaScript, in django/contrib/admin/media/js/urlify.js
You could potentially insert a new script in the ModelAdmin extra JS property, but make sure your admin page doesn't actually need the "real" slugify script :)

Thumbnails of ImageField of Many-to-Many relationship list in the admin interface

I have two models, Post and Photo. Photo contains information about photos, the date the original photo was uploaded (or alternatively - shot), a description, a title and more. The post contains post text and a ManyToManyField which links back to photos to see which are related to the post.
I however have a problem that, seeing all the photos' titles in list view makes very little sense, and so I must look into a way to show previews.
I have sorl-thumbnails working, but I'm not sure where to start in order to get them into the admin interface.
Help will be very much appreciated!

Categories

Resources