I'm trying to use together autocomplete_light and django-dynamic-formset: both work alone but I'm not able to let them work together... let's start with the code...
models.py:
class Tmp_User(models.Model):
userName = models.CharField(max_length=200, blank=True, null=True,)
firstName = models.CharField(max_length=200, blank=True, null=True,)
lastName = models.CharField(max_length=200, blank=True, null=True,)
...
class Project(models.Model):
title = models.CharField()
...
class Segment_in_Project(models.Model):
name = models.CharField()
the_project = models.ForeignKey('Project')
translator = models.ForeignKey('Tmp_User')
...
forms.py
autocomplete_light.autodiscover()
class AddProjectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AddProjectForm, self).__init__(*args, **kwargs)
class Meta:
model = Project
fields = '__all__'
class AddSegmentForm(autocomplete_light.ModelForm):
def __init__(self, *args, **kwargs):
super(AddSegmentForm, self).__init__(*args, **kwargs)
class Meta:
model = Segment_in_Project
autocomplete_exclude = ['the_project']
fields = '__all__'
SegmentFormSet = formset_factory(AddSegmentForm)
autocomplete_light_registry.py
autocomplete_light.register(Tmp_User,
search_fields=['^firstName', 'lastName'])
and views.py
class ProjectCreateView(CreateView):
model = Project
template_name = 'home/formset-table.html'
form_class = AddProjectForm
success_url = '/admin'
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
formset = SegmentFormSet()
return self.render_to_response(
self.get_context_data(
form_Project=form,
formset_Segment=formset))
def post(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
formset = SegmentFormSet(self.request.POST)
if (form.is_valid() and formset.is_valid()):
return self.form_valid(form, formset)
else:
return self.form_invalid(form, formset)
def form_valid(self, form, formset):
self.object = form.save()
self.object.save()
formset.instance = self.object
formset.save()
return super(ProjectCreateView, self).form_valid(form)
def form_invalid(self, form, formset):
return self.render_to_response(
self.get_context_data(
form_Project=form, formset_Segment=formset))
The problem is that the formset instance is not "bound" to the Project model and the validation fail: the error tells that the_project
is not defined... I tried to save the form first, get the id of the new saved project and pass it to the formset together with the request.POST when I istantiate the formset... but it didn't work...
on the other hand, using the inlineformset_factory(Project,Segment_in_Project)
instead of formset_factory
breaks the autocomplete_light functionality... (I don't know how to use autocomplete_light.ModelForm
for the Segment_in_Project formset)
Any idea or suggestions on how to do it? Thanks