Hidden select multiple items Django - django

I've got several forms in my django app that require support for attachments. Each form instance may have any number of attachments, including none. I want to present a jQuery based upload widget for managing these uploads, allowing the uploads to be processed asynchronously. The attachments are stored in their own model, so there is then a many-to-many from the attachments model to each model that requires attachments. When an attachment is sucessfully uploaded and processed, the view handling the upload will return the id in the attachments model, which will then be inserted into a hidden field on the form. I'm currently trying to decide how best to represent this in the form.
One method would be to simply have a single hidden input which takes a comma separated list of ids. This would then require quite a lot of manual processing and validation on submission, which I can't help feeling could be avoided.
Elsewhere, I've used a HiddenInput for a single value where I'm doing something similar and dynamically adding items to the related model in the form. I can't however see how I can extend this directly to a Many to Many from a simple Foreign Key.
Anyone able to suggest the best way to go about doing this?

Try to use formsets or model_formsets to create a form for creating/editing multiple objects, also you can use javascript to add forms dynamically in your browser.

Related

How to build conversational form with django?

I am trying to build a conversational form with Django.
It will be used in landing page. The form questions will be loaded one by one as user answers them. And there will be some greeting and "human-way" responses to user input (such as "wow! you did a good choice!" after user selects one of the choices from form). The experience and look of the app will be like a real-time chat but user can only select one of the choices from form or upload a file/image.
1. Which technology is better to use for it? I am planning to do it with Fetch.
2. Since I want it to work without page reloading, how do I need to load Django forms through Fetch? Do I need to pass elements of it with JSON and construct it in client-side or can I just pass it as an html with {{form.as_p}} and display it in HTML?
Does these options make difference in matter of security?
I do not know anything about Fetch, but anyway I think it must be constructed clientside, but at first I would simply display the form in a template to get the ids of its fields and then use it in clientside code.
What about security - you'll need to pass the csrf token via your form.

How to Fill Form with Data in DJango without Update

New to Django here. I have a link to a form in DJango. I use the CreateView to have the user enter the initial information. It all works great and the data is accurately saved to the database. My issue is this: I would like that same link to open the form (it's a one-to-one relationship) with the filled data so the user can see what they have previously entered and correct, edit or update as needed. The form currfently opens as a blank form so if the user has entered that information previously they are unable to see it. I cave researched get_or_create and update_or_create as well as a number of other topics, but can't seem to figure this out. This needs to be a user-friendly experience so multiple entires or clicking multiple buttons to access the data is not an option. How best can I implement this?
#Don you can checkout django formsets, I think this will help in this situation. And you can use a single FormView for all your needs by overriding its methods.
Have you looked at Django Sessions. It’s a simple way of saving session data and passing the data to future requests. https://docs.djangoproject.com/en/3.0/topics/http/sessions/. I. In your form view you cloud save the session data you want to pass to your next form. In your next form, you could use the session data as default values. I’ve done something similar in the past.

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 Rest Framework Hyperlinked Fields Understanding

I can't seem to grasp the difference between HyperlinkedIdentity and HyperlinkedRelated Fields. I have a few questions that I can't seem to find the answers to online.
What is the actual difference? When would I want to use one vs. the other.
My next question is say I have 2 models, Project and Task.
A Task has a ForeignKey to Project. If I wanted the Project to hyperlink to the tasks within it, which Hyperlink field would I put in the ProjectSerializer? And what field would I put in the TaskSerializer to complement the ProjectSerializer assuming I wanted the tasks to be able to hyperlink back to the Project they are related to?
What is the difference between using the hyperlinked fields vs. just using regular nested serializers? When using hyperlinked fields, can I still filter by pk/id?
Last, What if a model had two hyperlinked relations in the serializer? From what I understand it creates a url field for each hyperlink, would it create two url fields in this case?
Thanks for any clarification you can offer, it will be a huge help towards cementing my understanding on the subject and allowing me to complete my API for my project.
What is the actual difference? When would I want to use one vs. the other.
HyperlinkedIdentityField is an hyperlink field for the current object itself while HyperlinkedRelatedField represent an hyperlink to other instances.
A Task has a ForeignKey to Project. If I wanted the Project to hyperlink to the tasks within it, which Hyperlink field would I put in the ProjectSerializer? And what field would I put in the TaskSerializer to complement the ProjectSerializer assuming I wanted the tasks to be able to hyperlink back to the Project they are related to?
HyperlinkedRelatedField is what you're looking for.
What is the difference between using the hyperlinked fields vs. just using regular nested serializers?
Hyperlinks can be browsed independently from the original resource. Handy if one of them belongs to another system. For example, you'll likely want to use hyperlink to tweets rather than let your server fetch them and them return them nested. Hyperlinks also allows the client to deal with its own caching rather than sending back all the data. Could be handy in case of fetching a list of items that nest the same user.
On the other hand, hyperlinks increase the network request count because it needs to fetch more data.
When using hyperlinked fields, can I still filter by pk/id?
Not sure what you mean here.
What if a model had two hyperlinked relations in the serializer? From what I understand it creates a url field for each hyperlink, would it create two url fields in this case?
Correct. hyperlinked relation are just a representation of a relation. It provides an hyperlink (an uri) to fetch the associated object.
This is useful because you won't need to know the pattern to fetch an object from the id: with a PrimaryKeyRelatedField you'll get the id but are missing the url to fetch the associated object.
This also allows the server to manage its own uri space without the need of updating the clients.
Hope this will help.

How do you upload multiple images to a blog post using django generic views?

I am trying to let my users upload multiple pictures to their blog posts. I have created a model for the blog post and a separate model for the images and used a foreign key to relate them. I was planning on using dropzone.js so that the user can drag and drop the pictures. I have looked into using formsets but can't get my head around them. Can anyone explain to a django beginner how to go about doing this? Or better yet give an example? Thanks!
I have done something similar using jquery-fileupload. Never used dropzone.js so maybe the explanation is not directly equivalent, but this is roughly what you do.
If you don't want to use formsets, upload the images via AJAX and add hidden fields to the form with the primary keys of the uploaded images, so you can attach them when saving the blogpost in Django. You will have to deal with orphaned images (those that were uploaded but the blogpost was never saved.)
If you want to use formsets, Django expects a format in the posted form data, you just need to make sure you create in your html (with dropzone js templating) the apropriate format Django is expecting, and also incrementing/decrementing the counter of the formset's management form.
Neither way is trivial, you need to pick a path and bang your head a few times before having it working.