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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:28:09+00:00 2026-05-26T00:28:09+00:00

When selecting a foreignkey in the django admin change form I am trying to

  • 0

When selecting a foreignkey in the django admin change form I am trying to add an href that can view the record next to the plus that adds the record.

What I’ve tried just to get the href to render is I’ve copied out the admins def render into my own custom widgets file and added it to and subclassed it:

widgets.py

class RelatedFieldWidgetWrapperLink(RelatedFieldWidgetWrapper):

    def render(self, name, value, *args, **kwargs):
        rel_to = self.rel.to
        info = (rel_to._meta.app_label, rel_to._meta.object_name.lower())
        try:
            related_url = reverse('admin:%s_%s_add' % info, current_app=self.admin_site.name)
        except NoReverseMatch:
            info = (self.admin_site.root_path, rel_to._meta.app_label, rel_to._meta.object_name.lower())
            related_url = '%s%s/%s/add/' % info
        self.widget.choices = self.choices
        output = [self.widget.render(name, value, *args, **kwargs)]
        if self.can_add_related:
            # TODO: "id_" is hard-coded here. This should instead use the correct
            # API to determine the ID dynamically.
            output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
                (related_url, name))
            output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="%s"/></a>' % (settings.ADMIN_MEDIA_PREFIX, _('Add Another')))
            output.append(u'<a href="%s" class="testing" id="add_id_%s" onclick="#"> ' % \        
                        (related_url, name))
        return mark_safe(u''.join(output))

and in admin.py

formfield_overrides = {models.ForeignKey:{'widget':RelatedFieldWidgetWrapperLink}}

however I get thefollowing error:

TypeError
init() takes at least 4 arguments (1 given)

Has anyone run into this problem before?

  • 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-26T00:28:10+00:00Added an answer on May 26, 2026 at 12:28 am

    The RelatedFieldWidgetWrapper widget, and your subclass, are not meant to be used as the widget in formfield_overrides. The __init__ methods have different function signatures, hence the TypeError.

    If you look at the code in django.contrib.admin.options, you can see that the RelatedFieldWidgetWrapper widget is instantiated in the model admin’s formfield_for_dbfield method, so that it can be passed the arguments rel, admin_site and can_add_related.

    I think you may have to override your model admin class’ formfield_for_dbfield method, and use your custom RelatedFieldWidgetWrapperLink widget there.

    class YourModelAdmin(admin.ModelAdmin):
        def formfield_for_dbfield(self, db_field, **kwargs):
            <snip>
            # ForeignKey or ManyToManyFields
            if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
                # Combine the field kwargs with any options for formfield_overrides.
                # Make sure the passed in **kwargs override anything in
                # formfield_overrides because **kwargs is more specific, and should
                # always win.
                if db_field.__class__ in self.formfield_overrides:
                    kwargs = dict(self.formfield_overrides[db_field.__class__], **kwargs)
    
                # Get the correct formfield.
                if isinstance(db_field, models.ForeignKey):
                    formfield = self.formfield_for_foreignkey(db_field, request, **kwargs)
                elif isinstance(db_field, models.ManyToManyField):
                    formfield = self.formfield_for_manytomany(db_field, request, **kwargs)
    
                # For non-raw_id fields, wrap the widget with a wrapper that adds
                # extra HTML -- the "add other" interface -- to the end of the
                # rendered output. formfield can be None if it came from a
                # OneToOneField with parent_link=True or a M2M intermediary.
                if formfield and db_field.name not in self.raw_id_fields:
                    related_modeladmin = self.admin_site._registry.get(
                                                            db_field.rel.to)
                    can_add_related = bool(related_modeladmin and
                                related_modeladmin.has_add_permission(request))
                    # use your custom widget
                    formfield.widget = RelatedFieldWidgetWrapperLink(
                                formfield.widget, db_field.rel, self.admin_site,
                                can_add_related=can_add_related)
    
                return formfield
            <snip>
    

    Other approaches

    You may find it cleaner to override the formfield_for_foreignkey method than formfield_for_dbfield.

    You may be able to subclass the Select widget, and add your link in it’s render method. Your custom select widget would then be wrapped by the RelatedFieldWidgetWrapper. However, I am not sure whether you can produce the view_url inside the scope of the render method.

    from django.contrib.contenttypes.models import ContentType
    from django.core.urlresolvers import reverse
    from django.forms.widgets import Select
    
    def get_admin_change_url(obj):
        ct = ContentType.objects.get_for_model(obj)
        change_url_name = 'admin:%s_%s_change' % (ct.app_label, ct.model)
        return reverse(change_url_name, args=(obj.id,))
    
    class LinkedSelect(Select):
        def render(self, name, value, attrs=None, *args, **kwargs):
            output = super(LinkedSelect, self).render(name, value, attrs=attrs, *args, **kwargs)
            model = self.choices.field.queryset.model
            try:
                id = int(value)
                obj = model.objects.get(id=id)
                view_url = get_admin_change_url(obj)
                output += mark_safe('&nbsp;<a href="%s" target="_blank">view</a>&nbsp;' % (view_url,))
            except model.DoesNotExist:
                pass
            return output
    
    class YourModelAdmin(admin.ModelAdmin):
        formfield_overrides = {models.ForeignKey:{'widget':LinkedSelect}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when selecting a column that can contains a NULL value I get an exception
I'm trying to create a Django app where the user can make a list
Selecting a large amount of text that extends over many screens in an IDE
I am selecting from a table that has an XML column using T-SQL. I
Hi guy i am selecting my active record from my table estates and all
I use a voting app (django-ratings if that makes any difference) which uses django's
When selecting a record for a FCE, CE, plugin or anything else in TYPO3,
I'm trying to make a gridview that shows details of a table based upon
I'm using Django to write a blog app, and I'm trying to implement a
Let's say that I have a model: class Ticket(models.Model): client = models.ForeignKey(Client) color =

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.