Single record of model in database - django

My django app needs to display data on my homepage which it collected from third party. Requesting the information and waiting for response takes about a second, which is too long processing time for a homepage. The data which my app receives doesn't change often, so there is no reason to fetch that data every time homepage is being rendered. Instead, I want to retain the data and have my app make a request only if the last "refresh" has been done more than an hour ago.
Since using global variables in django is apparently a no-no, I'd need to make a database model which will at all times hold a single record. This feels wrong. Is making a one-record table really a way to go here?

Instead of creating a model to cache the remote site's response, you can use Django's caching framework. More specifically, you can cache a specific view and set a timeout for the cached view. See this documentation page for more details on how to do that.

Related

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.

Django page update no ajax or comet

I'm making a small app for internal use between 3 people. the idea is to have a page where user can upload files and data ( more specifically images.) to the database and a second page where the information the user uploaded in the first page will be visible without having to manually refresh the page. I have read about comet and ajax and I think having a function check the Db every certain time is not what I'm looking for. Since there will be almost no updates in the DB. maybe every 3 to 4 months the user might update a new image.
Have a read through at least one good tutorial on ajax & django, it's quite simple once you get started with it. But you can't really achieve this without AJAX.
Take a look at this;
https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html

Django - Is it possible to show loader until a start page is loaded?

My view function takes a long time until returning a template. So, I'd like to show something to a user while running the function.
Is it possible to show loader until a start page is loaded?
What's important is the loader should be shown when loading the first page after first visit of a user?
Thank you for reading my question.
You would have to fetch the page using JS. You could use something like Intercooler or PJAX which provides HTML attributes that show a spinning animation while loading the content via AJAX.
A better solution would be to make your page faster. There are several things you should consider:
Check that all Model fields that you are using for filtering or sorting have set db_index=True unless the tables are small (few hundred entries) or the fields are already unique or foreign/primary keys. Also check that your DB does sorting and merging in RAM not on disk (== the DB has enough RAM resources and has also the correct configuration to use it).
Sometimes, if you show a list of model instances you end up making separate DB requests per row if you access related models in your template. Again, check which statements your DB executes and have a look at Django's select_related, prefetch_related, values and values_list methods that can dramatically increase lookup performance. Make sure your template context contains all necessary data and only the necessary data (e.g. pageing, how much, or maybe you should consider a search index like SOLR or Elastic which can be integrated nicely via Django Haystack).
Load everything except heavy data at once in your main view, which also includes JS. The JS then uses AJAX to load the rest from a second Django view which returns an HTML snippet that your JS simply adds to the DOM.
It really depends on how comfortable you are with JS and how much you want to stick to HTML to make as much use of Django as possible (thinking of Django Forms for example). But first, tune your DB setup (disclaimer: I have written that article).
it's better to make a request with javascript to your Django endpoint, until you get a response back from your server you should show your loader, and when you get the response back successfully you should make display: none for loader and mak display: block for your loaded content
Create a function in views.py and send JsonResponse. URL example: http://localhost:8000/somedata
Render any other HTMLlet's say it's index.html. URL example: http://localhost:8000/home
That index.html file need to have some JavaScript, let's say main.js
In main.js make a request to http://localhost:8000/somedata and fetch data. Use async javascript that way you can easily track fetched data or not

Django Statelessness

I was reading some blogs and came up to the conclusion that django is an MVT architecture which do not maintain state. I am working on application that have maps visualization. When the user selects a variable from the drop down, a request is sent to the backend database and on the screen, it generates the heat-map of that specific variable.
what I want to achieve is that if I go some other tab and do some other changes like change the layer of map or select some another variable, the state of old heat map should be maintained regardless if I want to clear it. I do not know how to maintain state in django can anyone help me in this?
You can leverage the Django's sessions mechanism. You'll need an AJAX request that POSTs the current tab state to Django, so your backend code can restore it on page reload.

Django - load new objects via ajax

In django app I need to periodically check for if new object of particular model are created.
I want to this by ajax.
I was thinking about something like this:
render current timestamp into template, load current objects.
Then, every x seconds do ajax request and ask for objects which are created later then this timestamp.
What do you think? Is there maybe a better way?
What you want is a way for the client to know whether something has changed in the server. Generally there are three ways to stimulate this subscriber/broadcaster, or pull/push, relationship. The first is Ajax long-polling, which is roughly what you described. The second is implemented via WebSocket, which unfortunately not all browser supports. The third is HTTP streaming, or a long polling at the HTTP level. All three are available in https://github.com/ziyan/django-comet
A newer technology is Webhooks, which allows you to subscribe to server changes via URL (http://en.wikipedia.org/wiki/Webhook). Check it out here for an early Django adaptation: https://github.com/johnboxall/django_webhooks