Search code examples
pythondjangodjango-autocomplete-light

How to use the django-autocomplete-light to return the foreign-key with a valid value in the form?


I am newbie to django.I have written a post form for my project and want to use autocomplete to select the foreign key with Node field in the Line post form. I have applyed the django-autocomplete-light successfully to return the foreign-key (node_name),but the form was not valid when I posted it.I guessed that the foreign-key is a number of node_id,but the autocomplete returned text(node_name). How can I fix it with the django-autocomplete-light app ? Thank you.

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',
    },
)

Solution

  • I have solved my problem.There is something wrong in my autocomplete_light_registry.py.Now,I have changed it.

    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',
        },
    )