I am working with fields in a model where the value of a field depends on an option selected in a previous field in the form (which is currently via django administrator
). For this I am using the django-smart-selects application
I would like you to see this video to give me a little understanding of my situation and why I am asking about debugging or debugging in relation to the fields I am working with.
I have put a video, since it is the most illustrative of my situation because they are visual events according to selections. I don't know how to generate gifs to put them as images. Excuse me if it bothers you.
The parent field
Segmento afectado
in the video allows you to select multiple options.The child field,
Movimiento
in the video, shows the movements that the affected segment that is selected in the parent field can perform.
Situation that happens to me
1. The video clearly shows that when I select only one affected segment in the parent field, the movement options of that segment appear in the child field, and in the console of my IDE the code status HTTP/1.1 200
with the GET operation, indicating that the movements that correspond to the selected affected segment could be successfully obtained and deployed.
2. But when I select more than one affected segment in the parent field (if I select from 2 segments onwards) immediately in the console of my IDE in the Django server log the code appears status HTTP/1.1 400
( Not Found
in yellow color in the console of my IDE) because it does not assimilate the second selection or the second affected segment that the user selects, and therefore, the associated movements or that the second affected segment can perform are not displayed in the child field either.
I don't know how to approach or evaluate this situation, since this form where the parent (Segment afectado
) and child ( Movimiento
) fields are, is represented in the Django administrator because I have a model called AffectedSegment and another called Movementy through the functionality ChainedManyToManyField
of django-smart-selects is that I make the values in the child field display according to the selection of the parent field.
My models and value chaining for this behavior are these:
class AffectedSegment(models.Model):
SEGMENTO_ESCAPULA = 'ESCAPULA'
SEGMENTO_HOMBRO = 'HOMBRO'
SEGMENTO_CODO = 'CODO'
SEGMENTO_ANTEBRAZO = 'ANTEBRAZO'
SEGMENTO_CARPO_MUNECA = 'CARPO_MUNECA'
SEGMENTO_MANO = 'MANO'
SEGMENTO_CHOICES = (
(SEGMENTO_ESCAPULA, u'Escápula'),
(SEGMENTO_HOMBRO, u'Hombro'),
(SEGMENTO_CODO, u'Codo'),
(SEGMENTO_ANTEBRAZO, u'Antebrazo'),
(SEGMENTO_CARPO_MUNECA, u'Carpo/Muñeca'),
(SEGMENTO_MANO, u'Mano'),
)
affected_segment = models.CharField(max_length=12, choices=SEGMENTO_CHOICES, blank=False, verbose_name='Segmento afectado')
class Meta:
verbose_name = 'Segmentos corporale'
def __str__(self):
return "%s" % self.affected_segment
class Movement(models.Model):
type = models.CharField(max_length=255,verbose_name='Tipo de movimiento')
corporal_segment_associated = models.ManyToManyField(AffectedSegment, blank=False, verbose_name='Segmento corporal asociado')
class Meta:
verbose_name = 'Movimiento'
def __str__(self):
return "%s" % self.type
And the way in which the behavior is chained or associated so that the movements appear in the child field according to the selection of affected segments in the parent field is done in this way:
class RehabilitationSession(models.Model):
affected_segment = models.ManyToManyField(AffectedSegment,verbose_name='Segmento afectado')
movement = ChainedManyToManyField(
Movement, #Modelo encadenado
chained_field = 'affected_segment',
chained_model_field = 'corporal_segment_associated',
verbose_name='Movimiento'
)
class Meta:
verbose_name = 'Sesiones de Rehabilitación'
def __str__(self):
return "%s" % self.patient
#return "%s" % self.affected_segment
Apparently another person using the same django-smart-selects application has had the same thing happen to him in the sense that unexpected things happen in the child fields, he doesn't save the information he selects in the child field, in fact The same thing happens to me (at the end of the video you can also detail this) only I don't mention it here so as not to make the post longer.
I can't think of how I can approach this challenge (for me it is :P). I assume that when I render this form of these models not in the django admin but in my applications, the same thing will happen since the admin is just another application in Django and the behavior must be cross-cutting to the entire Django framework itself.
What do you think I can start thinking? It is important that both fields (parent and child) are multiple selection.
Suddenly think of another mechanism to display values in fields according to a selection of a previous field? What other way do I have to address this requirement?
Pure JavaScript (ajax, some frontend framework)? Another app that does the same?
Any collaboration in this regard will be highly appreciated. Cheers
You can try using the Pycharm debugger (for example) seeing the content of all the variables and their metadata, it will highlight the ones that have been most recently modified, the behavior will be modified due to the content of some.
Pycharm in Community Edition doesn't have a Django debugger but if you select it as a start script
manage.py
it works fine.Guys, thank you both for taking the time to help me with my concern.
According to the following answer,
django-smart-selects
it does not implement therequests
ones that receive comma-separated values in its regular expressions in the fileurls.py
that processes the onesrequests
that the users make.When I select a single option in the Affected Segments field, (this is a multiple selection)
django-smat-select
it processes the request correctly, obtaining a codeHTTP/1.1" 200
But when I select more than one affected segment I get this message
NotFound
Who guides me in the answer provides me with some alternative solutions such as:
django-smart-selects
so that it works withrequests
integers separated by commas and when it processes this, it returns the desired results according to the options that must be displayed.Regarding taking urls with integer values separated by commas, how can I start to investigate this?
I have in mind some resources such as:
https://docs.djangoproject.com/en/1.9/ref/validators/#validate-comma-separated-integer-list
https://stackoverflow.com/questions/16321007/python-regular-expression-regex-match-comma-separated-number-why-does-this-n
https://stackoverflow.com/questions/2921453/regex-comma-delimited-integers
And in the option to return the desired results according to the options that are selected first, it is not clear to me how I should do to return the fields that must be displayed.
Perhaps by modifying a queryset somewhere, such as when filtering the results of model chaining ?
Another alternative that they offer me is that in my administration class I can overwrite some query of my field based on the selection of the previous field, I am not very clear how this is.
Another alternative is to look for some other application.
I would like to be able to try the first option, for which I must document more about it in terms of how I should proceed.
So, this is the current state of the application
django-smart-selects
in terms of its work with linked models when more than one option is selected in the parent model, currently it does not receive but only one selection in said parent model and therefore it does not display the options chained in the child model when a second value or second selection of a field is selected in the parent model.Any consideration or appreciation is welcome.
I recommend you to use vue js+ajax either with vue resources or axios, at the beginning I was like you looking for alternatives because I had to work with smart selects and I tried django smart selects but it is very cumbersome, I preferred to do things step by step and believe me that helps you to use a neat and native way of working and the code is not so much. you simply make get requests and return a json response from django and that's it, you organize everything with vue js using v-for v-if and v-bind which, believe me, are magical. It looks like a lot of client side code but I assure you it is not.
notice, there is no simpler example than this one that involves 2 selects, the first being the parent which I call with python and its double keys going through it with a backend for, and the second select depends on a query that is made in python as well , is sent in json and traversed with a v-for from Vue js.
now the part of the backend where I shortcode the parameters that are sent by get.
with axes. In addition to the selects, you also have a guide to save data from a form, that works without the need to install django applications, without configuring permissions or anything, only the native python modules to serialize and those that respond to requests. Regarding vue js I recommend the one from rimorsoft https://www.youtube.com/watch?v=WKOZ2xPWURU&t=22s I recommend the complete course so that you become familiar with this extraordinary frontend framework. Starting from this example you will be able to learn how to do your own selects better personalized and with a less cumbersome way of working.