How can I implement adding multiple objects with Django form? - django

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.

Related

How can i confirm a database entry in django admin?

I have been working around this problem. I have a user submitting a HTML form lets say to list their hotel on my website. I need to review this form before i add it into my hotels model to be published.
One approach i have worked on is to use a requests model where this form can be stored and later using django admin action write a custom action to add/remove the request. In case of acceptance i copy the details to my hotels model else it sends an email or a notification back to user.
Second approach is simply using django action on hotels model where the request is sent to approve it or reject it. In this case i want to know if thats possible where a data point doesn't get written to database until it's been accepted by admin. If yes how can i do that?
Finally, these details are displayed on my main page and search page for users to book these places.
If there is a better and effective way to do it. Please share that.
Thanks in advance. If something isn't clear i can answer your specific questions in the comments below 😊.
Have a nice day.
You can have is_published Boolean field in your hotel model and you can default it to false initially. After you inspect the hotel details you can set the is_published field to True from django admin.
So now whenever you are querying for hotels to show on your website. You can query
Hotel.objects.filter(is_published=True)

Oracle Apex Additional Forms

I am working to achieve a flexible form region where a user can add additional forms. My tables are users and wallets, a user can have many wallets. The end goal is to allow a user to add as many wallets they would like in one page, to do this I will need to add Dynamic Forms. Normally in the MEAN stack, It is common for us to loop the form, so if a user clicks on a button it will generate a form and send the JSON Request as an array. I don't know how to get this done as I am a beginner in Oracle Apex

Django ModelChoiceField, thousands of options , not user friendly

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.

Session or Model Field in Django?

I'm a beginner in Django. When developing an app, I want to fulfill the following functionality: There is some pictures in one webpage. A user can 'like' a picture by clicking a button bellow it. But one user can only like a specific picture once.
Now there seem two methods to do this.
1) Set an attribute in the session. So I when a user click a button, I can check if he has already 'liked' this picutre according to this session.
2) Add a new field in my user's model to record which pictures he has 'liked'.
Then I don't know which one to use. My questions are as follows:
For method (1), session can expire after some time (e.g. 2 weeks).
So for a user who revisits my website after 2 weeks, is it true that
I can not prevent him from re-liking the picture he's already 'liked'
before?
If I want to have access to the info about which pictures a user
has 'liked', is it true that I can only use method (2) to store this
information?
Thanks a lot!
If you want the "favorites" to persist across multiple sessions, then yes, you need to store the data somewhere that isn't volatile. A simple solution is to use a separate model, a LikedPicture for example:
class LikedPicture(models.Model):
user = models.ForeignKey(User, db_index=True)
picture = models.ForeignKey(Picture)
Session expired? No problem, just get the ones they've liked from the model. You could take it a step further and make the related model generic, so you don't have to make a separate model to hold each association, if you have several different models you're going to associate similarly.
Want to make sure the user only favorites something once? Django makes this ridiculously easy with get_or_create():
favorited_picture, created = FavoritedPicture.objects.get_or_create(user=user,
picture=picture, defaults={'user': user, 'picture': picture})
I find this method to be much more straightforward than trying to maintain a comma-separated-field on a model to store the ids of the favorited things.

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.