0

I get this error when trying to create a custom Form on the Admin view, I don't see any solution on stackoverflow (yes similar problems) so I think it's a good idea to post it:

My Models.py:

class Webhook(models.Model):

    url = models.CharField('URL', max_length = 60, unique = True)
    description = models.CharField('Descripcion', max_length = 255)
    enabled = models.BooleanField('Enabled', default=True)
    event = models.CharField('Evento', max_length=1, choices=Events.EVENTS_CHOICES)

My Forms:

class WebhookForm(forms.ModelForm):

    class Meta:
        model = Webhook
        fields = '__all__'

    def save(self, commit=True):
        print('saveeeeeee')
        webhook = super().save(commit=False)
        webhook.code = webhook.id

        # Get token
        response_token = TOKEN.get()
        if response_token['success']:

            # Create Webhook
            url = 'https://sandbox.bind.com.ar/v1/webhooks'
            headers = {
                    'Content-type': 'application/json',
                    'Authorization': 'JWT ' + response_token['token']
                    }
            data = {
                    'url': webhook.url, # Debera responder status_code == 200
                    'description': webhook.description,
                    'code': webhook.id,
                    'enabled': webhook.enabled,
                    'events': webhook.event
                    }
            data_json = json.dumps(data)
            response = requests.put(url, data= data_json, headers = headers)
            response_json = response.json()

            # Result
            result = {}
            if response.status_code == 409:
                result['success'] = False
                result['details'] = response_json
            else:
                result['success'] = True
                result['data'] = response_json

        # If ok, Save
        if commit & result['success']:
            webhook.save()

        return result['success']

My Admin.py

class WebhookAdmin(forms.ModelForm): # Only to override a form in admin

    class Meta:
        model = WebhookForm
        fields = '__all__'

    # Style to add a new User
    add_form = WebhookForm
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('url', 'description', 'code', 'enabled', 'events',)}
        ),
    )

    # Style to edit a new User
    form = WebhookForm
    fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('url', 'description', 'code', 'enabled', 'events',)}
        ),
    )

admin.site.register(Webhook, WebhookAdmin)

And I get this error when I try to do python manage.py makemigrations:

AttributeError: 'ModelFormOptions' object has no attribute 'private_fields'

2 Answers 2

3

The Attribute error was because I was trying to assign a Form into the class Meta, and it needs a Model:

class Meta:
    model = WebhookForm
    fields = '__all__'
Sign up to request clarification or add additional context in comments.

Comments

1

You have subclassed the wrong thing. The admin class needs to inherit from admin.ModelAdmin, not forms.ModelForm.

ModelAdmin classes don't have an inner Meta class. You need to remove that class altogether.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.