如何使用 django-autocomplete-light return 表单中具有有效值的外键?
How to use the django-autocomplete-light to return the foreign-key with a valid value in the form?
我是 django.I 的新手,已经为我的项目编写了一个 post 表单,并希望使用自动完成 select 行 post 中带有节点字段的外键] 形式。我已经成功地将 django-autocomplete-light 应用到 return 外键 (node_name),但是当我 posted it.I 猜到外键是 node_id 的数字,但自动完成 returned 文本(node_name)。如何使用 django-autocomplete-light 应用程序修复它?谢谢。
models.py:
class Node(models.Model):
node_name = models.CharField(max_length=255)
def __unicode__(self):
return self.node_name
class Line(models.Model):
node = models.ForeignKey(Node,on_delete=models.PROTECT)
line_code = models.CharField(max_length=100)
def __unicode__(self):
return self.line_code
forms.py:
from django import forms
import autocomplete_light
from .models import Line,Node
class LineForm(forms.ModelForm):
class Meta:
model = Line
autocomplete_fields = ('node')
widgets = {
'node': autocomplete_light.TextWidget('NodeAutocomplete'),
}
class NodeForm(forms.ModelForm):
class Meta:
model = Node
autocomplete_light_registry.py:
import autocomplete_light.shortcuts as al
from models import Node,Line
al.register(Node,
search_fields=['node_name'],
attrs={
'data-autocomplete-minimum-characters': 1,
},
widget_attrs={
'data-widget-maximum-values': 4,
'class': 'modern-style',
},
)
我已经解决了我的 problem.There 我的 autocomplete_light_registry.py.Now 有问题,我已经改变了它。
import autocomplete_light.shortcuts as al
from models import Node,Line
# This will generate a LineAutocomplete class
al.register(Line,
# Just like in ModelAdmin.search_fields
search_fields=['node'],
attrs={
# This will set the input placeholder attribute:
'placeholder': '',
# This will set the yourlabs.Autocomplete.minimumCharacters
# options, the naming conversion is handled by jQuery
'data-autocomplete-minimum-characters': 1,
},
# This will set the data-widget-maximum-values attribute on the
# widget container element, and will be set to
# yourlabs.Widget.maximumValues (jQuery handles the naming
# conversion).
widget_attrs={
'data-widget-maximum-values': 4,
# Enable modern-style widget !
'class': 'modern-style',
},
)
al.register(Node,
# Just like in ModelAdmin.search_fields
search_fields=['node_name'],
attrs={
# This will set the input placeholder attribute:
'placeholder': '',
# This will set the yourlabs.Autocomplete.minimumCharacters
# options, the naming conversion is handled by jQuery
'data-autocomplete-minimum-characters': 1,
},
# This will set the data-widget-maximum-values attribute on the
# widget container element, and will be set to
# yourlabs.Widget.maximumValues (jQuery handles the naming
# conversion).
widget_attrs={
'data-widget-maximum-values': 4,
# Enable modern-style widget !
'class': 'modern-style',
},
)
我是 django.I 的新手,已经为我的项目编写了一个 post 表单,并希望使用自动完成 select 行 post 中带有节点字段的外键] 形式。我已经成功地将 django-autocomplete-light 应用到 return 外键 (node_name),但是当我 posted it.I 猜到外键是 node_id 的数字,但自动完成 returned 文本(node_name)。如何使用 django-autocomplete-light 应用程序修复它?谢谢。
models.py:
class Node(models.Model):
node_name = models.CharField(max_length=255)
def __unicode__(self):
return self.node_name
class Line(models.Model):
node = models.ForeignKey(Node,on_delete=models.PROTECT)
line_code = models.CharField(max_length=100)
def __unicode__(self):
return self.line_code
forms.py:
from django import forms
import autocomplete_light
from .models import Line,Node
class LineForm(forms.ModelForm):
class Meta:
model = Line
autocomplete_fields = ('node')
widgets = {
'node': autocomplete_light.TextWidget('NodeAutocomplete'),
}
class NodeForm(forms.ModelForm):
class Meta:
model = Node
autocomplete_light_registry.py:
import autocomplete_light.shortcuts as al
from models import Node,Line
al.register(Node,
search_fields=['node_name'],
attrs={
'data-autocomplete-minimum-characters': 1,
},
widget_attrs={
'data-widget-maximum-values': 4,
'class': 'modern-style',
},
)
我已经解决了我的 problem.There 我的 autocomplete_light_registry.py.Now 有问题,我已经改变了它。
import autocomplete_light.shortcuts as al
from models import Node,Line
# This will generate a LineAutocomplete class
al.register(Line,
# Just like in ModelAdmin.search_fields
search_fields=['node'],
attrs={
# This will set the input placeholder attribute:
'placeholder': '',
# This will set the yourlabs.Autocomplete.minimumCharacters
# options, the naming conversion is handled by jQuery
'data-autocomplete-minimum-characters': 1,
},
# This will set the data-widget-maximum-values attribute on the
# widget container element, and will be set to
# yourlabs.Widget.maximumValues (jQuery handles the naming
# conversion).
widget_attrs={
'data-widget-maximum-values': 4,
# Enable modern-style widget !
'class': 'modern-style',
},
)
al.register(Node,
# Just like in ModelAdmin.search_fields
search_fields=['node_name'],
attrs={
# This will set the input placeholder attribute:
'placeholder': '',
# This will set the yourlabs.Autocomplete.minimumCharacters
# options, the naming conversion is handled by jQuery
'data-autocomplete-minimum-characters': 1,
},
# This will set the data-widget-maximum-values attribute on the
# widget container element, and will be set to
# yourlabs.Widget.maximumValues (jQuery handles the naming
# conversion).
widget_attrs={
'data-widget-maximum-values': 4,
# Enable modern-style widget !
'class': 'modern-style',
},
)