django-formset Tutorial/Example #26
Replies: 8 comments 9 replies
-
I have been struggling to create one-to-many relation collections as well. I understand that you might be targeting more advanced users, but at this point I am about to give up on this promising project. |
Beta Was this translation helpful? Give feedback.
-
this is where I gave up. the instances show up, but each save / change creates new copies of the formset forms. For example, changing name from a to b will create two instances a and b. Can't seem to get the id saved, nor populated. Thank you for responding. models.py class JobService(CommonFields):
job = models.ForeignKey("Job", on_delete=models.CASCADE)
name = models.CharField(max_length=300, default="") forms.py class JobServiceForm(ModelForm):
id = forms.IntegerField(required=False, widget=forms.HiddenInput)
class Meta:
model = JobService
fields = ['name','id'] #also tried without id here, or only with id here and not above, same result
class JobServiceCollection(FormCollection):
min_siblings = 1
service = JobServiceForm()
"""tried to model this after the documentation on foreign keys, not sure if i understood it 100% model_to_dict would get None for id when I printed out the return of model_to_dict
"""
def model_to_dict(self, job):
opts = self.declared_holders['service']._meta
return [{'service': model_to_dict(service, fields=service._meta.fields)} for service in job.jobservice_set.all()] #couldn't initially understand that the _set.all() is what you mean here from the tutorial
def construct_instance(self, job, data):
for item_data in data:
try:
service_object = job.jobservice_set.get(pk=item_data['service']['id'])
except (KeyError, JobService.DoesNotExist):
service_object = JobService(job=job)
form_class = self.declared_holders['service'].__class__
form = form_class(data=item_data['service'], instance=service_object)
if form.is_valid():
if form.marked_for_removal:
service_object.delete()
else:
construct_instance(form, service_object)
form.save() views.py class JobCollectionView(FormCollectionView):
model = Job
collection_class = JobCollection
template_name = "jobs/job_edit.html" template <django-formset endpoint="{{ request.path }}" csrf-token="{{ csrf_token }}">
{{ form_collection }}
<button type="button" click="submit -> proceed !~ scrollToError">Submit</button>
</django-formset> |
Beta Was this translation helpful? Give feedback.
-
Did you try using this from the documentation: When looking at your |
Beta Was this translation helpful? Give feedback.
-
It was the first view I tried actually, but then concluded that maybe it wasn't meant for formsets, so i tried to replicate what you have on the examples website, and then tried to mix and match all, then gave up :) class JobEditView(FileUploadMixin, FormViewMixin, LoginRequiredMixin, UpdateView, IncompleteSelectResponseMixin):
model = Job
template_name = "jobs/job_edit.html"
form_class = JobForm
success_url = reverse_lazy("job-list")
extra_context = None
def get_object(self, queryset=None):
if self.extra_context["add"] is False:
return super().get_object(queryset)
def form_valid(self, form):
if extra_data := self.get_extra_data():
if extra_data.get("delete") is True:
self.object.delete()
success_url = self.get_success_url()
response_data = {"success_url": force_str(success_url)} if success_url else {}
return JsonResponse(response_data)
return super().form_valid(form)
|
Beta Was this translation helpful? Give feedback.
-
is there any way to render {{form_collection }} in field level. ? |
Beta Was this translation helpful? Give feedback.
-
I was using the Selectize widget for the city selection. I will try with removing the upload mixin. Would it help if I send a screencast showing what's happening ?
To be clear,
You are suggesting to use the crud view, and keep everything as I had and try to see if it works, right?
Thank you
…On Feb 9, 2023, 2:13 PM -0800, jrief/django-formset ***@***.***>, wrote:
You only need IncompleteSelectResponseMixin if you're using the Selectize widget.
Did you add some extra context to your buttons?
|
Beta Was this translation helpful? Give feedback.
-
I'm also struggling using FormCollections. It would be really helpful to get a simple example for saving, updating and deleting a One-To-Many FormCollection. |
Beta Was this translation helpful? Give feedback.
-
how can use i this as modal |
Beta Was this translation helpful? Give feedback.
-
Hi, I am new to django-formset and trying to learn and use it. But I am not able to create form collection with One-to-Many Relations. if you, please guide or share the code for https://django-formset.fly.dev/bootstrap/contact form collection. I am looking for:
1. add/update view for form collection
2. {{form_collection }} field level rendering
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions