Pre-check Django admin checkboxes - python

Consider a Django Admin changelist view looking somewhat like this:
I would like to pre-check some of the checkboxes in the list. Documentation didn't help me further.
Any ideas on how to achieve this in a nice way? Where by nice I mean, as standard as possible.

You could tick the checkboxes using JavaScript. For example, to tick the checkbox for the item with primary key 1, using jQuery, you would do:
$(".action-select[value='1']").prop('checked', true);
Or you could use Django's bundled jQuery if you want:
django.jQuery(".action-select[value='1']").prop('checked', true);

Related

Enable wagtail DateField when a wagtail BooleanField is true

I need to have a wagtail DateField disabled by default, but if the content author checks a box (a wagtail BooleanField) then the field should be enabled and required. I'm struggling to find the best way to solve this, I haven't found documentation on how to do this. I was thinking about using Django signals or wagtail hooks but it does seem like a complex solution for what I think should be a common use case. So I was wondering if anyone has a better alternative or point me to the right direction.
I don't believe this is possible currently in Wagtail (as of Wagtail 2.2). Not natively, at least.
What you can do, however, is add custom JavaScript to your admin with a snippet like this:
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.utils.html import format_html
from wagtail.core import hooks
#hooks.register("insert_global_admin_js", order=100)
def global_admin_js():
"""Add custom.js to Wagtail Admin."""
return format_html('<script src="{}"></script>', static("js/custom.js"))
And then inside your custom.js file, you can add a JavaScript event to detect when the boolean field is checked or not, and then find and select the DateField and disable or hide it.
As Willem Van Onsem has mentioned, you'll want to make the DateField nullable and blank so you can save the Page (as a form) when the DateField is empty.

Add per-page selector to Django admin list-view

I would like to add a per-page selector in the list-view of ModelAdmin.
Like this:
I could not find anything specific to this in django admin docs: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/
I've also tried list_per_page attribute.
I'm using Django 1.7.7.
Am I missing something from docs, or is there any 3rd party app available to achieve this?
I think you need to customize djnago admin. Use jQuery to solve purpose.
Use Jquery and hit a service with page=20 or page=50 and in view.py get records according ot passing param.

How to edit data with django-tables2 in a frontend?

I have a Django and i want to create CRM system, allowing users to view, add, delete and edit data in a front-end. I found nice module, named django-tables2, which allows displaying nice tables of my data:
django-tables2 turns data into HTML tables. Features:
Pagination Ordering Extendable Class based view Supports for queryset
and list data Themes
So my question is what is the best way to make front-end editing with this tables?
For example: i want to make records from table be selectable with checkboxes and then i want them to be deletable and editable, like in django built-in admin. In other words: i need some tool like django-admin but in my front-end (in my template).
So do i need to write js to handle user clicks on table records and point this actions to my urls/views or there is a better way?Hope this question will help not only my but anyone who planning to became frontend-ninja, THANKS!
I think you should take a look at Swampdragon and Angular. They might integrate nicely with django-tables2. You can always just write the table with Angular.

Bootstrap Multiselect Integration with WTForms

I'm new to WTForms and was wondering how to integrate a bootstrap styled multiselect like this one http://davidstutz.github.io/bootstrap-multiselect/ into WTForms.
I know I can write the HTML directly to create the multiselect dropdown form but I'd rather work with WTForm objects to keep my forms consistent. Is there a simple way to convert the WTForms SelectMultipleField class into a dropdown multiselect box? Or is something more sophisticated needed?
Thanks!
I'd recommend a hybrid approach: Write the JavaScript yourself but let WTForms do the HTML part: Create a normal SelectMultipleField and on rendering call it like this: form.select_multiple(class_='multiselect'). Because the plugin itself just required valid HTML and something to select such a select field to apply its JavaScript part (which you can include on the page).
Of course, if you want more advanced stuff, you can always subclass the widget for the SelectMultipleField and extend it. Then use this as the custom widget (For example on the iconized part of the examples you linked).
Really, on the part of WTForms you don't need much modification.

Ways to get AuditTrail data in Django

I'd like to know what's the best way to get data from AuditTrail table in views.
I'd like to show all the versions of a model's fields using a template and a view. I looked for a way to show it in admin area but I had no success.
look at Django reversion https://github.com/etianen/django-reversion
It should do what you need.

Categories

Resources