【问题标题】:django smart-selects ajax configurationdjango smart-selects ajax 配置
【发布时间】:2016-06-30 15:57:40
【问题描述】:

所以我终于在管理员端实现了智能选择 (https://github.com/digi604/django-smart-selects),但现在当我尝试让实际过滤器在用户端工作时,过滤器不起作用。我试图研究一个解决方案,似乎我需要实现 ajax 才能让智能选择过滤器正常工作。我也确定我的 form.py 设置不正确,但我无法找到调用 .objects.all() 的替代方法,因为我认为 smart-selects 会在后台进行适当的过滤。

我将包含我的 models.py 和 forms.py,以防解决方案在其中。我以前从未编写过 ajax 脚本,我的研究也没有为我指明从哪里开始。

模型.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'
    )

【问题讨论】:

    标签: python ajax django django-smart-selects


    【解决方案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 中时会自动呈现。

    【讨论】:

    • 谢谢!我取出了实验室、类别和子类别的代码。但是在用户方面,我仍然没有根据选择的实验室来更新类别/子类别,即使我已经在管理员方面设置了链接。
    • 在页面上打开网页调试器;选择实验室后,您是否看到弹出 Ajax 函数的 404 或 500 错误?确保您已将 `url(r'^chaining/', include('smart_selects.urls')), 添加到您的基本 urls.py 中,并且在呈现任何表单之前,您的模板在脚本标记中包含 JQuery>=1.10。如果您还有其他问题,最好打开一个带有任何错误代码等的新问题
    【解决方案2】:

    Ian Price 的反应很好。我已经尝试过了,而且效果很好。问题在于 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 2014-06-14
      相关资源
      最近更新 更多