noticing you have provided also models.py, here is a more complete
answer:
def my_formfield_cb(field):
if isinstance(field, models.FileField) and field.name == 'file':
return fields.FileField(widget = AdminFileWidget(attrs={'url':
"/my/url/"}))
return field.formfield()
FileFormSet = inlineformset_factory(Version, File, extra=1,
formfield_callback = my_formfield_cb)
formSet = FileFormSet()
Marco
On Dec 18, 8:18 pm, Cortland Klein <[email protected]> wrote:
> I'm using inlineformset_factory in one of my views, which is working
> great, except now I'd like to change the widget for one of the fields
> (in case anyone's curious, an AdminFileWidget). For clarification this
> is my own view and has nothing to do with contrib/admin.
>
> I see that inlineformset_factory has something for fields, but I'm not
> sure what to pass to it to override a field's widget:
>
> # django/forms/models.py
>
> > def inlineformset_factory(parent_model, model, form=ModelForm,
> > formset=BaseInlineFormSet, fk_name=None,
> > fields=None, exclude=None,
> > extra=3, can_order=False, can_delete=True,
> > max_num=0,
> > formfield_callback=lambda f: f.formfield()):
>
> From my app:
>
> # models.py
>
> > class Version(models.Model):
> > number = models.FloatField(_("Number"))
>
> > class File(models.Model):
> > file = models.FileField(_("File"), upload_to="materials/file/
> > %Y/%m/%d") # I want to eventually make this an AdminFileWidget
> > version = models.ForeignKey(Version)
>
> # forms.py
>
> > from models import Version, File
> > from django.forms.models import inlineformset_factory
> > from django.contrib.admin.widgets import AdminFileWidget # Not sure
> > how to get this into FileFormSet
>
> > FileFormSet = inlineformset_factory(Version, File, extra=1)
>
> # views.py
>
>
>
> > from forms import FileFormSet
> > from django.shortcuts import render_to_response
> > from django.template import RequestContext
>
> > def version(request, curriculum, version):
> > response = {}
> > if request.method == 'POST':
> > response['fileformset'] = FileFormSet(request.POST,
> > request.FILES, prefix="file", instance=response['version'])
> > if response['fileformset'].is_valid():
> > response['fileformset'].save()
> > else:
> > response['fileformset'] = FileFormSet(prefix="file",
> > instance=response['version'])
> > return render_to_response('redacted/version.html', response,
> > RequestContext(request))
>
> Any ideas?
>
> --
> Cortland Klein <[email protected]> +1 408 506 9791
> Web Developer, Media and Technology
> Apple Retail Training and Development
> 1 Infinite Loop, MS 49-SM
> Cupertino, CA 95014http://apple.com/retail/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---