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 854195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:53:08+00:00 2026-05-15T07:53:08+00:00

I have an app using raw_id on both ForeignKeyField and ManyToManyField. The admin displays

  • 0

I have an app using raw_id on both ForeignKeyField and ManyToManyField. The admin displays the value of the foreign key on the right of the edit box.

Unfortunatey, it doesn’t work with ManyToMany. I’ve checked the code and I think that it is the normal behavior. However I would like to know if someone has an easy tip to change this behavior?

Thanks in advance

Update: I’ve tried to subclass the ManyToManyRawIdWidget but I don’t know how to say that the raw_id_fields should use my custom widget. formfield_overrides doesn’t seem to work with raw_id fields

  • 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-15T07:53:09+00:00Added an answer on May 15, 2026 at 7:53 am

    Finally I succeed to make it working. Here is the updated Django2.0 version

    from django.contrib.admin.widgets import ManyToManyRawIdWidget
    from django.utils.encoding import smart_str
    from django.urls import reverse
    from django.utils.html import escape, mark_safe
    
    
    class VerboseManyToManyRawIdWidget(ManyToManyRawIdWidget):
        """
        A Widget for displaying ManyToMany ids in the "raw_id" interface rather 
        than in a <select multiple> box. Display user-friendly value like the ForeignKeyRawId widget
        """
    
        def __init__(self, remote_field, attrs=None, *args, **kwargs):
            super().__init__(remote_field, attrs, *args, **kwargs)
    
        def label_and_url_for_value(self, value):
            values = value
            str_values = []
            field = self.rel.get_related_field()
            key = field.name
            fk_model = self.rel.model
            app_label = fk_model._meta.app_label
            class_name = fk_model._meta.object_name.lower()
            for the_value in values:
                try:
                    obj = fk_model._default_manager.using(self.db).get(**{key: the_value})
                    url = reverse('admin:{0}_{1}_change'.format(app_label, class_name), args=[obj.id])
                    label = escape(smart_str(obj))
                    elt = '<a href="{0}" {1}>{2}</a>'.format(
                        url,
                        'onclick="return showAddAnotherPopup(this);" target="_blank"',
                        label
                    )
                    str_values += [elt]
                except fk_model.DoesNotExist:
                    str_values += [u'???']
            return mark_safe(', '.join(str_values)), ''
    
    
    class MyAdmin(admin.ModelAdmin):
         ...
         def formfield_for_dbfield(self, db_field, **kwargs):
             if db_field.name in ('groups', ):
                 kwargs['widget'] = VerboseManyToManyRawIdWidget(db_field.remote_field, self.admin_site)
             else:
                 return super().formfield_for_dbfield(db_field, **kwargs)
             kwargs.pop('request')
             return db_field.formfield(**kwargs)
    

    Unfortunately, I’ve spend a bounty for nothing 😉

    UPDATE : this snippet is now compatible with Django 2.0. See also http://djangosnippets.org/snippets/2108/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app using a ListView as a main screen. Each row displays
I have an app using the EWS API. Some computers using Exchange 2003 and
I have an app using PHP and the PayPal API. The basic way it
I have an app using SQLite3. It's running pretty well, but I would like
I have an app using the ADO.NET entity framework (the VS2008 version, not the
I have an App using UITableViews and fetching data from a server. I am
I have an app using a SQLite db, and I need the ability for
I have an App using core data with 3 entities with very similar attributes.
I have an iPhone app using a UINavigationController. On the fourth controller of the
I have a web app using forms authentication and I have restricted a folder

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.