HELP! I am struggling to render a Charfield model with choices on my html code

I have the following model in my model.py which used the Django choice package(django-choices · PyPI)

class VehicleStatus(models.Model):

	class AccidentHistory(DjangoChoices):
		No_History = ChoiceItem('NHA','No History of Accidents')
		odometer_fraud = ChoiceItem('OIIA', 'Once Involved In Accident')
		theft_involvement =ChoiceItem('CAD', 'Currently Accident Damaged')


	vin_number = models.OneToOneField(Vin, on_delete=models.CASCADE, related_name='vehiclestatus')
	accident_history = models.CharField(max_length=30, 
	choices=AccidentHistory.choices, default=AccidentHistory.No_History)
	owner_history = models.IntegerField()

	# Convert the vin_number to an integer if needed (use int() only if vin_number is numeric)
	def __str__(self):
		return f"Vehicle Status for VIN {self.vin_number}"

here is my views.py

def search(request):
	vin = request.GET.get('vin_number')
	vehicle = None
	specs = None
	inspections = []
	history_records = []
	vehicle_status = None


	if vin:
		vehicle = get_object_or_404(Vin, vin_number=vin)

		#Retrieve realted model data
		specs = getattr(vehicle, 'specs', None)
		inspections = vehicle.inspections.all()
		history_records = vehicle.history.all()
		vehicle_status = getattr(vehicle, 'vehicle_status', None)

	return render(request, 'search.html', {
		'vehicle':vehicle,
		'specs':specs,
		'inspections':inspections,
		'history_records':history_records,
		'vehicle_status':vehicle_status,
	})

and here is my html

 <div class="col-md-4">
                                                    <div class="card" style="width: 18rem;">
                                                        <div class="card-body">
                                                        <h2 class="card-title form-control-plaintext" value="Read-only text" style="color:red">{{ vehicle_status.get_accident_history_display }}</h2>
                                                        </div>
                                                    </div>
                                                </div>

When I run the server and it get no display on the vehicle_status.get_accident_history_display. I am using the Choices packaging because the normal choices also didn’t work. I am open to reverting back to the norma charfiled with choice.

Can you show the code for the Vin model, which is the one you’re getting in the view?

A suggestion for the view: you don’t need to do any of the definition of specs, inspections, history_records or vehicle_status in the view, and pass them in the context. You could access all of those in the template, using the vehicle that you pass in.

e.g.

Specs: {{ vehicle.specs }}
<br>
Inspections:
<ul>
  {% for inspection in vehicle.inspections.all %}
    <li>{{ inspection }}</li>
  {% endfor %}
</ul>

etc.

Oh, and also, django-choices’ README says “New projects should not use this package”. It’s not up to date. You should be able to do everything you need with Django’s standard choices. So, something like:

class VehicleStatus(models.Model):

    class AccidentHistory(models.TextChoices):
        NONE = "NHA", "No History of Accidents"
        ONCE = "OIIA", "Once Involved in Accident"
        CURRENT = "CAD", "Currently Accident Damaged"

    accident_history = models.CharField(
        max_length=4,
        choices=AccidentHistory,
        default=AccidentHistory.NONE,
    )
1 Like