newforms-admin: defaults and radio select

27 views
Skip to first unread message

_

unread,
Jul 8, 2007, 10:38:19 PM7/8/07
to Django users
Hi folks,

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

leifbyron

unread,
Jul 9, 2007, 1:18:29 AM7/9/07
to Django users
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/bebad28c9701b4d0/180dd29380fbece9?hl=en#180dd29380fbece9

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

_

unread,
Jul 9, 2007, 7:12:28 AM7/9/07
to Django users
Thanks Leif! I'll try to play around with the radio widget today. The
question about defaults was more simple. Formerly I had a model class
with default="0.0" or default=datetime.date.today() How do I reproduce
that with newforms-admin? Is there any easier way to provide initial
values (that's what I meant by defaults... poor choice of words) than
this?:

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

Message has been deleted

leifbyron

unread,
Jul 9, 2007, 1:23:02 PM7/9/07
to Django users
Hi Allen,

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

_

unread,
Jul 9, 2007, 4:34:42 PM7/9/07
to Django users
Thanks for the code, I'll give it a shot.

As for initial values, I'm thinking about the add form. I'm sorry, I
should have said.

leifbyron

unread,
Jul 11, 2007, 2:02:56 PM7/11/07
to Django users
Hey Allen -- Any luck?


Reply all
Reply to author
Forward
0 new messages