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.