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

**URL:** <https://forum.djangoproject.com/t/help-i-am-struggling-to-render-a-charfield-model-with-choices-on-my-html-code/36453>\
**Category:** Templates & Frontend\
**Created:** [November 15, 2024, 12:17pm UTC](https://forum.djangoproject.com/t/help-i-am-struggling-to-render-a-charfield-model-with-choices-on-my-html-code/36453 "2024-11-15T12:17:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fanelemenzi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fanelemenzi/32/24580_2.png) [@Fanelemenzi](https://forum.djangoproject.com/u/Fanelemenzi)\
**Post date:** [November 15, 2024, 12:17pm UTC](https://forum.djangoproject.com/t/help-i-am-struggling-to-render-a-charfield-model-with-choices-on-my-html-code/36453/1 "2024-11-15T12:17:16Z")

</div>

I have the following model in my model.py which used the Django choice package([django-choices · PyPI](https://pypi.org/project/django-choices/))

```auto
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

```auto
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

```auto
 <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.

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [November 15, 2024, 2:24pm UTC](https://forum.djangoproject.com/t/help-i-am-struggling-to-render-a-charfield-model-with-choices-on-my-html-code/36453/2 "2024-11-15T14:24:43Z")

</div>

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.

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

```

etc.

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [November 15, 2024, 2:31pm UTC](https://forum.djangoproject.com/t/help-i-am-struggling-to-render-a-charfield-model-with-choices-on-my-html-code/36453/3 "2024-11-15T14:31:46Z")

</div>

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](https://docs.djangoproject.com/en/5.1/ref/models/fields/#django.db.models.Field.choices). So, something like:

```python
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,
    )
```
