Django-Taggit in Edit Form

后端 未结 2 1583
花落未央
花落未央 2021-01-01 02:21

This is a Model Class

class ModelName(models.Model):
  (...)
  pasta = TaggableManager(verbose_name=u\'Pasta\')

and a form template (normal

相关标签:
2条回答
  • 2021-01-01 02:37

    I would use django-taggit-autosuggest as it offers better UI to the user.

    0 讨论(0)
  • 2021-01-01 02:52

    Give a look at the code in: https://github.com/alex/django-taggit/blob/master/taggit/forms.py. You will find the widget used to render the tags. You can use it to render them correctly.

    Example:

    models.py

    from django.db import models
    from taggit.managers import TaggableManager
    
    
    class Example(models.Model):
        name = models.CharField(max_length=20)    
        tags = TaggableManager()
    

    forms.py

    .models import Example
    from django import forms
    from taggit.forms import TagWidget
    
    
    class ExampleForm(forms.ModelForm):
    
        class Meta:
            model = Example
            fields = ('name', 'tags',)
            widgets = {
                'tags': TagWidget(),
            }
    

    I'd recommend you to check this answer too. django - django-taggit form

    0 讨论(0)
提交回复
热议问题