django admin - show all for more than 200 items - django

I currently have a listing in django admin that is split across 8 pages.
what i need to do is to have a button/link to display all items of a list in django admin even if there are more than 200 items while keeping the pagination.
the "show all" link does exactly what i need but its limited to 200 items. Is there any way i can change that? (without modifying the core). Also is there a way so i can change the list_per_page in the modeladmin on demand?

You can change the list_max_show_all and list_per_page attributes on your admin class.
class FooAdmin(admin.ModelAdmin):
list_max_show_all = 500
list_per_page = 200
Works with Django 1.4 and newer. See the manual.

Not sure it's what you're looking for when you say "on demand" modification of list_per_page, but you could almost certainly query a database. It'd be rather unwieldy, but depending on your use case, administrators could log in, modify their preference, and then proceed to whatever model actually matters.
For example:
#models.py
class PageLength(models.Model):
page_length = models.IntegerField()
#admin.py
class FooAdmin(admin.ModelAdmin):
list_per_page = PageLength.objects.get(pk=1)

Related

Overriding django admin pagination along with dynamic values

I am working on a project, I have a requirement from my client he wants to implement a dynamic paginator in django admin panel. Requirement is when user input 10, ten record will display per page same for 20,30
is there any way to do it.
See ModelAdmin.list_per_page. The default is 100 but you can change it to whatever you like.
class UserAdmin(admin.ModelAdmin):
model = User
list_per_page = 5 # No of records per page
admin.site.register(UserAdmin)
If you want to dynamically change it I'd assume you'd use an ajax with a GET for the page number otherwise you'd have to do a lot more and alter the default admin template used by django.
Check out this approach if it helps you .

Django - Confirmation page before change of different models

I'd like to create a confirmation page for selected objects before a change is made to them (outside the admin). The objects can be of different models (but only one model a time).
This is much like what is done in administration before deletion. But the admin code is complex and I haven't grasped how it is done there.
First I have severall forms that filter the objects differently and then I pass the queryset to the action / confirmation page. I have created a form factory so that I can define different querysets depending on model (as seen in another similiar question here at Stackoverflow):
def action_factory(queryset):
''' Form factory that returns a form that allows user to change status on commissions (sale, lead or click)
'''
class _ActionForm(forms.Form):
items = forms.ModelMultipleChoiceField(queryset = queryset, widget=forms.HiddenInput())
actions = forms.ChoiceField(choices=(('A', 'Approve'), ('D' ,'Deny'), ('W' ,'Under review'), ('C' ,'Closed')))
return _ActionForm
Which I use in my view:
context['form']=action_factory(queryset)()
The problem is that the items field wont be displayed at all in the html-code when it is hidden. When I remove the HiddenInput widget it displays the form correctly.
I don't want to display the choice field since there can be thousands of objects. All I want to have is something like "Do you want to change the status of 1000 objects" and a popdown and a submit button. A simple enough problem it seems, but I can't get it to work.
If someone has a solution to my current attempt I would be glad to hear how they have done it. Even better would be if there is a cleaner and better solution.
I used the wrong widget. It should be MultipleHiddenInput not HiddenInput.

django admin hierarchical inline model edit

Consider a wiki application. There is a model Page, that has many Revisions and each revision has many blocks.
What is the simplest way to create an admin in which, you select a page and all the blocks of the latest revision appear; bonus points for letting change of revision by a dropdown (which is by default, sorted in reverse order anyway)
Is it absolutely necessary to create views, or can I extend some of those StackedInline forms, override save and mention some magic meta options, to get it all done automagically.
Have you tried something like this (in admin.py):
class RevInline(admin.TabularInline):
model = Revision
class PageAdmin(admin.ModelAdmin):
model = Page
inlines = (RevInline,)
admin.site.register(Page, PageAdmin)

Django admin changelist - restrict where field is NULL

I'm setting up a new Django app and need to customize the Admin for a given table by restricting the records where a field is NULL. Basically a built-in, permanent filter.
Seems like changelist_view needs to be overridden, but I'm uncertain what that change would look like.
There's no code to be included as I'm not overriding changelist_view right now.
You can override default manager, but it has a drawback that you'll have to explicitly specify original manager in all your queries:
class MyManager(models.Manager):
def get_query_set(self):
return super(MyManager, self).get_query_set().filter(my_field__isnull=False)
class MyModel(models.Model):
objects = MyManager()
all_objects = models.Manager()
MyModel.all_objects.all() # all objects including those having my_field=None
There's not really a good way to do this at present - there is in fact an open ticket on Django requesting the ability to customise what QuerySet gets used for the admin views - see ticket #10761. Antony's solution will work in the short term, but you may have to wait until that ticket is resolved for a proper solution.
I've decided to use limited queryset manager as objects. For ModelAdmin I've copied queryset() from django/contrib/admin/options.py and changed _default_manager by mine unlimited queryset manager. Simple!

Multiple images per Model

I'm writing a simple real-estate listing app in Django. Each property needs to have a variable number of images. Images need to have an editable order. And I need to make the admin user-proof.
So that said, what are my options?
Is there a ImageList field that I don't know about?
Is there an app like django.contrib.comments that does the job for me?
If I have to write it myself, how would I go about making the admin-side decent? I'm imagining something a lot slicker than what ImageField provides, with some drag'n'drop for re-ordering. But I'm a complete clutz at writing admin pages =(
Variable lists, also known as a many-to-one relationship, are usually handled by making a separate model for the many and, in that model, using a ForeignKey to the "one".
There isn't an app like this in django.contrib, but there are several external projects you can use, e.g. django-photologue which even has some support for viewing the images in the admin.
The admin site can't be made "user proof", it should only be used by trusted users. Given this, the way to make your admin site decent would be to define a ModelAdmin for your property and then inline the photos (inline documentation).
So, to give you some quick drafts, everything would look something like this:
# models.py
class Property(models.Model):
address = models.TextField()
...
class PropertyImage(models.Model):
property = models.ForeignKey(Property, related_name='images')
image = models.ImageField()
and:
# admin.py
class PropertyImageInline(admin.TabularInline):
model = PropertyImage
extra = 3
class PropertyAdmin(admin.ModelAdmin):
inlines = [ PropertyImageInline, ]
admin.site.register(Property, PropertyAdmin)
The reason for using the related_name argument on the ForeignKey is so your queries will be more readable, e.g. in this case you can do something like this in your view:
property = Property.objects.get(pk=1)
image_list = property.images.all()
EDIT: forgot to mention, you can then implement drag-and-drop ordering in the admin using Simon Willison's snippet Orderable inlines using drag and drop with jQuery UI
Write an Image model that has a ForeignKey to your Property model. Quite probably, you'll have some other fields that belong to the image and not to the Property.
I'm currently making the same thing and I faced the same issue.
After I researched for a while, I decided to use django-imaging. It has a nice Ajax feature, images can be uploaded on the same page as the model Insert page, and can be editable. However, it is lacking support for non-JPEG extension.
There is a package named django-galleryfield. I think it will meet your demand.