Django model overview: show more than 100 items? - django

In one of the models overview panel, after I filter the items by month, I have to select them all and then create a document with information regarding them (kind of like a monthly report). This is a problem when one month has more than 100 items as Django paginates the filtering results.
Is there a way to increase the number of items shown from 100 to 400 or select all the items from the filtering result?
Selecting all the items from one page, creating a document, going to the next, creating another, etc, then merging the documents isn't an option.

See http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_per_page
In your ModelAdmin definition, set list_per_page:
class MyModelAdmin(admin.ModelAdmin):
list_per_page = 400
I believe you can also add the all GET parameter to your query (ie, add ?all to the end of your url), but that only works if you have less than 200 items, and this limit is hardcoded in the admin. Therefore it's only useful if you have more than list_per_page (100) and less than 200 items, but the admin will offer you a link for this anyway when this is the case.

If you're talking about admin, see this.
ModelAdmin.list_per_page

Related

DynamoDB filter condition and Limit

Can anyone help me to understand what is the best approach to handle condition filtering and Limit
I'm using dynanmodb to store some products and I want to use pagination with them.
Because some of the products are enabled I got to filter based on a field.
no problem until there.
When I use Limit, if I want to Limit the data to be returned the Limit will be applied after the query makes the filtering.
Here is the deal :
All the time the query will return the data and Limit based on the last item to top first one, and I get the Key to make the pagination to the top.
If I would like to retrieve from the top to the bottom I Will set Scan Index Forward = true, and do the same as the other query.
But I would like to get random products and do the pagination because I don't I apply something like that, all the time the same products from the first one to the bottom or unlike. but what about the products in the middle etc?
I'm using a simple schema :
PK: PRODS
SK: ITEM#RamdomID
thanks a lot, I appreciate any helpful comment.

How to create multiple numbers of fields based on the value in one number field in Oracle Apex form?

I am working on a form in which I need to take numeric value from one field and based on that number(n) I need to create n fields for another column. How can I do so?
Probably you can use APEX_ITEM API if I understand your task right:
https://docs.oracle.com/en/database/oracle/application-express/19.2/aeapi/APEX_ITEM.html
Something like this:
SELECT APEX_ITEM.TEXT(rownum)
FROM DUAL
CONNECT BY rownum < :YOUR_ITEM;
You will not be able to create n page items in a declarative way. APEX simply wasn't made for this.
I can think of two workarounds:
Use a plsql dynamic content region to generate html with the apex_item package just like Ivan Dubashinskii suggested. You will need to submit the page in order to have the region re-generate your page items when the amount n is changed. There are some downsides to this approach, as for example it won't be that simple to use your page items in a page process.
Just limit your page items to some number, like 10. Then, create 10 page items in a declarative way and use dynamic actions to show/hide them when n changes. This way, it will also be much easier to use your items in a page procress.

Trying to minimize the number of trips to a database voting table

I use django 1.10.1, postgres 9.5 and redis.
I have a table that store users votes and looks like:
==========================
object | user | created_on
==========================
where object and user are foreign keys to the id column of their own tables respectively.
The problem is that in many situations, I have to list many objects in one page. If the user is logged in or authenticated, I have to check for every object whether it was voted or not (and act depending on the result, something like show vote or unvote button). So in my template I have to call such function for every object in the page.
def is_obj_voted(obj_id, usr_id):
return ObjVotes.objects.filter(object_id=obj_id, user_id=usr_id).exists()
Since I may have tens of objects in one page, I found, using django-debug-toolbar, that the database access alone could take more than one second because I access just one row for each query and that happens in a serial way for all objects in the page. To make it worse, I use similar queries from that tables in other pages (i.e. filter using user only or object only).
What I try to achieve and what I think it is the right thing to do is to find a way to access the database just once to fetch all objects voted filtered by some user (maybe when the user logs in in or the at the first page hit requiring such database access), and then filter it further to whatever I want depending on the page needs. Since I use redis and django-cacheops app, can it help me to do that job?
In your case I'd better go with getting an array of object IDs and querying all votes by user's ID and this array, something like:
object_ids = [o.id for o in Object.objects.filter(YOUR CONDITIONS)]
votes = set([v.object_id for v in ObjVotes.objects.filter(object_id__in=object_ids, user_id=usr_id)]
def is_obj_voted(obj_id, votes):
return obj_id in votes
This will make only one additional database query for getting votes by user per page.

How can i avoid repeating objects with endless pagination and order_by('?') queryset

I am using django-endless-pagination with its twitter-style pagination. Now i want to paginate over shuffled query set. I have tried to add
return Fact.objects.all().order_by('?')
But then objects can appear more than 1 time.
How can I change this behavior?
I think using pagination is misleading in this case. When the user clicks on page 2 it's not actually the second page, it's just another 20 items.
A better option is having a single button (called Fetch for example) and fetch 20 items (or whatever the page size is) each time the user clicks on it.
To avoid the same item showing up twice, you can keep a list of viewed ids in session and exclude them from the subsequent queries.
Another approach that you can take try is something that I used and I found on another StackOverflow post here.
import random
items = sorted(Fact.objects.all().order_by('nr'), key=lamda x: random.random())
return items

Django admin store dynamic formset added with ajax

I'm currently implementing a solution using django admin, it allows users to define in the db a product, and then custom attributes and details, more details may be aggregated by a common attribute, this allows me to query with ajax a custom view that returns some JSON data to build automagically the form fields that I need directly in the same formset view (manipulating the DOM).
The current DB design follows this schema:
Catalog(name, description, photo)
Product(rel_catalog, name, base_price, photo, manufacturer_email)
ProductDetail(rel_product, rel_attribute, percentage_price, fixed_price)
ProductAttribute(rel_product, name, description)
As you may see I have a catalog, where there can be more products, a lot of details per product, aggregated by attributes. Then I simple show by default the Catalog, then the select with all available products for that catalog, then, choosing the right Product, I obtain the complete form (each row has a label with ProductAttribute.name and a select with related ProductDetail).
All works pretty dam good, but I also need to store this references in the DB when someone completes the form (making an order with choosen products). This forms are displayed as StackedInline (the ModelAdmin is for the Order).
I don't know how many options there may be per product so I was thinking to use this design to track orders:
Order(customer, status, notes, tot_price, inserted_by)
OrderItem(rel_order, catalog, product, unit_price)
But I don't know how to store the dynamic added inputs...
I was thiking to implement OrderItemProperty(rel_orderitem, rel_productdetail, rel_productattribute) to store each single input... but how do I loop over this unknown fields?
Maybe do you suggest a better design?
If you need more code just ask for it and I'll reply with a pastebin link.
Thankyou.
Finally I got a working solution,
I've created a custom view, overriding the default "add/" view, this way I can customize whatever I want to and I can read the POST data handling each validation, putting then the data in the right model.