Question

I am trying to add extra lines in my Form but it seems to be getting ignored. I have a similar set up as django inlineformset_factory extra attribute being ignored? but without the base model. I have tried to implement that solution but was not successful. Does anyone have a solution to add extra lines in my form?

Here is my code:

Models.py

class Disease(models.Model):
    disease = models.CharField(max_length=300, verbose_name="disease")

    def __unicode__(self):
      return self.disease


class Indicator(models.Model):
    relevantdisease = models.ForeignKey(Disease, verbose_name="disease")       
    indicator = models.CharField(max_length=300, verbose_name="indicators")

    def __unicode__(self):
      return self.indicator

Forms.py

MAX_INDICATORS = 3

class DiseaseForm(forms.ModelForm):
    class Meta:
       model = Disease

class IndicatorForm(forms.ModelForm):
    class Meta:
       model = Indicator

DiseaseFormSet = inlineformset_factory(Disease, 
    Indicator,
    can_delete=False,
    extra=MAX_INDICATORS,
    form=DiseaseForm)

IndicatorFormSet = inlineformset_factory(Indicator,
    IndicatorValue,
    can_delete=False,
    extra=MAX_INDICATORS,
    form=IndicatorForm)

Views.py

def drui(request):

if request.method == "POST":

   indicatorForm  = IndicatorForm(request.POST)

   if indicatorForm.is_valid():
      new_indicator = indicatorForm.save()
      diseaseInlineFormSet = DiseaseFormSet(request.POST, request.FILES, instance=new_indicator)

      if diseaseInlineFormSet.is_valid():
         diseaseInlineFormSet.save()
         return HttpResponseRedirect(reverse(something))

else:
   indicatorForm = IndicatorForm()
   diseaseInlineFormSet = DiseaseFormSet()

return render_to_response("something.html", {'indicatorForm': indicatorForm, 'diseaseInlineFormSet': diseaseInlineFormSet},context_instance=RequestContext(request))  

Updated code: Now that 1 disease will be linked to multiple indicators, I can get extra lines. However, now the problem is that whenever I save my indicators and choose a disease from the database, a NEW disease will be written to the db that links to the indicators.

Views.py

def drui(request):
if request.method == "POST":

   diseaseForm  = DiseaseForm(request.POST)

   if diseaseForm.is_valid():
      new_disease = diseaseForm.save(commit=False)
      indicatorInlineFormSet = IndicatorFormSet(request.POST, instance=new_disease)

      if indicatorInlineFormSet.is_valid():
         new_disease.save()
         indicatorInlineFormSet.save()
         return HttpResponseRedirect(reverse(valdrui))

else:
   diseaseForm = DiseaseForm()
   indicatorInlineFormSet = IndicatorFormSet(instance=Disease())

return render_to_response("drui.html", {'diseaseForm': diseaseForm, 'indicatorInlineFormSet': indicatorInlineFormSet},context_instance=RequestContext(request)) 

forms.py

class DiseaseForm(forms.ModelForm):
    disease = forms.ModelChoiceField(queryset=Disease.objects.all())
    class Meta:
       model = Disease

class IndicatorForm(forms.ModelForm):
    class Meta:
       model = Indicator  

IndicatorFormSet = inlineformset_factory(Disease, 
    Indicator,
    can_delete=False,
    extra=MAX_INDICATORS)

HTML:

{{ diseaseForm.as_table }}
{{ indicatorInlineFormSet.as_table }}
Was it helpful?

Solution

In your view you have a single IndicatorForm for a single Indicator instance, along with a DiseaseFormSet to bind multiple Disease instances to that single Indicator. Yet in your models.py you have a ForeignKey from Indicator to Disease, meaning that an Indicator can only be bound to one single Disease instance, while a Disease instance can have as many Indicators as you want.

To fix it, change the ForeignKey from being in the Indicator model to the Disease model, to being in the Disease model to the Indicator model - or if you want it the other way around, stick with the models you've got and use an IndicatorFormSet with a single DiseaseForm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top