0

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

Attilio
  • 77
  • 1
  • 9

1 Answers1

0

OK. After a LOT of tentative I found a solution. It's not perfect in the sense that it does what I need but I'm still having the sensation that there should be something better...

Anyhow what I did is hack completely the post function: I save the ProjectModel first, then I get the id of the saved project, then I read the formset POST info (which doesn't have any info on the ID of the project just created), get the info that I need and build a new formset with the proper project ID and then I save it. Code below:

def post(self, request, *args, **kwargs):
    self.object = None
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    form.instance.created_by = self.request.user
    formset = SegmentFormSet(self.request.POST)
    if form.is_valid():
        self.object = form.save()
    newdata = {}
    for idx, form in enumerate(formset):
        for field in form.fields:
            print idx, field, form[field].value()
            if field == 'the_project':
                tmp_field = u"form-%s-the_project"%(idx)
                newdata[tmp_field] = self.object.id
            elif field == 'id':
                pass
            else:
                tmp_field = u"form-%s-%s"%(idx,field)
                newdata[tmp_field] = form[field].value()
    newdata[u"form-MAX_NUM_FORMS"] = u"1000"
    newdata[u"form-MIN_NUM_FORMS"] = u"0"
    newdata[u"form-INITIAL_FORMS"] = u"0"
    newdata[u"form-TOTAL_FORMS"] = len(formset)
    newdata[u"title"] = self.object.title
    new_formset = SegmentFormSet(newdata)
    if new_formset.is_valid():
        return self.form_valid(form, new_formset)
    else:
        return self.form_invalid(form, new_formset)

As you see, the newdata DICT is exactly what I need (including the project ID) for correctly saving the SegmentFormSet.

If someone has a better solution... please let me know

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Attilio
  • 77
  • 1
  • 9