Home > Mobile >  Django.db.models.field.CharField
Django.db.models.field.CharField

Time:08-07

I have a Django project that I'm trying to cobble together for a very basic survey that needs to be entered for demographic information. I've dummied up some data through the admin site in order to get my ListView working for the time being.

Unfortunately, I'm getting a strange response. I built the models.py to be a drop-down of a full spelled out and then to keep it small, I've made the actual storage in the db 1-3 characters. When I get my list view, one that uses 1 character to store for gender presents as (<django.db.models.fields.CharField>,), I would like for it to present as as the larger name of "Male", "Female", "Non-binary".

However, for my race and ethnicity fields which are 2 & 3 characters, those are returning as the actual storage for the DB (i.e, 'AA', 'NHP'). I'm more comfortable with SQL, so I'm not opposed to adding a table for a key as there would only be 11 entries for now, but it is possible that more things could be added down the line.

models.py:

from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
# Create your models here.

class StuData(models.Model):
    id_num = models.IntegerField(primary_key=True)
    entry_date = models.DateTimeField('Date of Entry')
    MALE = 'm'
    FEMALE = 'f'
    OTHER = 'x'
    GENUNK = 'u'
    GENDER_SELECTIONS = [
        (MALE,'Male'),
        (FEMALE,'Female'),
        (OTHER,'Non-Binary'),
        (GENUNK,'Unknown'),
    ]
    gender = models.CharField(max_length=1, choices=GENDER_SELECTIONS),
    ## Build the selections for the race field
    AFR_RACE = 'AA'
    ASI_RACE = 'AS'
    WHI_RACE = 'WH'
    UNK_RACE = 'UN'
    RACE_SELECTIONS = [
        (AFR_RACE, 'African-American'),
        (ASI_RACE, 'Asian/Pacific Islander'),
        (WHI_RACE, 'White'),
        (UNK_RACE, 'Unknown Race'),
    ]
    race = models.CharField(max_length=2, choices=RACE_SELECTIONS)
    ## Build the selections for the ethnicity field
    HSP = 'HIS'
    NHP = 'NHP'
    UNK = 'UNE'
    ETHNICITY_SELECTIONS = [
        (HSP, 'Hispanic Origin'),
        (NHP, 'Not of Hispanic Origin'),
        (UNK, 'Unknown Ethnicity'),
    ]
    ethnicity = models.CharField(max_length=10, choices=ETHNICITY_SELECTIONS)
    stu_count = models.IntegerField(default=1)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ["cad_num"]

    def __str__(self):
        return self.name

views.py:

from django.shortcuts import render
from django.http import HttpResponse
from mvstop.models import StuData
from django.views.generic.list import ListView
from django.views.generic.edit import UpdateView, CreateView
# Create your views here.

class StuDataList(ListView):
    model = StuData
    template = "myapp/studata_list.html"
    fields = ""
    paginate_by = 25

    def Meta():
        ordering = ['id_num']

    def __str__(self):
        return self.name

studata_list.html:

<!DOCTYPE html>
{% extends "./base.html" %}
{% block content %}
<h3>Most recent entries</h3>

<table id="studata">
    <tr>
        <th>ID Number</th>
        <th>Entry Date</th>
        <th>Gender</th>
        <th>Race</th>
        <th>Ethnicity</th>
        <th>Count</th>
    </tr>
    {% for stu in studata_list %}
    <tr>
    <tr>
        <td>{{ stu.id_num }}</td>
        <td>{{ stu.entry_date }}</td>
        <td>{{ stu.gender }}</td>
        <td>{{ stu.race }}</td>
        <td>{{ stu.ethnicity }}</td>
        <td>{{ stu.occupants }}</td>
    </tr>
    {% endfor %}
    </tr>
</table>
{% endblock %}

CodePudding user response:

Django have build in method for that:

get_FIELD_display()

so in your template:

{% for stu in studata_list %}
    <tr>
    <tr>
        <td>{{ stu.id_num }}</td>
        <td>{{ stu.entry_date }}</td>
        <td>{{ stu.get_gender_display }}</td>
        <td>{{ stu.get_race_dispaly }}</td>
        <td>{{ stu.get_ethnicity_display }}</td>
        <td>{{ stu.occupants }}</td>
    </tr>
    {% endfor %}
  • Related