Home > Enterprise >  In a Django Forms drop-down list, how do I show text accessed via a foreign key?
In a Django Forms drop-down list, how do I show text accessed via a foreign key?

Time:07-01

I have code to edit a table "instruments", which has a foreign key to another table 'instrumenttype'. I'd like to show a drop-down list showing the text values from the instreumenttype table. My problem is that the drop-down list shows "InstrumentType object(n)" instead of the instrument type description from the other table. The update works fine; when I get the selected value and update the instruments table, the correct key id is put in.

Here is the form:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from instruments.models import Instrument
from instrumenttypes.models import InstrumentType

# Form used to edit the data pertaining to a specific instrument
class EditInstrumentForm(forms.ModelForm):
    instrumenttype = forms.ModelChoiceField(queryset=InstrumentType.objects.all())
    class Meta:
        model =  Instrument
        fields = ('instrument', 'dateAdded', 'dateRemoved', 'nickname', 'serialNo', 'status',
              'instrumenttype')

Here is the view snippet:

def update_instrument_view(request, id=id):
    instrument = get_object_or_404(Instrument, id=id)
    if request.method == 'POST':
        form  = EditInstrumentForm(request.POST, request.FILES, instance=instrument)
        if form.is_valid():
            instrument.instrument = form.cleaned_data.get('instrument')
            instrument.dateAdded  = form.cleaned_data.get('dateAdded')
            instrument.dateRemoved = form.cleaned_data.get('dateRemoved')
            instrument.nickname  = form.cleaned_data.get('nickname')
            instrument.serialNo  = form.cleaned_data.get('serialNo')
            instrument.status    = form.cleaned_data.get('status')
            instrument.instrumenttype = form.cleaned_data.get('instrumenttype')
            instrument.save()
            messages.success(request, 'Your instrument has been updated.')
            return redirect('instrument_details', instrument.id)
        else:
            form = EditInstrumentForm(instance=instrument)
            messages.error(request, 'Issue updating data.')
            return render(request, 'instrument_update.html', {'form': form, 'instrument':instrument})
    else:
        form = EditInstrumentForm(instance=instrument)
        return render(request, 'instrument_update.html', {'form':form, 'instrument': instrument})

The drop down list shows the object reference, but I want it to show instrumenttype.instrumentType (which is a text description). snapshot of form output

Here is the model of the instrument type table:

from django.db import models

from instrumenttypes.models import InstrumentType
from stations.models import Station

# Create your models here.
class Instrument(models.Model):
    instrument = models.CharField(max_length=40)
    instrumenttype = models.ForeignKey(InstrumentType, on_delete=models.CASCADE, null=True)
    station = models.ForeignKey(Station, on_delete=models.CASCADE, default=1)
    serialNo = models.CharField(max_length=60, null=True, blank=True)
    dateAdded = models.DateTimeField("Date Added", null=True, blank=True)
    dateRemoved = models.DateTimeField("Date Removed", null=True, blank=True)
    status = models.CharField(max_length=10, null=True, blank=True)
    nickname = models.CharField(max_length=40, null=True, blank=True)

Here is the model of the instruments table:

from django.db import models

from instrumenttypes.models import InstrumentType
from stations.models import Station

# Create your models here.
class Instrument(models.Model):
    instrument = models.CharField(max_length=40)
    instrumenttype = models.ForeignKey(InstrumentType, on_delete=models.CASCADE, null=True)
    station = models.ForeignKey(Station, on_delete=models.CASCADE, default=1)
    serialNo = models.CharField(max_length=60, null=True, blank=True)
    dateAdded = models.DateTimeField("Date Added", null=True, blank=True)
    dateRemoved = models.DateTimeField("Date Removed", null=True, blank=True)
    status = models.CharField(max_length=10, null=True, blank=True)
    nickname = models.CharField(max_length=40, null=True, blank=True)

I have read a lot of docs and related answers about this, but so far have not found a solution... frustrating because it seems like such a simple thing...

CodePudding user response:

override the __str__ method on your model to return the appropriate description, like;

def __str__(self):
    return self.instrumentType

(note that you have your Instrument model twice in your question, not the InstrumentType model you mention, so not 100% sure that's the right field to access, but should be easy enough for you to make work if not)

If you wanted to call a custom method instead of relying on the __str__ behaviour, see https://docs.djangoproject.com/en/4.0/ref/forms/fields/#django.forms.ModelChoiceField.iterator:


from django.forms import ModelChoiceField

class InstrumentTypeModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return f"choice: {obj.instrumentType}"
  • Related