Django ModelChoiceField, thousands of options , not user friendly - django

I have a ModelChoiceField in my form:
customer = forms.ModelChoiceField(Customer.objects.all())
the problem is it renders as a drop down with hundreds of options and its difficult for users to find a customer, is there a way to overcome this???

You should use Bootstrap Select - which is a JQuery plugin that allows the searching of dropdown data by setting data-live-search="true" on the desired field.
You can also set data-size="5" which would only show the first 5 options in the immediate dropdown field, other items are accessed via scrolling.
(This will not help if the problem is the time it takes for this dropdown to load).

This is incorrect way to do what you're trying to accomplish here.
If you're displaying more than 20 or so customer then the UI would clutter and it would be hard to find the customer with ChoiceField. You'd probably want to index the data from your DB into a full text search engine such as ElasticSearch which is based on Lucene and then use AJAX to query particular customer by it's name or any unique identifier.
Needless to say here that instead of the ChoiceField, now your form will have a text field and as soon as user tries to fill in the name, the AJAX calls fetches the customers from ElasticSearch and renders the result.

Related

Django: How to request data and simultaneously have it editable

I have a simple app that gets user input and when a button is clicked, the input is saved as an entry on the database. I'm thinking of creating another app that not only displays the same information (think of view profile) but also simultaneously lets the user edit the text that is displayed in the text field.
I'm guessing the solution is to have the text-fields be auto-filled by pulling the data from the database, and allow overwriting the data once the submit button is clicked.
Typically read and edit views are separated for good reasons like avoiding accidental edits, allowing different levels of access, things like that. But this capability does exist in Django via forms. If you already have a Form built for submitting the data, you can provide a page with existing data pre-filled by initializing the Form instance with the data - in the docs they call this a bound form. See this example in the docs to get an idea of the mechanics.

auto-populate a column in a new row in edit list mode in SharePoint 2013

I have a list that is connected to a form and filtered based upon a value in the parent form (an ID field). I want to allow the users to add new rows by directly editing the list. In doing so, I don't want to have to make the users enter in the value that already exists on the form. I can't seem to find anyway of doing this.
Example Screenshot
Are you using infopath ? I think your option is to call a web service to verify such thing. Or if you customize your form using javascript you could achieve this the same way by javascript.

Store temporary information in sitecore

I've a doubt about the best practices related with Sitecore.
I will need to store a form information when the user presses Save button while he's still filling all the form before submit. Therefore, in case he goes to the website in the next day he will see the already filled information and he can submit.Because the form is long and splitted in four steps.
My doubt is more related with the best practices for Sitecore. Where should I store this information?
Should I create a table inside sitecore_core or other existing database and read from there? (If there's any way to do that with Sitecore libs)
Or should I create my own database, probably with just two tables, to store that information?
Thanks
You should create your own database and store the information there. It is no best practice to add tables to the Sitecore databases and storing this information inside one of the existing tables (ie Sitecore items) is also not the way to go.
So just go for the custom database.
If you are using user profiles and (xdb) analytics data and want to store the form data there anyway, that could also be an option (using a custom facet).
You should use WFFM for saving any form information. In case if you want to send/save any additional information then you add your custom save action. Like for example we save data from the contact us form and newsletter form to Eloqua or Marketo for other purposes using custom action.

How can I implement adding multiple objects with Django form?

How I can go about adding multiple objects from a single form with Django?
For example: if I have a sales form which stores sold products' register.
Imagine the form:
datetime: [_____________]
customer: [_____________]
product: [______________] ---> How should I implement adding multiple products
in the same form?
cost: [_________________]
Save (button)
Hint: it is just a question, if you have some ideas tell me please because I don't know how to do it.
thanks
One way is to use a Formset. Formsets you may have seen, for example, in Django admin for displaying multiple related objects as inlines.
Another way could involve AJAX, example solution:
“Added products” is a simple <ul> list with products added to order
“Search product” is a plain text field, where user enters product name or SKU
User input is sent via AJAX to the server, which returns a list of relevant products
These product suggestions are displayed to user
When user clicks on a product name, another AJAX request is made to associate given product with the order
After request completes, the “Added products” list is refreshed via AJAX, and newly added product appears there
This would require that you first create a temporary order to which you could later attach products via separate requests. Formsets don't have this requirement, but personally I haven't used them a lot, they look a bit cumbersome.

Django: Save user-submitted data from ModelForm for admin review and committing at a later date

I have a form on my Django site (made with ModelForm) where users can submit some data to create new objects or modify existing objects. These data, however, need to be reviewed by our staff before they're committed to the database, sometimes in bulk at a later date.
I know I can use .save(commit=False) in my form-processing view to return an object that has not been saved to the database yet. But is there a way to collect all of these objects from multiple user submissions for later review? Ideally, I'd have an admin page that had a summary list of submissions with "Commit" or "Reject" buttons.
There's no one-step-out-of-the-box way to do this (at least not built in to Django), so you'll need to create the logic yourself, which should be pretty straightforward. Some approaches to consider:
Have a second model/table to which your form saves, then create a view for the review functionality which copies any approved records into the first table.
Avoid having second model/table and add a approved = BoolField(default=False) field to your model/table. Set objects to a custom manager which filters for default=True so the rest of your code will only see approved records by default. Have a second manager that does the opposite, i.e. filtering for unapproved records. Using this second manager, create a view for the review functionality which flips approved to True for anything that gets approved.
If, with the second approach above, you want use Django's admin site to do the review, create a proxy for your model which by default uses the second manager which filters for unapproved records. Then you can use the admin's inline display and editing functionality to see records at a glance and click approve as needed.