Question

I'm trying to use autocomplete_light and taggit both on an admin form.

I've read the docs on integrating autocomplete light and taggit here, and the docs on integrating autocomplete light in the admin here. But there seems to be little (or no) discussion on doing both at the same time.

what I've got so far.

In models.py:

from django.db import models
from taggit.managers import TaggableManager
from taggit.models import TagBase, GenericTaggedItemBase


class MyTag(TagBase):
    description = models.CharField(max_length = 250, blank = True, null = True)

class MyTagThroughModel(GenericTaggedItemBase):
    tag = models.ForeignKey(MyTag, related_name = "tagged_items")

class MyModel(models.Model):
    Name = models.CharField(max_length = 200)
    ...
    tags = TaggableManager(through = MyTagThroughModel)

In autocomplete_light_registry.py:

import autocomplete_light
from models import MyTag
autocomplete_light.register(MyTag)

How am I meant to structure admin.py?

If this was a non-admin form, the field would be given as:

tags = TagField(widget = TagWidget('MyTagAutocomplete'))

If this was a non-taggit admin form, I would add the following to the admin model class:

form = autocomplete_light.modelform_factory(MyTag)

How can I combine the two?

Was it helpful?

Solution

How am I meant to structure admin.py?

Here's an example to autocomplete Tags. It shows you how autocomplete_light and taggit work on admin and non-admin forms.

models.py

from django.db import models
from taggit.managers import TaggableManager

class MyModel(models.Model):
    name = models.CharField(max_length = 200)
    tags = TaggableManager(blank=True)

autocomplete_light_registry.py

import autocomplete_light
from taggit.models import Tag

autocomplete_light.register(Tag)

forms.py

from django import forms
import autocomplete_light
from autocomplete_light.contrib import taggit_tagfield
from models import MyModel

class MyModelForm(forms.ModelForm):
    tags = taggit_tagfield.TagField(widget=taggit_tagfield.TagWidget('TagAutocomplete'))
    class Meta:
        model = MyModel
        widgets = {
            'tags': autocomplete_light.TextWidget('TagAutocomplete'),
        }

admin.py

from django.contrib import admin
import autocomplete_light
from models import MyModel
from forms import MyModelForm

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm
    model = MyModel

admin.site.register(MyModel, MyModelAdmin)

views.py

from django.views.generic.edit import CreateView
from models import MyModel
from forms import  MyModelForm

class CreateMyModel(CreateView):
    model = MyModel
    form_class = MyModelForm

urls.py

from django.conf.urls import patterns, url
from views import CreateMyModel

urlpatterns = patterns('',
     url(r'^create/$', CreateMyModel.as_view()),
)

The quick docs seem to be more straightforward to understand than the docs you were looking at.

OTHER TIPS

Consider using django-taggit-autosuggest instead.

Tag Autocomplete Autosuggest on Django

It works best with the django-grapelli admin skin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top