question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Autocomplite for CharField

See original GitHub issue

Hello. i have the same problem in my project as https://github.com/yourlabs/django-autocomplete-light/issues/810

I want to autocomplite CharField of model

# models.py


class Pet(models.Model):
    name = models.CharField(blank=False, null=False, default='Noname', max_length=50)

    def __str__(self):
        return self.name


# forms.py


class PetForm(ModelForm):
    class Meta:
        model = Pet
        fields = [
            'name',
        ]
        widgets = {
            'name': autocomplete.ListSelect2(url='pet_name_lookup'),
        }

# admin.py


class PetAdmin(admin.ModelAdmin):
    form = PetForm

admin.site.register(Pet, PetAdmin)


# view.py


class PetNameLookup(autocomplete.Select2ListView):

    def create(self, text):
        return text

    def get_list(self):
        result_list = []
        if self.q:
            data = Pet.objects.all().filter(name__icontains=self.q)[:10]

            result_list = [x.name for x in data]
        return result_list

On create admin form it works perfect, but when i try change pet name ListSelect2-widget set the field to blank.

Please help me. DAL is perfect!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jpiccommented, Sep 18, 2017

What happens when you set self.fields[‘country’].initial ?

0reactions
drrcodebasecommented, Feb 14, 2020

Excellent BorisPlus

Read more comments on GitHub >

github_iconTop Results From Across the Web

CharField autocompletes — django-autocomplete-light 2.2.9 ...
CharField autocompletes¶. django-tagging and derivates like django-tagging-ng provide a TagField , which is a CharField expecting comma separated tags.
Read more >
How to use django-autocomplete-light on form with multiple ...
However, I am having issues with using it with the form, which has two char fields, both to use the same model called...
Read more >
Using of django-autocomplete-light with CharField (not as tags)
I think, that the best way to resolve this problem is using of autocomplete, when manager is typing name of company, wich he...
Read more >
Implement Search Autocomplete For Input Fields in Django
In this article we will learn how to fetch data from django models and give it feature like autocomplete. We will be using...
Read more >
Auto-Complete - Django Packages
Completion for CharField/TextField widget based on ForeignKey completion list. Tp-Tham, Coming soon.. Show. 10, 25, 50, 100. entries. Search ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found