Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3938644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:12:29+00:00 2026-05-20T00:12:29+00:00

I have a model containing a ManyToMany field to a table Tags. Since this

  • 0

I have a model containing a ManyToMany field to a table “Tags”. Since this table could be huge, I don’t want to display a select in the form, but a coma separated list of tags, provided by a charfield (I suppose).

On the saving, I would split the list by their comma and then add them one by one (using get_or_create). I already did that.

But when I want to change data, instead of having a list of tags, I have a list of IDs.

How can I display a list of comma separated tags? Do I have to create a new specific Field for that ? or is there already something that do what I’m looking for?

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:12:30+00:00Added an answer on May 20, 2026 at 12:12 am

    You’ll want to create a custom widget (I’m not aware of a built-in widget that will do exactly what you want). The most useful examples would probably be the widgets that come with Django (in forms/widgets.py). You can also see an example of creating a custom widget here.

    I did a little fiddling, and after adapting the built-in Input widget this is what I came up with; I did some testing and it works for me:

    class CommaTags(Widget):
        def render(self, name, value, attrs=None):
            final_attrs = self.build_attrs(attrs, type='text', name=name)
            objects = []
            for each in value:
                try:
                    object = Tag.objects.get(pk=each)
                except:
                    continue
                objects.append(object)
    
            values = []
            for each in objects:
                values.append(str(each))
            value = ', '.join(values)
            if value: # only add 'value' if it's nonempty
                final_attrs['value'] = force_unicode(value)
            return mark_safe(u'<input%s />' % flatatt(final_attrs))
    

    Note that in this example, the widget is hardcoded to use a hypothetical Tag model, and it’s just using the str() method of each object as what will show up in the comma-separated list. You’ll probably want to change these to fit your use. Also, I had this directly in forms/widgets.py, so if you put it somewhere else (like you probably should), you’ll need to import a few of the things I used.

    Once you have that created, you can specify it as the widget for your ModelMultipleChoiceField in your form, like so:

    from django import forms
    
    class TagForm(forms.ModelForm):
        tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),
                                              widget=CommaTags)
        class Meta:
            model = Tag
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I don't unterstand django-haystack properly: I have a data model containing several
In my application I have a model, with a :string field containing a description.
We have a django-model containing a many-to-many field. We use the same form to
I have a Django model containing a text field. In the admin GUI, I'd
I have a model containing ImageField which should be resized after uploading. class SomeModel(models.Model):
I have a simple model containing a FileField among others. When I use a
I have an old Xcode project which contains a CoreData model (containing a version
I have a YML file containing fixtures for a Rails model (Comment) which looks
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.