Search code examples
djangodjango-modelsdjango-formsdjango-admindjango-smart-selects

How to apply DEBUG to the django admin forms fields?


I am working with fields in a model in which the value of a field depend of a selected option in a previous field in a form (for the moment via django administrator), by which I am using django-smart-selects

I would share this video for a better understanding about of my question or situation and the reason about of ask the way of apply DEBUG or depuration in relation with the fields which I am working in this scenario.

  • The parent field Segmento afectado in the video let me select multiple choices

  • The child field Movimiento in the video, detail the movements that a affected segment (selected in the parent field) can perform

My situation about it

  1. In the video is detailed of a way clear that when I select only a affected segment in the parent field, the options of movements are deployed in the child field, and my log console of Django Server appear the status code HTTP/1.1 200 with the GET operation, indicating to me that was possible get and deploy successfully the movements that correspond to the affected segment selected in the parent field

  2. But, when I select more than one affected segment in the parent field (If I select two or more segments in forward), inmediately my django server at the console show me the status code HTTP/1.1 404 (Not Found in yellow color), because not perform the second selection or the second affected segment that the user select, and due to this reason also don't deploy or appear in the child field the movements associated that these second affected segment can perform

I don't know how to address this situation, due to this form in which are the parent (Segmento Afectado) and child (Movimiento) fields are represented inside the django administrator.

I have a model named AffectedSegment and another model named Movement and through of functionality of ChainedManyToManyField of django-smart-selects is the way in how to I get the values deployed in the child field accord to the selection in the parent field.

My models and the chaining of values for this behavior are:

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 or I get that appear the movements in the child field accord to the selection on the parent field is perform of 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.affected_segment

I don't know how to can I address this situation. What another alternatives can I have? JS, some framework forntend? Another django app?

Any support, orientation about it, will be highly appreciated

:D


Solution

  • Just by briefly looking to your video and the code of "django-smart-select" application, I can see that this application was not meant to do what you are trying.

    When you select only one option, the ending of your url looks like this:

    "../RehabilitationSession/movement/4/"
    

    But when you select two options, your url looks like this:

    "../RehabilitationSession/movement/4,5/" 
    

    and then you get the 404 error.

    Reason for you error lies in the fact that the application "django-smart-select" don't have an appropriate url pattern to deal with "4,5" block. For this to work, it would need to accept comma-separated integers and then have some kind of parser implemented in the app to filter your results by multiple conditions.

    I cannot give you the complete answer, but I can tell you that you have few options:

    1. If you have the skill, you can try and extend the mentioned app and implement the parser and url patterns yourself.

    2. You can do the same thing in your admin class by overriding the query of your field based on the selection of the above field, but without jQuery tinkering, it wont have real-time changes.

    3. You can try some other application, but I cannot guarantee that you will find a suitable solution for your problem.