I am trying to display the version field from the below model other than the default str which is field2_name:
Note: This SO link Displaying a specific field in a django form might be more than I need but I am not 100% sure. I tried to implement this but was not successful.
Also note that I tried the example at https://docs.djangoproject.com/en/1.10/ref/forms/fields/ but was not able to get it to work
Model (Generic names):
class CodeVersion(models.Model):
field1= models.ForeignKey(SomeOtherModel, on_delete=models.CASCADE)
field2_name = models.CharField(max_length=256)
field3_description = models.CharField(max_length=1000, blank=True)
version = models.PositiveIntegerField()
def __str__(self):
return self.field2_name
Form:
class VersionsForm(forms.Form):
code_versions = forms.ModelChoiceField(queryset=CodeVersion.objects.none())
def __init__(self, SomeOtherModel_id):
super(VersionsForm, self).__init__()
self.fields['infocode_versions'].queryset = CodeVersion.objects.filter(SomeOtherModel_id=SomeOtherModel_id)
This works - it returns field2_name as it is supposed to.
How do I return version instead - what is the simplest way?
Any help or guidance is appreciated.