1

我正在尝试通过我的安装获取 Django autocomplete_light(版本:2.0.0a18),以及自动完成字段旁边的“添加新项目”按钮。我能够对现有项目使用自动完成功能,并且能够使用添加新按钮创建新项目。但是,在添加新项目后,我不断收到错误消息,说它的主键不是可用的选择之一。

有人可以帮我弄清楚这个设置有什么问题吗?

模型.py:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Skill(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def get_absolute_url(self):
        return urlresolvers.reverse(
            'skill_update', args=(self.pk,))

    def __unicode__(self):
       return self.name

表格.py:

from django import forms
from feedback.models import *
from django.forms.extras.widgets import SelectDateWidget
from django.contrib.admin import widgets
from functools import partial
from django.template import RequestContext, Context
import autocomplete_light
DateInput = partial(forms.DateInput, {'class': 'datepicker'})
autocomplete_light.autodiscover()


class ReportForm(forms.ModelForm):
    start_date = forms.DateField(help_text="Please enter the Start Date: ", widget=DateInput())
    end_date = forms.DateField(help_text="Please enter the End Date: ",widget=DateInput())
    skills = autocomplete_light.MultipleChoiceField('SkillAutocomplete',help_text="Skills used in this period: ") 

    class Meta:
        # Provide an association between the ModelForm and a model
        model = Report
        exclude = ["report_owner"]


SkillForm = autocomplete_light.modelform_factory(Skill)

autocomplete_light_registry.py:

import autocomplete_light
from django.contrib.auth.models import User
from feedback.models import Skill
autocomplete_light.autodiscover()

autocomplete_light.register(Skill,add_another_url_name='skill_create',attrs = {
        'data-autcomplete-minimum-characters': 1,
        'placeholder': 'Start typing skill name ...',
    })

技能表格.html:

<html>
    <body>
        <form method="post" action="">
            {% csrf_token %}
            <table>
                {{ form.as_table }}
            </table>
            <input type="submit" value="Create New Skill" class = "btn btn-primary btn-lg"/>
        </form>

        <script src="{{ STATIC_URL }}jquery.js" type="text/javascript"></script>
        {% include 'autocomplete_light/static.html' %}
    </body>
</html>
4

1 回答 1

0

我已将“ModelChoiceField”更改为“ModelMultipleChoiceField”并消除了错误。我还不了解解决方案的完整机制,但我将其发布在这里,以防有人遇到此问题。

于 2014-10-06T06:13:43.577 回答