How to load django model rows in a list? - django

I have a django model that I am basically querying on keypress using ajax to find possible matches cause I am avoiding duplicated rows.
Now my question is how can I copy all the rows from a django model and query on it rather than hitting the database every time the user presses a key because I do not think this is a good idea and please correct me if I am wrong.
What are the consequences of hitting a database on keypress?

Related

How to check duplicate posts when submitting a new post in Django admin?

I have coworkers to upload posts through Django admin. The problem is they keep making duplicate posts as we've been covering a lot of posts. Is there a way to find out if a post already exists when typing a certain column or submitting?
I searched about that, but didn't get any useful information.
Your business case seems to be a text that is duplicate if it is case sensitive equal.
On a DB and Django Model level you assure unique entries by adding unique:
class MyModel(Model):
my_field = TextField(unique=True)
To check during input you need JavaScript in the client and an AJAX endpoint on the Django server side. It's actually an Autocomplete/Autosuggest functionality for that field. There are several packages that might help you with that. Out of the box, the Django Admin does not support this.

Improving the loading performance of foreignkey fields with thousands of options

I have a similar situation as this post in which I encounter a slow page loading as I have thousands of entries in a foreignkey field.
At modelform, is there a way to improve the page loading while keeping the dropdown function? I have used select2 to efficiently find the chosen item in the dropdown, thus want to keep this function.
Django has to fetch all those foreignkey objects from database and then render them as HTML. This is why it takes a lot of time. Fetching from database can be pretty quick if you cache everything, but rendering to HTML will still be a problem.
Here's a solution that I think would work best:
Exclude that ForeignKey field from your form.
Instead, just create a blank input field. You'll have to capture user's input via JavaScript and send that value to your backed to fetch suggestions. In fact, select2 supports fetching data from backend via AJAX, So, half of your work is done.
Create a view which will take that AJAX request and search the database for suggestions and send them back to the client.
And you're done.

Filtering queries on django admin site

I have a model Sessions that contains almost 10,000 objects. Each session has a foreign key relationship to a Subject. The problem is that when loading a page that has a foreign key field to a session, the page takes a long time to load.
To fix this, I like to have an admin on which you select a subject from one ForeignKey field, and have the sessions that have a relationship with that subject appear in a second ForeignKey field.
Is this something that is possible from within the django admin interface? I have checked out django-smart-selects (though I'm unsure if that would actually do what I'd like it to), but when I try to import it I get an error "No module named admin_static", which I believe may be because I'm using an outdated version of Django (v 1.1).
Any help would be appreciated. Thanks.
Are you sure you don't just need raw_id_fields?
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
When you visit an admin change form that has a foreign key to Sessions it will build a select dropdown with 10,000 entries. This is probably your slowdown.

Django admin interface shows counts but no data

I'm using Django 1.4 and experiencing an unusual problem with hidden rows in my admin site.
I can view individual models in my app, and they appear to be querying the database correctly when I click into each one; I can see the number of matches in the top left corner ("3 locations" below my actions bar, for example). However, entries do not appear below that count.
If I try to open an entry directly using the URL, I also get a blank page.
I haven't customized the templates or css. Anyone have experience with this type of problem?
Update: if foreign keys are removed in the model, the entries reappear below the counts. However, that FK field is needed so still trying to identify the root cause.
Fixed. This appears to happen when the model uses a ForeignKey field, and the table it points to does not have its data loaded. That accounts for the model seeing the number of rows, but not being able to pull up the individual entries.
I had this same issue (i.e. I could see the model count in the admin, but not the actual records).
I have a model (Category) with a ForeignKey to another model (Competition). I looked in the database and somewhere along the line while doing some code refactoring on the models and migrations, I ended up with the FKs on the Categories pointing to a non-existent Competition.
When I updated these FKs in the database, the records reappeared in the admin.
In admin.py where you use foreign key you have to add at the last _id for example my foreign key I'd is iClientId so in admin.py we have to write in list_display iClientId_id.

Django not updating objects completely; cache or index?

I inserted through SQL (manage.py dbshell) a handful of records. I then went to view the records in the Django admin interface. All of the records appeared, as expected.
However, any queries via Django (not direct SQL), did not return the records. Only after I went to a record through the admin interface, and clicked Save did the record appear in the query.
I'm new to Django, but after repeated searching and reading Django documentation, I did not find an answer. Any idea what is going on, and what can be done to make the SQL INSERT statements appear in queries without visiting each record to Save it through the admin interface?
Maybe you have to declare the transaction "dirty":
from django.db import transaction
transaction.set_dirty()