How to declare a ChoiceField on models.py

1

Good I am modifying my project and decided to place a ChoiceField but when I want to make the makemigrate I get the following error:

  

File "/home/jbarreto/Documentos/Misproyectos/Personal/personal/datos/models.py", line 8, in Employees       genre = models.ChoiceField ()   AttributeError: module 'django.db.models' has no attribute 'ChoiceField'

This is my models.py :

from django.db import models


class Empleados(models.Model):
    nombre = models.CharField(max_length=15)
    apellidos = models.CharField(max_length=15)
    ci = models.IntegerField()
    genero = models.ChoiceField()
    cargo = models.CharField(max_length=15)
    creado = models.DateTimeField(auto_now_add=True)
    email = models.EmailField()
    telefono = models.CharField(max_length=12)
    documento = models.FileField(
        upload_to="archivo/",
        null=True,
        blank=True
    )

    def __str__(self):
        return '%s'% (self.nombre)

I'm new in this I need help, thanks

    
asked by Jhonny Barreto 16.08.2018 в 15:19
source

3 answers

0

There are two ways to save possible options that a field and a model can have.

The first one is with CharField , and you help with the attribute choices , in the following way:

OPCIONES_GENERO = (
    ('M', 'MASCULINO'),
    ('F', 'FEMENINO'),
    ('O', 'OTRO')  # hay que ser inclusivos
)

genero = models.CharField(max_length=1, choices=OPCIONES_GENERO)

In this way, in your forms, if you leave the default configuration, for this model the gender field will be rendered with a select and the options will be the same as you have in the variable OPCIONES_GENERO .

The other way is through the relationships either ForeignKey , OneToOneField or ManyToManyField , which also leaving the default configuration of the forms end up rendering a select, but with the options you have in your database, according to the relationship

    
answered by 16.08.2018 в 15:52
0

You must add the STATUS fields, an example would be to first define them:

 STATUS_CHOICES = (
        (1, "PENDIENTE"),
        (2, "En REVISION"),
        (3, "APROBADO"),
        (4, "RECHAZADO")
    )  

After defining it you must add it to your integer or char field, in the example use an integer

genero = models.IntegerField(choices=STATUS_CHOICES ,
            default=STATUS_CHOICES.PENDIENTE)

the model would look like this:

 from django.db import models
 STATUS_CHOICES = (
            (1, "PENDIENTE"),
            (2, "En REVISION"),
            (3, "APROBADO"),
            (4, "RECHAZADO")
   )
        class Empleados(models.Model):

            nombre = models.CharField(max_length=15)
            apellidos = models.CharField(max_length=15)
            ci = models.IntegerField()
            genero = models.IntegerField(choices=STATUS_CHOICES , default=STATUS_CHOICES.PENDIENTE)
            cargo = models.CharField(max_length=15)
            creado = models.DateTimeField(auto_now_add=True)
            email = models.EmailField()
            telefono = models.CharField(max_length=12)
            documento = models.FileField(
            upload_to="archivo/",
            null=True,
            blank=True
            )

            def __str__(self):
                return '%s'% (self.nombre)

EDITION: To use it in the view you must do the following:

empleado = Empleados(name="Fred Flintstone", genero=1)
empleado.save()
print (empleado.genero) # "sera 1"
empleado.get_genero_display() # "sera PENDIENTE

in your view

 {{ empleado.get_genero_display }}
    
answered by 16.08.2018 в 15:47
0

In my Models modify:     def str (self):         return '% s'% (self.employee)

In my Forms: from django import forms from django.contrib.admin import widgets from datos.models import Employees, ActualizacionEmpleado

from django.forms import ModelForm, TextInput, Select, EmailInput

widgets = {             'name': TextInput (attrs = {'class': 'form-control', 'placeholder': 'Enter name'}),             'last names': TextInput (attrs = {'class': 'form-control', 'placeholder': 'Enter surnames'}),             'genre': Select (attrs = {'class': 'form-control', 'placeholder': 'Select your gender'}),             'ci': TextInput (attrs = {'class': 'form-control', 'type': 'number', 'placeholder': 'Enter ci'}),             'charge': TextInput (attrs = {'class': 'form-control', 'placeholder': 'Enter charge'}),             'email': EmailInput (attrs = {'class': 'form-control', 'placeholder': 'Enter email'}),             'phone': TextInput (attrs = {'class': 'form-control', 'placeholder': 'Enter only phone number'}),

On my Views: from django.views.generic import CreateView, UpdateView, DeleteView, DetailView from .models import Employees, UpdateEmployee from datos.forms import EmpleadoForm

In my Urls: from django.views.generic import ListView from datos.views import EmpleadosView, EmpleadosUpdate, EmpleadosDelete, DetalleFormulario

    
answered by 16.08.2018 в 21:24