Re: Using BaseInlineFormSet with customized form
On 3 mrt, 14:32, koenbwrote: > Do you think this is worth a ticket ? I created ticket #10403 for this. Koen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Using BaseInlineFormSet with customized form
On 3 mrt, 14:09, Alex Gaynorwrote: > > AFAIK the intention was always to eventually have a metaclass so it looked > more like model forms, but the usecase wasn't as strong, and no one who > wanted it ever wrote any code AFAIK > > Alex Thank you Alex, something like this seems to work: class InlineFormSetMetaclass(type): def __new__(cls, name, bases, attrs): # TODO: add some checking try: parents = [b for b in bases if issubclass(b, InlineFormSet)] except NameError: # We are defining InlineFormSet itself. parents = None new_class = super(InlineFormSetMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if 'can_order' not in attrs: new_class.can_order = False if 'can_delete' not in attrs: new_class.can_delete = False if 'max_num' not in attrs: new_class.max_num = 0 if 'extra' not in attrs: new_class.extra = 3 parent = attrs.pop('parent') model = attrs.get('model') new_class.fk = (i for i in model._meta.fields if i.name == parent).next() return new_class class InlineFormSet(forms.models.BaseInlineFormSet): __metaclass__ = InlineFormSetMetaclass Do you think this is worth a ticket ? I guess modelformset needs the same kind of love ? Koen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Using BaseInlineFormSet with customized form
On Tue, Mar 3, 2009 at 8:01 AM, koenbwrote: > > Hi all, > > I want to use a inlineformset with a highly customized form (it is > based on a specific metaclass and has a customized __init__ function). > > Due to the way the inlineformset_factory function works, if I specify > my form in the form argument, the specific form instantiation does not > seem to work (because a new form class is created based on my form). I > acknowledge that this may be because my metaclass may be lacking and > therefore does not allow subclassing correctly. > > In case you are wondering: this is entirely outside of the admin. > > I found a way around this in creating my inlineformset as a class > directly off of BaseInlineFormSet like this: > > class ReportDaysFormset(BaseInlineFormSet): >form = InlineReportDayForm >model = ReportDay >fk = (i for i in model._meta.fields if i.name == 'report').next() >can_order = False >can_delete = True >max_num = 0 >extra = 3 > > This works well, and looks nice (I don't much like factory functions > BTW) but I wonder if this could be done easier: > 1. I have to specify can_order, can_delete etc. that is there are no > default values for these > 2. setting the fk attribute this way seems hackish > > I think it would be nice to have a 'InlineFormSet' based on > BaseInlineFormSet that would allow to use a declarative syntax to > specify formsets and that > a. specifies defaults for can_order etc > b. allows setting the fk using a string that specifies the fieldname > of the foreign key to the parent > > Am I missing the point here completely ? > > Does anybody have similar experiences ? > > Koen > > > > AFAIK the intention was always to eventually have a metaclass so it looked more like model forms, but the usecase wasn't as strong, and no one who wanted it ever wrote any code AFAIK Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Using BaseInlineFormSet with customized form
Hi all, I want to use a inlineformset with a highly customized form (it is based on a specific metaclass and has a customized __init__ function). Due to the way the inlineformset_factory function works, if I specify my form in the form argument, the specific form instantiation does not seem to work (because a new form class is created based on my form). I acknowledge that this may be because my metaclass may be lacking and therefore does not allow subclassing correctly. In case you are wondering: this is entirely outside of the admin. I found a way around this in creating my inlineformset as a class directly off of BaseInlineFormSet like this: class ReportDaysFormset(BaseInlineFormSet): form = InlineReportDayForm model = ReportDay fk = (i for i in model._meta.fields if i.name == 'report').next() can_order = False can_delete = True max_num = 0 extra = 3 This works well, and looks nice (I don't much like factory functions BTW) but I wonder if this could be done easier: 1. I have to specify can_order, can_delete etc. that is there are no default values for these 2. setting the fk attribute this way seems hackish I think it would be nice to have a 'InlineFormSet' based on BaseInlineFormSet that would allow to use a declarative syntax to specify formsets and that a. specifies defaults for can_order etc b. allows setting the fk using a string that specifies the fieldname of the foreign key to the parent Am I missing the point here completely ? Does anybody have similar experiences ? Koen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---