How to implement Django Chained Drop Down using clever select

124 views Asked by At

I'm using doing a small django project and using django-clever-select to created chained drop down select.

I followed the exact instruction given in their installation useage guide at https://pypi.org/project/django-clever-selects/.

But once i run the app in pycharm the dropdown doesn't show any data to be selected.

models.py

<pre>
class Category(models.Model):
    mname = models.CharField(max_length=100, blank=False,unique=True)
    mdescription = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.mname

    class Meta:
        ordering = ('mname',)

class SubCategory(models.Model):

    category = models.ForeignKey(Category, on_delete=models.CASCADE,null=True, blank=True)
    scname = models.CharField (max_length=100, blank=False,unique=True)
    scdecription = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.scname

    class Meta:
        ordering = ('scname',)

class Deals(models.Model):

    vendor_deal = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
    subcategory = models.ForeignKey(SubCategory, on_delete=models.SET_NULL, null= True)
    description = models.CharField(max_length=255, blank=True)
    document = models.FileField(upload_to='documents/')
    uploaded_at = models.DateField(auto_now_add=True)
    expire_date = models.DateField(null=False)
    current_date = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.description

    class Meta:

        ordering = ('description',)
 </pre>    

template file for the form
`
    {% extends 'base.html' %}
    {% load static %}
    {% block content %}
    </br></br></br></br>
    <div class="content">
        <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-6">
             <form id="post_from" method="post">
                {% csrf_token %}
                <div class="form-group">
                 {{ form.as_p }}
                </div>
                <button type="submit" class="btn btn-primary">Upload</button>
             </form>
        </div>
            <div class="col-md-2"></div>
        </div>
        <div class="row"> </div>
        <div class="row"> </div>


    </div>
    {% endblock %} </pre></code>
` form.py file `<pre> from django import forms from clever_selects.forms import ChainedModelChoiceField from django.urls import reverse_lazy from uploads.core.models import Deals, Category, SubCategory class DocumentForm(forms.ModelForm): category = forms.ModelChoiceField(widget=forms.Select(attrs={'class':'form-control'}),queryset=Category.objects.all()) subcategory = ChainedModelChoiceField(parent_field='category', ajax_url=reverse_lazy('ajax_chained_models'), empty_label=(u'Select Sub Category'), model=SubCategory, required=True, widget=forms.Select(attrs={'class':'form-control'})) description = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'})) document = forms.FileField(widget=forms.FileInput(attrs={'class': 'form-control-file'})) # Call attrs with form widget expire_date = forms.DateField(widget=forms.widgets.DateInput(attrs={'class':'datepicker','id':'date','type': 'date'})) class Meta: model = Deals fields = ('category', 'subcategory', 'description', 'document', 'expire_date') </pre> view.py file <pre>class AjaxChainedView(ChainedSelectChoicesView): def get_child_set(self): return SubCategory.object.filter(catego=self.parent_value) </pre>` url.py file ` <pre> from django.conf.urls import url, include from rest_framework import routers from .views import home, model_form_upload, DealViewSet, AjaxChainedView app_name = "core" router = routers.DefaultRouter() router.register(r'deals_list', DealViewSet) urlpatterns = [ url(r'^$',home, name='home'), # url(r'^uploads/simple/$', views.simple_upload, name='simple_upload'), url(r'^form/$', model_form_upload, name='model_form_upload'), url(r'^', include(router.urls)), url(r'^ajax/custom-chained-view-url/$', AjaxChainedView.as_view(), name='ajax_chained_view'), ] </pre> `
0

There are 0 answers