Question

I can't figure out how to format the output of a ModelChoiceField when only 2 or 3 columns are selected.

Here is my model:

class ActsIdsModel(models.Model):
    releveAnnee = models.IntegerField(max_length=4, blank=False, null=False)
    releveMois=models.IntegerField(max_length=2, blank=False, null=False)
    noOrdre=models.IntegerField(max_length=2, blank=False, null=False)
    ...
    #many other fields
    ...
    def __unicode__(self):
        releveAnnee=vn.variablesNameDic['releveAnnee'] + "=" + str(self.releveAnnee)
        releveMois=vn.variablesNameDic['releveMois'] + "=" + str(self.releveMois)
        noOrdre=vn.variablesNameDic['noOrdre'] + "=" + str(self.noOrdre)
        return releveAnnee + ", " + releveMois + ", " + noOrdre

The code below works but I get all the columns (so not efficient for my purpose):

class ActsAddForm(forms.Form):
    actsToValidate=forms.ModelChoiceField(queryset=ActsIdsModel.objects.filter(validated=0))

This works

But the code below does not work:

class ActsAddForm(forms.Form):
    actsToValidate=forms.ModelChoiceField(queryset=ActsIdsModel.objects.values("releveAnnee", "releveMois", "noOrdre").filter(validated=0))

This does not work!!

How to fix the problem? It seems that when I choose the columns the unicode function of my model is not called anymore. Right?

Was it helpful?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top