formset - How to display Model attribute as a fixed display string in Django InlineFormSet? -
edit: part of question has been answered here remaining part
i have django inlineformset allows users edit multiple score objects corresponding report. each score tied indicator (indicates area of performance being scored).
how can extract text of initial indicator choice in indicator dropdown choicefields , display them instead fixed, un-editable text labels score choicefields?
my models , views below.
models.py
class country(models.model): name = models.charfield(max_length=50) # indicators class indicator(models.model): target = models.foreignkey(target) number = models.charfield(max_length=5) # order integer indicating how areas should sequentially ordered # smallest integer should listed first order = models.integerfield(default=0) name = models.charfield(max_length=200) def __str__(self): if self.name: return self.value # reports class report(models.model): author = models.foreignkey('auth.user') country = models.foreignkey(country) created_date = models.datetimefield(default=timezone.now) # possible numeric score choice , associated display color class scorechoice(models.model): value = models.integerfield() name = models.charfield(max_length=50, null=true) color = models.charfield(max_length=10) description = models.textfield(blank=true, null=true) created_date = models.datetimefield(default=timezone.now) def __str__(self): if self.name: return str(self.value) # actual scores, value 1 of score choices # , belonging report , indicator class score(models.model): value = models.foreignkey(scorechoice, null=true) indicator = models.foreignkey(indicator, null=true) report = models.foreignkey(report, null=true)
views.py
def report_create(request, pk): indicators = indicator.objects.order_by('order') # create new report country , associated report form report = report(country=country.objects.get(pk=pk), author=request.user) report_form = reportform(instance=report) scoreinlineformset = inlineformset_factory(report, score, fields=('indicator', 'value'), extra=len(indicators), can_delete=false) formset = scoreinlineformset(instance=report, initial=[{'indicator': i} in indicators]) if request.method == 'post': report_form = reportform(request.post, instance=report) formset = scoreinlineformset(request.post) if report_form.is_valid(): created_report = report_form.save(commit=false) formset = scoreinlineformset(request.post, instance=created_report) if formset.is_valid(): created_report.save() formset.save() return redirect('report_detail', pk=created_report.pk) return render(request, 'ghsanavigator/report_create.html', {'formset': formset, 'report_form': report_form, 'indicators': indicators, 'country': country.objects.get(pk=pk).name})
----------
the first, , solved question summarized below reference (answered vgrubes)
how can change dropdown fields populated not "scorechoice object" actual score.scorechoice.value? same indicator dropdown fields.
i able achieve #2 simple modelform doing following, have no idea how inlineformset! , have no idea on how #1.
class scorechoicefield(modelchoicefield): def label_from_instance(self, obj): return "%s" % (obj.value) class scoreform(forms.modelform): value = scorechoicefield(queryset=scorechoice.objects.all()) ...
i can provide partial answer question. can specify unicode method in models return string instead of model object instance.
class indicator(models.model): target = models.foreignkey(target) number = models.charfield(max_length=5) order = models.integerfield(default=0) name = models.charfield(max_length=200) def __unicode__(self): if self.name: return self.name
this fix indicator object problem. , in similar manner can fix scorechoice object problem.
Comments
Post a Comment