Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created September 20, 2011 17:18
Show Gist options
  • Save mhulse/1229708 to your computer and use it in GitHub Desktop.
Save mhulse/1229708 to your computer and use it in GitHub Desktop.
Django (1, 4, 0, 'alpha', 0): Trying to get custom model field to pass custom attribute to form (and eventually a widget)... What am I doing wrong here?
from django.contrib import admin
from test.models import Test
admin.site.register(Test)
from django.db import models
from test import forms
class TestField(models.CharField):
"""
Docstring...
"""
description = 'Description...'
def __init__(self, baz=None, *args, **kwargs):
# def __init__(self, *args, **kwargs):
# Get model kwarg (is this the best place to set default values?):
# self._baz = kwargs.get('baz', None) # TypeError: __init__() got an unexpected keyword argument 'baz'
self._baz = baz
super(TestField, self).__init__(*args, **kwargs)
def formfield(self, **kwargs):
# Set kwarg defaults:
defaults = {
'baz': _baz,
'form_class': forms.TestField,
}
# Update kwargs with defaults:
defaults.update(kwargs)
return super(TestField, self).formfield(**defaults)
from django.forms import fields
# from test import widgets
class TestField(fields.CharField):
"""
Docstring...
"""
description = 'Description...'
def __init__(self, baz=None, *args, **kwargs):
# Or this?
# self.baz = kwargs.get('baz', None)
# Call the widget:
# self.widget = widgets.TestWidget(baz=baz, **kwargs)
# Default kwargs:
dwargs = {
'required': True,
'label': None,
'initial': None,
'help_text': None,
'error_messages': None,
'show_hidden_initial': False,
}
# Assign attribute values from kwargs to dwargs:
for attr in dwargs:
if attr in kwargs:
dwargs[attr] = kwargs[attr]
super(TestField, self).__init__(*args, **dwargs)
def clean(self, value):
# Do stuff with value here.
pass
from django.db import models
from test.fields import TestField
class Test(models.Model):
"""
Docstring...
"""
description = 'Description...'
foo = TestField(u'Foo field', baz='bar', max_length=100, help_text='Foo baz bar?')
# I've hit a brick wall here:
# __init__() got multiple values for keyword argument 'baz'
# In line #12 above. :(
@mhulse
Copy link
Author

mhulse commented Sep 20, 2011

I have spent the last few days trying to get this to work (reading numerous sources, including docs) and nothing seems to be working.

Goal

Pass custom attribute to custom model field and eventually pass that off to a custom widget. :)

Problem

My model is complaining about baz attribute:

File "/.../test/models.py", line 12, in Test
foo = TestField(u'Foo field', baz='bar', max_length=100, help_text='Foo baz bar?')
TypeError: __init__() got multiple values for keyword argument 'baz'

Where have I gone wrong?

Notes

Original code found here.

I have used other code examples for inspiration and learning purposes, but the above code is closely related a project I am working on.

@mhulse
Copy link
Author

mhulse commented Sep 20, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment