2016-06-30 2 views
0

最後に管理者側でスマートセレクト(https://github.com/digi604/django-smart-selects)を実装しましたが、実際のフィルタをユーザ側で動作させようとするとフィルタが機能しません。私はこれに対する解決策を研究しようとしました。スマートセレクトフィルタを適切に動作させるためには、ajaxを実装する必要があるようです。私はまた、私のform.pyが間違って設定されていると確信していますが、私はスマートセレクトがバックグラウンドで適切なフィルタリングを行うと思っていたので、.objects.all()を呼び出すための代替手段を理解することはできません。djangoはスマートでAjaxの設定を選択します

解決策がある場合は、models.pyとforms.pyの両方が含まれます。私は前にajaxスクリプトを作ったことがないし、私の研究はどこから始めるべきかを指摘していない。

models.py

from django.db import models 
from django.contrib.auth.models import User 
from smart_selects.db_fields import ChainedForeignKey 

class Status(models.Model): 
    status = models.CharField(primary_key=True, max_length=25) 

    ## For the API 
    def __str__(self): 
     return self.status 

    ## Eliminates title plurality in the admin interface 
    class Meta: 
     verbose_name_plural="Status" 

class Lab(models.Model): 
    name = models.CharField(primary_key=True, max_length=100) 

    ## For the API 
    def __str__(self): 
     return self.name 

    ## Eliminates title plurality in the admin interface 
    class Meta: 
     verbose_name_plural="Lab" 

class Category(models.Model): 
    lab = models.ForeignKey(Lab) 
    name = models.CharField(primary_key=True, max_length=100) 

    ## For the API 
    def __str__(self): 
     return self.name 

    ## Eliminates title plurality in the admin interface 
    class Meta: 
     verbose_name_plural="Category" 


class SubCategory(models.Model): 
    lab = models.ForeignKey(Lab) 
    category = ChainedForeignKey(
      Category, 
      chained_field = 'lab', 
      chained_model_field = 'lab', 
      show_all = False, 
      auto_choose = True 
     ) 
    subcategory = models.CharField(max_length=100) 
    category = models.ForeignKey(Category) 
    ## For the API 
    #def __str__(self): 
    # return self.category 

    ## Eliminates title plurality in the admin interface 
    class Meta: 
     verbose_name_plural="SubCategory" 

forms.py

import datetime 
from django import forms 

## importing models 
from .models import Category, Lab, SubCategory ## need these tables to  populate dropdown menus 'Category' and 'Lab' 
from submit_app.models import Incident 
from smart_selects.db_fields import ChainedForeignKey 

## importing crispy form utilities 
from crispy_forms.helper import FormHelper 
from crispy_forms.layout import Layout, Field, Submit, Div 
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions 

## importing regex validator 
from django.core.validators import RegexValidator 

## Maybe best to try to use ModelForm....it seems to have overwhelming internet support 
from django.forms import ModelForm 


class IncidentForm(forms.ModelForm): 

    class Meta: 
     model = Incident 
     fields = [ 'date_occurred', 
        'number_of_samples_affected', 
        'capa', 
        'title', 
        'description', 
        'category', 
        'lab', 
        'subcategory', 
        'upload1', 
        'upload2', 
        'upload3' 
        ] 

    ## Pre-populated dropdown menu 
    lab = forms.ModelChoiceField(
     queryset=Lab.objects.all(), 
     label ="Lab" 
    ) 

    ## Pre-populated dropdown menu 
    category = forms.ModelChoiceField(
     queryset=Category.objects.all(), 
     label = "Category" 
    ) 

    subcategory = forms.ModelChoiceField(
     queryset=SubCategory.objects.all(), 
     label = "SubCategory" 
    ) 

    date_occurred = forms.DateField(
     label="Date Incident Occurred", 
     initial=datetime.date.today() 
    ) 

    number_of_samples_affected = forms.IntegerField(
     label="Number of Samples Affected", 
     initial='0' 
    ) 

答えて

1

smart selects、それは自分のフォームフィールドの種類のしています。あなたの形で今categoryのためにすることをオーバーライドする:

## Pre-populated dropdown menu 
category = forms.ModelChoiceField(
    queryset=Category.objects.all(), 
    label = "Category" 
) 

は、フォームから上記のすべてのコードを削除します。 ChainedForeignKeyを使用している場合、モデルフィールドのデフォルトフォームフィールドタイプはsmart selects(ソースのChainedModelChoiceFieldを参照)によって提供されます。

AJAX呼び出しを実行するJavascriptは、smart selectsによって提供されるフォームフィールドの一部としてレンダリングされる小さなインラインスクリプトです。

フィールドはModelFormに含まれていると自動的に表示されるため、コードを何かに置き換える必要はありません。

+0

感謝をロードするための任意のタグの上に最初のいずれかでなければなりません!ラボ、カテゴリ、サブカテゴリのコードを取り出しました。しかし、ユーザー側では、管理者側でリンケージを設定したにもかかわらず、どのラボが選択されているかによってカテゴリ/サブカテゴリが更新されません。 – led4966

+0

ページ上でWebデバッガを開きます。ラボを選択すると、Ajax関数のポップアップで404または500のエラーが表示されますか? 'url(r '^ chaining /'、include( 'smart_selects.urls'))'をベースurls.pyに追加し、テンプレートに[JQuery> = 1.10](http:// code。 jquery.com/)をスクリプトタグで使用して、フォームをレンダリングします。さらに問題がある場合は、おそらくエラーコードなどで新しい質問を開くことをお勧めします –

0

イアンプライスの反応はうまくいきます。私はそれを試して、それはかなり働いています。問題は、jQueryライブラリの読み込みです。

Base.html

<head> 
    <title>testing</title> 
    {% load static %} 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
<script src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script> 
<script src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script> 
</head> 
関連する問題