Django - how to have dependent select fields manually - python

I am new in django (and python as well).
I want to have a dependent select field for state and city in admin . I know the feature Grouped Selects in django smart selects can do this. But this method render the options from the database which is not what I looking for.
I have the dependent data from the external source (json format), and using javascript to populate the regarding data.
So, how do I create the select field and add a class to it (for js selector)?
should I create a empty text field and using js to convert it to select field?
Thanks

I would consider using django autocomplete light, it is a fairly solid select-field chaining tool that is good for both model relationships and remote data sources.
This is a link to the documentation for the remote data integration.
http://django-autocomplete-light.readthedocs.org/en/latest/remote.html

Related

How to create dynamic input fields in html

I want to dynamically create multiple dropbox fields as I input the number of fields to be created.
For instance, there are 2 fields:
integer input field
dropdown menu
so when I input(let's say "4" in field 1) then 4 dropdowns should be created.
How should it be done? And upon submitting the form the same will be stored as a list in a database (flask is used at the backend).
Please guide.
Thanks in advance.
Flask is rather lightweight framework so to create fields dynamically use Javascript. Note nowadays its preferred to develop websites using some advanced JS library or 'framework' JQuery, vue, react etc. On backend you create a controller to save the data. Start with going though a tutorial to understand the procedure. https://www.youtube.com/watch?v=6N4OmsfwfdU . If you use WTF find a tutorial or discussion specific to WTF.

python application link models and views

I am trying to create a python gui application where I need an MVC like pattern to display and control models.
My issue is that I will create and modify the models over time and I need to create several different "view types" (like a form view on one window and a map view on an other), each "view type" should be able to show each of my models.
If I use an MVC pattern (which I am not even sure is relevant), I should then create a view-controller for each of my model and "view type".
So if I create a new model, I will have to create a view-controller for each of the existing "view types", and if I want to create a new "view type" I will have to create a new view-controller for each model.
Creating a generic view is hard because the models are quite independant and differents.
Is there a good pattern or example I could use so I can make this smarter ? I'm stuck with this model / view design...
Thanks for ideas.
I believe you can this by using MVC. You don't have to create a lot of controllers for each model, you can just design GET-parameter like ?view= or specify endpoint to read view type and after this to handle it in a single controllers.

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.

Custom Django Database Frontend

I have been trying to get my head around Django over the last week or two. Its slowly starting to make some sense and I am really liking it.
My goal is to replace a fairly messy excel spreadsheet with a database and frontend for my users. This would involve pulling the data out of a table, presenting it in a web tabular format, and allowing changes to be made through text fields and drop down menus, with a simple update button that will update all changes to the DB.
My question is, will the built in Django Forms functionality be the best solution? Or would I create some sort of for loop for my objects and wrap them around html form syntax in my template? I'm just not too sure how to approach the solution.
Apologies if this seems like an simple question, I just feel like there is maybe a few ways to do it but maybe there is one perfect way.
Thanks
The fastest way not to implement you own pages and to have a tabular view of your data is to use the django's built-in admin interface. It gives you sorting, filtering and search functionality and quick to start. You just need to define your models in models.py and setup the admin pages as described in the docs.
Normally the admin page is not used as a representation to users or customers but in the case you described it seems a clean and quick choice.
Exporting the excel sheet in Django and have the them rendered as text fields , is not as easy as 2 step process.
you need to know how Django works.
First you need to export the data in mysql in database using either some language or some ready made tools.
Then you need to make a Model for that table and then you can use Django admin to edit them

Improving Performance of Django ForeignKey Fields in Admin

By default, Django's admin renders ForeignKey fields in admin as a select field, listing every record in the foreign table as an option. In one admin-accessible model, I'm referencing the User model as a ForeignKey, and since I have thousands of users Django is populating the select with thousands of options. This is causing the admin page to load incredibly slowly, and the select is not very useful since it can take a while to scroll through thousands of options to find the one you want.
What's the best way to change the rendering of this field in order to improve page load and usability? I'd like the select field to be replaced with some sort of button to launch a search form popup, or a text field that searches keywords via Ajax to find the Id for the specific User they want to associate. Does admin have anything like this builtin, or would I have to write this from scratch?
Add raw_id_fields to your model to only show the ID instead of a dropdown.
You're right, Cerin, the cause of the slowdown is because Django is populating the <select> element with too many options. You might want to use an autocomplete element instead.
Interestingly, Django 2.0 has introduced a new feature on the admin site, called autocomplete_fields, which I think you will find useful in this case. It uses AJAX.
class ExampleAdmin(models.ModelAdmin):
autocomplete_fields = ['example_field_user']
You can use one of the few autocomplete apps for Django. Check them at Django Packages.
There's also django-extensions that have ForeignKeyAutocompleteAdmin that fit your needs pretty well.
Another option is to add readonly_fields instead of raw_id_fields

Categories

Resources