jazzband / django-smart-selects

chained and grouped selects for django forms
https://django-smart-selects.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.11k stars 348 forks source link

ChainedManyToManyField not deploy select options in relation to parent field. In django admin works. #214

Closed bgarcial closed 3 years ago

bgarcial commented 7 years ago

Checklist

Put an x in the bracket when you have completed each task, like this: [x]

This issue is similar, but in their case, happens the same problem to me but in the admin area. My problem is in my frontend application using forms.ModelForm

Steps to reproduce

1. My models

I have the following models: In StudiesTypeOffered model I have some type of studies, in the field name.

class StudiesTypeOffered(models.Model):

    CONTINUING_EDUCATION_STUDIES = 'Continuing Education studies'
    TECHNIQUE = 'Technique'
    TECHNOLOGY = 'Technology'
    PROFESSIONAL = 'Professional'
    SPECIALIZATION = 'Specialization'
    MASTER = 'Master'
    DOCTORATE = 'Doctorate'

    STUDIES_TYPE_CHOICES = (
        (CONTINUING_EDUCATION_STUDIES, u'Continuing Education studies'),
        (TECHNIQUE, u'Technique'),
        (TECHNOLOGY, u'Technology'),
        (PROFESSIONAL, u'Professional'),
        (SPECIALIZATION, u'Specialization'),
        (MASTER, u'Master'),
        (DOCTORATE, u'Doctorate'),
    )

    name = models.CharField(
        max_length=100,
        choices=STUDIES_TYPE_CHOICES,
        blank=False,
        verbose_name=u'nombre',
    )

    class Meta:
        verbose_name = "Tipo de estudio ofertado"
        verbose_name_plural = "Tipo de estudios ofertados"

    def __str__(self):
        return "%s" % self.name

And I have the StudiesOffertList model, which have an studies offert list asociated to studies type in my parent model above

class StudiesOffertList(models.Model):

    name = models.CharField(
        max_length=100,
        verbose_name = u'nombre'
    )

    studies_type_offered_associated = models.ManyToManyField(
        StudiesTypeOffered,
        blank=True,
        verbose_name='Tipo de oferta asociada'
    )

    class Meta:
        verbose_name = "Oferta de estudios"
        verbose_name_plural = "Oferta de estudios"

    def __str__(self):
        return "%s" % self.name

In my StudyHostProfile model. which is the model that meet to previous models, I have:

studies_type_offered is my parent field studies_offert_list is my child field

class StudyHostProfile(models.Model):

    studies_type_offered = models.ManyToManyField(
        'StudiesTypeOffered',
        verbose_name=u'Studies Type offered'
    )

    studies_offert_list = ChainedManyToManyField(
        'StudiesOffertList', # Chained model
        horizontal=False,
        verbose_name='Studies Offert List',
        chained_field='studies_type_offered',
        chained_model_field='studies_type_offered_associated',
    )

2. My forms.py file

I am rendering the following field in my forms.py

class StudyHostProfileForm(forms.ModelForm):
    title = "Study Host Details"
    widgets = {
            'institute_character':forms.RadioSelect,
    }

    high_quality_accreditations = forms.MultipleChoiceField(
        required=False,
        label='Accreditations of high quality',
        widget=CheckboxSelectMultiple(),
        choices=StudyHostProfile.ACCREDITATIONS_CHOICES,
    )

    rankings_classification = forms.CharField(widget=forms.Textarea)
    knowledge_topics_choice = forms.CharField(widget=forms.Textarea)
    strengths = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = StudyHostProfile
        fields = ('institution_type', 'institute_character',
            'high_quality_accreditations', 'students_number',
            'rankings_classification', 'knowledge_topics_choice',
            'strengths', 'studies_type_offered', 'studies_offert_list',)

3. My views.py

@login_required
def user_profile_update_view(request, slug):
    user = request.user

    # Populate the forms and Instances (if applicable)
    form_profiles = []
    if user.is_study_host:
        profile = user.get_study_host_profile()
        form_profiles.append({'form': StudyHostProfileForm, 'instance': user.studyhostprofile, 'title': "Study Host Details"})

    if request.method == 'POST':
        forms = [x['form'](data=request.POST, instance=x['instance'],) for x in form_profiles]
        if all([form.is_valid() for form in forms]):
            for form in forms:
                form.save()
            return redirect('dashboard')
    else:
        forms = [x['form'](instance=x['instance']) for x in form_profiles]

    return render(request, 'accounts/profile_form.html', {'forms': forms, 'userprofile':profile,})

**4. My profile_form.html template

{% extends 'layout.html' %}
{% load bootstrap3 %}
{% block title_tag %}Accounts | Profile | iHost {{ block.super }}{% endblock %}
{% block body_content %}
<div class="container">  
    <form method="POST">
        {% csrf_token %}
        {% for form in forms %}
            <h2>{{ form.title }}</h2>
                {% bootstrap_form form %}
        {% endfor %}
        <input type="submit" value="Save Changes" class="btn btn-default">
    </form>
</div>
{% endblock %}

In my urls.py I have this

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^chaining/', include('smart_selects.urls')),
]

Actual behavior

In my frontend application the current behavior is:

gifrecord_2017-06-07_111419

In my django-console do not happen any http GET requests

Expected behavior

The expected behavior is when I choose some option in my studies_type_offered parent field, in the studies_offert_list child field appear some options according to the previous selection.

Curiously in my admin section in Django, this expected behavior is accomplished. Worrks Ok

gifrecord_2017-06-07_112432

This is the traceback http requests in my django console

[07/Jun/2017 16:25:07] "GET /chaining/filter/accounts/StudiesOffertList/studies_type_offered_associated/accounts/StudyHostProfile/studies_offert_list/6/ HTTP/1.1" 200 86
[07/Jun/2017 16:25:08] "GET /chaining/filter/accounts/StudiesOffertList/studies_type_offered_associated/accounts/StudyHostProfile/studies_offert_list/6,7/ HTTP/1.1" 200 136
[07/Jun/2017 16:25:09] "GET /chaining/filter/accounts/StudiesOffertList/studies_type_offered_associated/accounts/StudyHostProfile/studies_offert_list/4,6,7/ HTTP/1.1" 200 267
[07/Jun/2017 16:25:09] "GET /chaining/filter/accounts/StudiesOffertList/studies_type_offered_associated/accounts/StudyHostProfile/studies_offert_list/3,4,6,7/ HTTP/1.1" 200 311
[07/Jun/2017 16:25:09] "GET /chaining/filter/accounts/StudiesOffertList/studies_type_offered_associated/accounts/StudyHostProfile/studies_offert_list/2,3,4,6,7/ HTTP/1.1" 200 311
[07/Jun/2017 16:25:10] "GET /chaining/filter/accounts/StudiesOffertList/studies_type_offered_associated/accounts/StudyHostProfile/studies_offert_list/1,2,3,4,6,7/ HTTP/1.1" 200 396
[07/Jun/2017 16:25:12] "GET /chaining/filter/accounts/StudiesOffertList/studies_type_offered_associated/accounts/StudyHostProfile/studies_offert_list/1,2,3,4,5,6,7/ HTTP/1.1" 200 489

Why the chaining works in django admin and in my django frontend application does not works? In a previous opportunity I can made this using django-smart-selects==1.2.2 which currently is unsupported.

My current packages are:

Django==1.11.1
django-smart-selects==1.5.2
blag commented 7 years ago

A few questions:

I suspect that your problematic page is not loading the bindfields.js file (probably due to a bug in django-smart-selects), which is causing the form to not be initialized for smart selecting.

If you manually include these three pieces of JS in your template with <script src=""> tags (in this order):

does your problematic page work properly?

blag commented 6 years ago

Hey @bgarcial, did you ever get this working, or are you still having issues?

bgarcial commented 6 years ago

Hi @blag in this moment I don't test about it. Can you have this issue open determined time more please? What has happened to me is that I don't had that use django-smart-select in that momento and then I have again to test this behaviour from scratch ...

blag commented 6 years ago

@bgarcial Yes, I’ll leave the issue open until somebody can test it. No worries. :smiley:

abedyngash commented 5 years ago

I've tried the above mentioned solution but it ain't working either. I'm using django 2.1.5 and python 3.7 And using the JSLint Branch since the master branch doesn't work either

manelclos commented 4 years ago

@bgarcial can you reproduce with latest master version?

manelclos commented 3 years ago

Closing due to inactivity.