I'm doing my best to convert over to the newforms-admin model. There
are three things that used to be model kwargs that I'm having
difficulty converting over: default values and radio_admin.. Any tips?
Do I
have to tie these things into formfield_for_dbfield() in the Options
class?
Cheers,
Allen
I'm no Django expert (in fact, I just started learning a month ago),
but I'm happy to share what I have discovered so far.
>From what I can tell, the only way to override the default widget or
field for a model's database field is to subclass the Options class
and override the kwargs['widget'] for the field. For more info, check
out my earlier post:
But there's a complication in the case of the radio widget. I could be
mistaken, but I was unable to get it to work with newforms-admin. I
had to create my own subclassed radio widget and use that instead.
(The problem was that the radio widget was returning a "radio field
renderer" object, not the actual code as a unicode string.)
If you're interested in the details, I can post my solution tomorrow
when I'm at the office. But if anyone with more intimate knowledge of
newforms-admin has a better solution, I do hope he'll chime in.
Cheers,
Leif
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'date':
kwargs['initial'] = date.today
return
super(ModelOptions,self).formfield_for_dbfield(db_field, **kwargs)
On Jul 9, 1:18 am, leifbyron <leifstrickl...@gmail.com> wrote:
> Hi Allen,
>
> I'm no Django expert (in fact, I just started learning a month ago),
> but I'm happy to share what I have discovered so far.
>
> >From what I can tell, the only way to override the default widget or
>
> field for a model's database field is to subclass the Options class
> and override the kwargs['widget'] for the field. For more info, check
> out my earlier post:
>
> http://groups.google.com/group/django-users/browse_thread/thread/beba...
>
> But there's a complication in the case of the radio widget. I could be
> mistaken, but I was unable to get it to work withnewforms-admin. I
> had to create my own subclassed radio widget and use that instead.
> (The problem was that the radio widget was returning a "radio field
> renderer" object, not the actual code as a unicode string.)
>
> If you're interested in the details, I can post my solution tomorrow
> when I'm at the office. But if anyone with more intimate knowledge ofnewforms-adminhas a better solution, I do hope he'll chime in.
>
> Cheers,
> Leif
>
> On Jul 8, 7:38 pm, _ <allen.ridd...@gmail.com> wrote:
>
> > Hi folks,
>
> > I'm doing my best to convert over to thenewforms-adminmodel. There
The RadioSelect widget is actually one of the more complicated ones in
newforms. Its render method returns a RadioFieldRenderer object, which
in turn utilizes the RadioInput widget. The problem with using a
RadioSelect widget in newforms-admin is that the template is expecting
each widget to produce a unicode string, not a non-string object. So
when the RadioSelect widget returns a RadioFieldRenderer object to the
template, an error is triggered.
Luckily, this problem is easy to fix by subclassing RadioSelect like
so:
class AdminRadioSelect(forms.widgets.RadioSelect):
def render(self, name, value, attrs=None, choices=()):
renderer =
super(AdminRadioSelect,self).render(name,value,attrs,choices)
return renderer.__unicode__()
This, of course, is pretty hackish. I am not sure if a ticket is open
to solve this problem. But at least this code suffices for now.
As for initial values ... are you referring to the values of a saved
object as rendered on a change form, or the default values (as defined
in the model) on the add form?
Leif
As for initial values, I'm thinking about the add form. I'm sorry, I
should have said.