Overriding django admin pagination along with dynamic values - django

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 .

Related

Additional field on django rest framework filters and form (not stored)

There are 2 scenarios here where I'm looking to add an additional non-stored field to the DRF 3.0 generics views. On both my add contact and list contacts web services I was able to call these correctly using python as client. I would like to be able to the same functionality possible in the browser though as well.
For add contact I need to add a scenario ('phone', 'form') so I can have different validations take placed based on this value.
For the list contacts, I have an interesting data scenario where I want to check 2 fields and related table for the phone number...so on this I'd just like to be able to add a field with name search_phone to the filters on the list view so I can handle it in get_queryset.
I tried adding a field to ModelSerializer but I couldn't get it in the form or filter.
TIA

list of textareas in django forms

I have a cms-like page where users can create their own simple pages, with a title, image and some content. The form I'm using for it is the following:
class PageForm(forms.Form):
"""
Helper form for the cms backoffice page.
"""
title = forms.CharField(max_length=50)
preview_image = forms.ImageField(required=False)
content = forms.CharField(widget=TinyMCE(attrs={'cols': 20, 'rows': 10}))
But now I want to enhance this by adding multiple sections to a page. The thing is these are optional, so user can choose to only leave one section, in which case this form should do it, but then have a button 'add section' or something which can will add an extra TinyMCE and ImageField to the UI, also Delete section will delete one.
Now the way I see it this is a long shot, but is there any way I could model this using django forms? The way I'm thinking now if I limit the number of forms to a given number, I could add content_i and preview_image_i to the form and then check which one were submitted. That should work right? Is there any way to do it without a maximum number of sections? Otherwise how much of an overhead would I be adding if I add 100 CharFields / ImageFields which would be used rarely to a full extent (if at all).
Or should I just abandon django forms for this and do some custom handling?
Thanks

django admin - show all for more than 200 items

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)

django admin inlines (and nested inlines) : how can I get this functionality?

I'm a little confused as to why this sort of functionality isn't default in the admin, but maybe someone can give me a few hinters to how to go about it.
I have a projects application which keeps track of projects and is to be edited through the admin. Each project has numerous ForeignKey related models (links, flatpages, video, image etc.) that could be placed as inlines within the project admin.
(One or two models have nested inlines, so they don't display in the admin (this and this ticket deal with this) )
Instead of being able to edit these models inline on the project admin (which gets messy and difficult to use), I would love a list of all the current instances of that related model, and simple add/edit button for each model which opens a popup with that model's form.
Project Admin:
- Normal Fields
- Links:
-Link 1 (edit)
-Link 2 (edit)
+ add link <- popup
- Images:
-Image 1 (edit)
-Image 2 (edit)
+ add image <- popup
so on. How would I go about writing this? I only need to do it for one section/model of the admin panel so I don't think writing my own Crud backend is necessary.
Thanks
I implemented something like this in an application once, but since django-admin doesnt support nested inlines (by which i mean inlines within inlines), i followed a slightly different approach. The use case was that you had an invoice (with a few inline attributes) and u had reciepts (again with inline attributes). Reciepts had a foreign key to the invoice model (basically a reciept was part payment of the invoice).
I implemented it by adding a field to the invoice list view which linked to a filtered reciept list view.
So in the invoice admin, there would be:
def admin_view_receipts(self, object):
url = urlresolvers.reverse('admin:invoice_%s_changelist'%'receipt')
params = urllib.urlencode({'invoice__id__exact': object.id})
return 'Receipts' % (url, params)
admin_view_receipts.allow_tags = True
admin_view_receipts.short_description = 'Receipts'
This gives you a link in the list view that takes you to another list view, but filtered by foreignkey. Now you can have inlines for both models and easy access to the related models.

Implement auto_current_user_add when using Django's User table

I have an AppEngine app that I'm migrating to run in Django, using app-engine-patch to get all the goodness of Django - particularly the Admin interface.
One of my models looks like (partially) this:
class Request(db.Model):
requestor = db.UserProperty(auto_current_user_add=True)
When I display a form based on this model I don't display the requestor field, and so when I call the Model's put() method the entity I'm saving doesn't have the requestor property set. This triggers the auto_current_user_add magic, and the user who created the request is automatically added.
Under Django, I'm using the provided Users table. I want this to display as a list of the users of my app, so the model becomes:
from ragendja.auth.google_models import User
class Request(db.Model):
requestor = db.ReferenceProperty(User)
However, this breaks the auto_current_user_add magic in the admin interface - if the user of the admin interface doesn't enter a value for the requestor property, Django sets the property to None, when I'd really like for the Request to have their username inserted automatically.
How can I restore the magic?
My solutions relies on three things:
First: it's possible to override the model's put() method.
Second: users.get_current_user() still provides the correct user, and
Third: ragendja.auth.google_models.User.get_djangouser_for_user() takes a google.appengine.api.users.user object and returns the corresponding Django User object - creating it first if it didn't already exist.
Putting this all together, I have:
class Request(db.Model):
requestor = db.ReferenceProperty(User)
def put(self):
if not self.requestor:
self.requestor = User.get_djangouser_for_user(users.get_current_user())
super(Request, self).put()
This works nicely with the admin interface: the admin can assign any existing user (or use the supplied + sign to create a new user) - if they leave it blank, they'll be assigned as the requestor.
Later when I add a view for users to manage their own requests, this value will be on the 'excluded' list, and the same method will add in their username every time they create a new request.
I'm not sure if this is an optimal solution though; I'm new to Django, so maybe there's a better way to achieve this.