django-autocomplete-light默认加载以前保存的值?

时间:2022-06-15 19:37:35

I have a working implementation of autocomplete_light in my django project, pulling values from cities_light in a dropdown, which correctly saves the foreign key to the field in the db on form submit. When I revisit the form, I would like for the autocomplete text field to default to the value that is saved, ideally with the value in plain text and with an "X" button (like that is already built in). Currently, I see placeholder text and a blank text field. Other saved values in the form (omitted here) are being defaulted correctly when I revisit the form. What do I need to add here to trigger the widget to present the saved value? Here is my code:

我在我的django项目中有一个autocomplete_light的工作实现,在一个下拉列表中从cities_light中提取值,这正确地将外键保存到表单提交中的db中的字段。当我重新访问表单时,我希望自动完成文本字段默认为保存的值,理想情况下使用纯文本中的值并使用“X”按钮(就像已经内置的那样)。目前,我看到占位符文本和空白文本字段。当我重新访问表单时,表单中的其他已保存值(此处省略)正确默认。我需要在这里添加什么来触发窗口小部件来显示保存的值?这是我的代码:

forms.py

class UserProfileForm(autocomplete_light.GenericModelForm):
    location = autocomplete_light.GenericModelChoiceField(
        widget=autocomplete_light.ChoiceWidget(
            autocomplete='AutocompleteItems',
            autocomplete_js_attributes={'placeholder':'City, State, Country',
                                        'minimum_characters': 3})
    )
    class Meta:
        model = UserProfile
        fields = ['location']

models.py

class UserProfile(models.Model):
    user = models.ForeignKey(
        User,
        unique=True
    )
    location = models.ForeignKey(
        City,
        blank=True,
        null=True
    )

autocomplete_light_registry.py

class AutocompleteItems(autocomplete_light.AutocompleteGenericBase):
    choices = (
        City.objects.all(),
    )
    search_fields = (
        ('search_names',),
    )
autocomplete_light.register(AutocompleteItems)

1 个解决方案

#1


4  

I know this post is more than 1 year old but the answer might help someone. I managed to populate the autocomplete-light widget by adding the extra_context parameter to ChoiceWidget in the form init.

我知道这篇文章超过1年,但答案可能对某人有所帮助。我设法通过在表单init中将extra_context参数添加到ChoiceWidget来填充autocomplete-light小部件。

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    if self.initial.get('city', None):
        cityPK = self.initial['city']
        city = cities_light.models.City.objects.get(pk=cityPK)
        self.fields['city_name'].widget=autocomplete_light.ChoiceWidget('CityAutocomplete', extra_context={'values':[cityPK], 'choices':[city]})

#1


4  

I know this post is more than 1 year old but the answer might help someone. I managed to populate the autocomplete-light widget by adding the extra_context parameter to ChoiceWidget in the form init.

我知道这篇文章超过1年,但答案可能对某人有所帮助。我设法通过在表单init中将extra_context参数添加到ChoiceWidget来填充autocomplete-light小部件。

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    if self.initial.get('city', None):
        cityPK = self.initial['city']
        city = cities_light.models.City.objects.get(pk=cityPK)
        self.fields['city_name'].widget=autocomplete_light.ChoiceWidget('CityAutocomplete', extra_context={'values':[cityPK], 'choices':[city]})

相关文章