I am wanting to work some attributes in some models under the field type Field.choice
The context of your data model is as follows:
I have the model/table Patient
and the model/table CorporalStructure
where the latter refers to the different parts of the body, such as the shoulder, elbow, forearm, etc.
In this way we have the following relationship where 1 patient has many body structures. ForeignKey
By having this type of relationship, a call is generated Patient_id
on the tableCorporalStructures
This means that every time I want to create a body structure, I must say to which patient it belongs, which I do not want according to how I have conceived my system.
My body_structure/models.py file is as follows:
from django.db import models
from metrics.models import Metric
# Create your models here.
class CorporalStructure(models.Model):
CORPORAL_STRUCTURE_CHOICES = (
('Escápula',(
('elevación:40°','Elevación:40°'),
('Descenso:10°','Descenso:10°'),
('retracción:25°','Retracción:25°'),
('protracción:30°','Protracción:30°'),
)
),
('Hombro',(
('flexión:150°-170°','Flexión:150°-170°'),
('extensión:40°','Extensión:40°'),
('abducción (separación):160° a 180°','Abducción (separación):160° a 180°'),
('adducción (acercamiento):20° a 40°','Aducción (acercamiento):20° a 40°'),
('rotación interna con hombro aducido (acercado a tórax):70°','Rotación interna con hombro aducido (acercado a tórax):70°'),
('rotación externa con hombro abducido a 90°:60°','Rotación externa con hombro abducido a 90°:60°'),
('rotación interna con hombro a 90° de abducción:70°','Rotación interna con hombro a 90° de abducción:70°'),
('rotación externa con hombro a 90° de abducción:90°','Rotación externa con hombro a 90° de abducción:90°'),
('movimiento de arco horizontal del hombro de afuera hacia adentro: de 40-50° posterior a 130-160° anterior','Movimiento de arco horizontal del hombro de afuera hacia adentro: de 40-50° posterior a 130-160° anterior'),
('*circunducción: sumatoria de los movimientos anteriores','Circunducción: sumatoria de los movimientos anteriores'),
)
),
('Codo', (
('extensión','Extensión'),
('flexión', 'Flexión'),
)
),
('Antebrazo', (
('supinación','Supinación'),
('Pronación', 'Pronación'),
)
),
('Carpo', (
('flexión palmar','Flexión palmar'),
('flexión dorsal (o extensión)', 'Flexión dorsal (o extensión)'),
('desviación ulnar', 'Desviación ulnar'),
('desviación radial', 'Desviación radial'),
('*circunducción: sumatoria de los movimientos anteriores','Circunducción: sumatoria de los movimientos anteriores'),
),
),
('Mano', (
('aproximación interfalángica','Aproximación interfalángica'),
('separación interfalángica', 'Separación interfalángica'),
('flexión superficial de falanges (se puede hacer en masa, es decir las 5 falanges al tiempo, o de manera individual)', 'Flexión superficial de falanges (se puede hacer en masa, es decir las 5 falanges al tiempo, o de manera individual)'),
('flexión profunda de falanges (se puede hacer en masa, o de manera individual)', 'Flexión profunda de falanges (se puede hacer en masa, o de manera individual)'),
('flexión metacarpofalangica (se puede hacer en masa, o de manera individual)','Flexión metacarpofalangica (se puede hacer en masa, o de manera individual)'),
('extensión de las falanges (se puede hacer en masa o de manera individual)','Extensión de las falanges (se puede hacer en masa o de manera individual)'),
('separación del pulgar','Separación del pulgar'),
('aproximación del pulgar','Aproximación del pulgar'),
('oposición del pulgar','Oposición del pulgar'),
('flexión del pulgar','Flexión del pulgar'),
),
),
)
name = models.CharField(max_length=150, choices=CORPORAL_STRUCTURE_CHOICES, blank=False)
type = models.CharField(max_length=150, blank=False)
freedom_degrees = models.CharField(max_length=150, blank=False)
metrics = models.ManyToManyField(Metric)
patient = models.ForeignKey('userprofile.PatientProfile', null=True, blank=True)
def __str__(self):
return '%s' % (self.name)
What I want to do is that when a patient is going to be created in the model, Patient
in its form, it is specified which body structures are going to be examined of that patient, so in the Patient model, I would also need a field of type Field.choice for this. , only that it would already be calling the objects or instances that have been created in the model CorporalStructures
in terms of their name
previously specified attribute.
It would be something like making a query to the CorporalStructure model from the Patient model and rendering that data CorporalStructure.name
(to say anything) in an attribute of type Field.choice
Django in the documentation raises it
Note that choices can be any iterable object – not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you're probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn't change much, if ever.
My Patient model is this PatientProfile/models.py
class PatientProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#active = models.BooleanField(default=True)
full_name = models.CharField(max_length=64)
CORPORAL_STRUCTURE_CHOICE =CorporalStructure.objects.all()
corporal_structures = models.CharField(max_length=150, choices=CORPORAL_STRUCTURE_CHOICE, blank=False)
partner_full_name = models.CharField(max_length=64)
partner_phone = models.CharField(verbose_name=u'phone', max_length=25, blank=True)
care_provider = models.CharField(max_length=64, blank=False)
When I added this field to the PatientProfile model
CORPORAL_STRUCTURE_CHOICE =CorporalStructure.objects.all()
corporal_structures = models.CharField(max_length=150, choices=CORPORAL_STRUCTURE_CHOICE, blank=False)
I get this error
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
userprofile.PatientProfile.corporal_structures: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
And I understand a little what happens, since the attribute, corporal_structures
when putting its property choices
, expects it to be a list or tuple or any element on which it can be iterated and what I am passing to it is the result of an ORM-type query, which is the data I saved in my model CorporalStructures
earlier.
CORPORAL_STRUCTURE_CHOICE =CorporalStructure.objects.all()
corporal_structures = models.CharField(max_length=150, choices=CORPORAL_STRUCTURE_CHOICE, blank=False)
I also have some doubts in relation to the conception of how I have thought the relationships between the models Patient
and CorporalStructure
and are:
I want that when I save a record of a patient, I can select the body structures that will treat that patient. The basic logic tells me that 1 patient has many body structures, but when I enter the Patient_id
as a foreign key in the CorporalStructures table, what I get is that every time I create a body structure, I must choose the patient to which it belongs, in this way :
When what I want is to be able to detail the Field.choice field (its multiple options) that is to say in this way
But in the model form of PatientProfile.
How can I achieve what I set out to do?
I know this is more of a design concern... Do I need a foreign CorporalStructure
en key PatientProfile
or can the multiple Field.choice information be simply called without the need for an FK?
It should be noted that the forms on which I verify this are in the django admin.
Any guidance will be appreciated.
I think (I may be wrong) that the problem may originate from the modeling. A patient can have several body structures, but it is also true that a body structure can "belong" or be shared by several patients. That is, you have an NN relationship , not a 1-N .
I would reconvert the "Body Structure" model to "Body Structure Type". The data you have put in the variable
CORPORAL_STRUCTURE_CHOICES
should go as data from the database, not in a variable. This also allows you to add new "types of body structures", if necessary, without having to assign them to any patient.To save the specific data of each patient you will need a third table, an intermediate table that simply stores foreign keys to "patient" and "body structure type", as well as the rest of the data that you need to associate with that patient and that limb
For example, to store that "menganito" (id_patient 21 ) has 40º of the extension of the shoulder (id_extension 23 ), a record must be created in the new table with the data
(paciente_id=21, tipo_extremidad_id=23, valor=40)
.