3

What should I put in rel and admin site? This is the field in my form:

tutor_temporal = forms.ModelChoiceField(
    queryset=Tutor_temporal.objects.all(),
    label='Tutor No Registrado', 
    required=False,
    widget=RelatedFieldWidgetWrapper(
        widget=forms.Select(attrs={'class': 'input is-small is-rounded'}),
        rel=Tutor_temporal._meta.get_field('id').rel,
        admin_site= admin_site
    )
)

The problem is that when I try that, throws this AttributeError: 'AutoField' object has no attribute 'rel', because apparently is deprecated.

nik_m
  • 11,825
  • 4
  • 43
  • 57

2 Answers2

6
from django.contrib.admin import site as admin_site

def __init__(self, *args, **kwargs):
        super(TesisForm, self).__init__(*args, **kwargs)
      
        self.fields['tutor_temporal'].widget = (
           RelatedFieldWidgetWrapper( 
               self.fields['tutor_temporal'].widget,
               self.instance._meta.get_field('tutor_temporal').remote_field,            
               admin_site,
           )
       )

  • 1
    Although though this is giving me the + button, it is opening in the same window and then not closing the 'popup' which isn't really a popup because it opened in the same window. How did you overcome this? – HenryM Dec 14 '20 at 14:56
  • @HenryM i faced the same problem , did you fix it please ? – artiest Sep 23 '21 at 07:04
  • 1
    That's weird. Can you post a paste bien of your code ? Which version of Django are you using? Can you post a screenshot of how it iss on the same window? – Joaquín De Tone Sep 23 '21 at 13:31
  • @JoaquínDeTone https://stackoverflow.com/questions/69289560/how-to-add-button-for-foreign-key-similar-to-djangos-admin – artiest Sep 23 '21 at 13:46
  • @JoaquínDeTone its the code – artiest Sep 25 '21 at 05:18
  • @HunarMohammed It was solved? If that is the case please mark as answer useful – Joaquín De Tone Sep 28 '21 at 14:46
1

I was stuck in a scenario which I overrode some fields in a ModelForm :

class VariationsForm(forms.ModelForm):
    """ Variations admin form """

    color = forms.ModelChoiceField(
        required=True,
        queryset=mdl.Color.objects.filter(
            is_active=True,),
        label='Color',
        empty_label="-----",
    )
    size = forms.ModelChoiceField(
        required=True,
        queryset=mdl.Size.objects.filter(
            is_active=True,),
        label='Talla / Medida',
        empty_label="-----"
    )

And the "add action button" never was displayed, so I started to search on the net about how to enable this feature and I found out that usually all generated fields are wrapped by the class: RelatedFieldWidgetWrapper https://github.com/django/django/blob/master/django/contrib/admin/widgets.py#L224,

As a part of the solution as previously shared above, I applied the wrapper to the fields that I early overrode :

    from django.contrib.admin import (
      widgets,
      site as admin_site
    )
    .....
    .....
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # personalize choices
        for field in ["color", "size"]:
            self.fields[field].widget = widgets.RelatedFieldWidgetWrapper(
                self.fields[field].widget,
                self.instance._meta.get_field(field).remote_field,
                admin_site
            )

finally :

enter image description here

Best regards,

Manuel Lazo
  • 745
  • 7
  • 7
  • I've been looking for this for a year. Thanks so much! – Anthony Petrillo Mar 02 '21 at 13:24
  • I've been looking at the code you pointed to hoping I could find a way to change the URLused when clicking on the + sign. I want to go to one of my own pages rather than admin. Would you know how to do this? – Anthony Petrillo Mar 02 '21 at 17:23
  • Hello, sorry the for delay, You can take a look in `django admin tabularinline form methods`, there you will find a method that will allows you change the action of the button. – Manuel Lazo Mar 09 '21 at 18:35
  • @ManuelLazo it is opening in the same window and then not closing the 'popup' which isn't really a popup because it opened in the same window. How did you overcome this – artiest Sep 23 '21 at 12:08
  • @HunarMohammed, when the new window opens you have the buttons to "add" and "cancel" if I am not wrong, right ?, hitting the cancel button closes the window? – Manuel Lazo Sep 24 '21 at 13:06
  • @ManuelLazo https://stackoverflow.com/questions/69313268/how-to-implement-add-another-to-django-model-form here is my question – artiest Sep 24 '21 at 13:07
  • @ManuelLazo no , i dont have none of them – artiest Sep 24 '21 at 13:19
  • its not pop up , it just replace the current url to the add new object – artiest Sep 24 '21 at 13:19