@@prettyprinted David's suggestion is great!. Maybe you can build up on the django documentation concerning modelform_factory', 'modelformset_factory' and 'inlineformset_factory'. So you don't have to explain everything explicitly and the student will be able to read up things, he doesn't understand. Source code (perhaps on github) would be great, too!
something like add a + or - symbol next to each entry and upon pressing it i can add or delete items. use case -1 a user wants to add multiple phone numbers and a description. phonenumber1 home - phonenumber2 mobile - phonenumber3 office - phonenumber4 mobile2 +
Hi Anthony, You are a great teacher. I like the easy informal tone. A wish for me would be formset combined with Select2 dropdown fields. I have a hard time getting this to work. Thanks in advance!
Here is my views.py: paste.ubuntu.com/p/hgR7vXmYBp/ And here is my models.py: paste.ubuntu.com/p/ttVSzFWG24/ I am able to add single product under one "ref_no" as usual. Now i wanted to add multiple products under one "ref_no" which is in Order model. How can i do this using Django formset?
For a modelformset_factory, with a parent and child table , form.save(commit=False) gives an instance of the parent model. How do I loop through the instances of child model that is about to be saved?
please try to explain this issue why when I use formsets to update images I used the extra argument to create just 3 image fields however I got more than one image when I update the formsets. you can create a user and create a product then updated the sub-images many times to see the image field will repeat itself and don't save just 3 images: this is an example djolx.pythonanywhere.com/details/34/
A practical, real world use case would make this sooooo much better. As it stands, why would ANYONE want extra blank copies of the forms some user has already filled in? It's pointless. Give it some thought, I am sure you can do better.
I support your project to teach us, it would be nice if the extra was created dynamically by the user at the click of a button for example. Can you do this in templates outside of admin?
Am work on a project where am working with formset and your video was very helpful but how can i save several instance because when i save the form only the first record is saved. Below is the view code def add_prescription_to_patient(request,pk): patient = get_object_or_404(Patient, pk=pk) PrescriptionFormSet = modelformset_factory(Prescription, fields = ('medication','number_of_days','times_daily','total','instructions',), extra=2) if request.method == 'POST': form = PrescriptionFormSet(request.POST) instances = form.save(commit=False) for instance in instances: instance.patient = patient instance.user = request.user instance.save() return redirect('patient_prescription_detail', pk=patient.pk) else: form = PrescriptionFormSet(queryset = Prescription.objects.none()) return render(request, "product/prescription_form1.html",{"form": form})
Hi, I had made two models, and trying to save both using forms, but nothing happens and no error. Also tried printing the data but seems nothing is printed. Not sure where the data is disapperaing. Please help. below is the model.py class Profile(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) class FamilyMember(models.Model): profile = models.ForeignKey(Profile, on_delete = models.CASCADE) name = models.CharField(max_length=100) relationship = models.CharField(max_length=100) and the view.py as below. def profileAdd(request): ProfileFormSet = modelformset_factory(Profile, fields = ('first_name', 'last_name', ), extra = 1) FamilyFormSet = inlineformset_factory(Profile, FamilyMember, fields = ('name','relationship',), can_delete = False, extra = 1) form_profile = ProfileFormSet(queryset = Profile.objects.none()) form_family = FamilyFormSet() if request.method == 'POST': if form_profile.is_valid(): profile = form_profile.save() print (profile) if form_family.is_valid(): instances = form_family.save(commit = False) form_family.profile = profile.id # form_family.save() print(profile.id) print(instances) return redirect('profile-detail', profile_id = profile.id) return render(request, 'dynamicaddfields/profile-add.html', {'form_profile' : form_profile, 'form_family': form_family})
You are the reason, i have been able to get this far in django and flask. Could you tell me how to use this in CreateView and with single form, update multiple Models(Example, Example1, Example2, where these are related tables(Example2 has Example and Example1 as foreign keys).
can you show how to manual print out a formset from the modelformset factory or any class based view. I would like to add in like a plus sign to add more instead of having them there by default and also a - sign to remove one you do not want. This is similar to the admin page in django. Can this be done in modelformsets.
thanks for putting up these series, i am trying to create a page where a user can create a question and give its options set and on the page he should be able to add more questions dynamically and for each question form he can add as many options as he wants and finally submit the full page Data as json , how do it do it, i wouldn’t want to get the entire code but a little help with the things that are required like formset ,ajax calls and etc
can you make a video about formsetfactories usage.how to use formset factories and how to make multiple rows for a form with no repeattitions of table headings
I created 3 forms from the same model, with different fields in each form. I am not sure how to submit the forms as each form has only specific fields and all the model fields are mandatory.
I support your project to teach us, it would be nice if the extra was created dynamically by the user at the click of a button for example. Can you do this in templates outside of admin?
clean and thorough as always...I always learn something new from your videos...please do something on developing for rest framework (drf)upto hosting incorporating both normal Django and DRF .....
How would this be applied in a case where you have a modelform already defined in forms.py, like you did in the video "Creating Forms from Models [...]", and you want to turn that into a model formset? Could you just call that modelform instead of specifying (Example, fields='name', 'location')?
It would be pretty nifty if you A) Covered how to pass database error messages back to the user B) Covered how to use the DOM to store the rows locally until it was time to submit an entire batch (to minimize round trips), C) told malik why people need multiple copies of the same form, since he didn't get it the first time around, D) covered the redirect pattern that prevents people from submitting the set of new form values twice after refreshing the page or hitting the back button, E) Covered how to override common methods like form.save or the way it the form responds to enter. Personally I want my form to shift from field to field every time someone enters a space, and simply go to the next row of values down when the user hits enter. Then there's F) How to add keyboard shortcuts so that a specific key combination "presses" a specific button. Also, I agreed with David: Cover the differences between the three types of factory.
Can you please make a form with different users to have different permissions accessing fields in the form. Example- I have a store with multiple locations, I want the user from his location to have access to edit his field but only to view others location field.
Example on a page contains a questionnaire with more than one form with a condition that each form accepts multiple tabulated multi-fields with more than one row.
please a do a video about how to redner a many to many model using forms. scenario : one author can have many books, a book can have many authors. model>form>view>template.
Nice video. My request for the next video: Use django forms to select object(Example objects(3), Example objects(2)......... ) and then show his data ( from displayed video, that would be 'Name' and 'Location' values ) in web browser.
Yes, something like that. Why am I asking this? I have objects lat = 50 & lon = 19, lat = 50 & lon19.1(~6000 combinations). When I open one of these objects, I see the information about that point (in which country it is located, at what height it is). How to use forms(or some other way), specify a specific object lat&lon and display the data of a point in the web browser. Thanks for your reply
It would be easiest to create two form objects but have one form in your HTML. Then you can insert data into each model in your view where you handle the form data.
I'm new in Django, How to display the list of the current model in the same form and load the data while clicking in the submitting button and clear the form fields?