FormView and django-formset Issue
Hi i used formsets in generic FormView like this: class RequestRecommendationView(FormView): template_name = "account/stepfour.html" form_class = formset_factory(StepFourForm) def form_valid(self,form): print form.is_valid() cleaned_data = form.cleaned_data # return redirect(reverse("some_url_name")) form for formset_factory is like this: class StepFourForm(forms.Form): contact_person = forms.CharField(required=True) email = forms.EmailField(required=True) company = forms.CharField(required=True) request_message = forms.CharField(required=True) my html structure is like this: Request a Recommendation {% csrf_token %} {% for f in form %} {% for field in f %} {{field.label_tag}} {{field}} {% for error in field.errors %} {{ error }} {% endfor %} {% endfor %} {% endfor %} Request Now {{ form.management_form }} Then i used django-dynamic-formset (https://code.google.com/p/django-dynamic-formset/) to add/remove extra forms through these: $(function() { $('#myForm tbody').formset(); }) the problem is: if i leave the form empty,(every field is required), it manages to get to form_valid() of my class-view (though it should not), if i fill one of the fields (leaving others unfilled), it displays the errors associated message successfully. why is this happening ? what should i do to fix it ? is providing form_class a formset is behind all of this ? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.
formset issue
I've send this post this morning: Hi I have a formset and I'm passing initial data: DeletionFormset = formset_factory(deletionForm, extra=0) formset = DeletionFormset(initial=make_initial_data(instance)) The initial data arrives to the constructor fine. When the construct bilds the forms it overrides a ChoiceField with a tuple wich comes on the make_initial_data return def make_initial_data(i): schools = School.objects.exclude(id=i.id) s = [] for school in schools: s.append((school.id, school.name)) n_schools = tuple(s) pupils = Pupil.objects.filter(school=i) ret = [] for pupil in pupils: ret.append({ 'pupil_id': pupil.id, 'pupil': pupil.user.get_full_name(), 'school': n_schools, }) return ret So far all works fine. This is my form used in the formset: class deletionForm(forms.Form): pupil_id = forms.IntegerField(widget=forms.HiddenInput) pupil = forms.CharField() action = forms.ChoiceField(widget=forms.RadioSelect(), choices=(('D',_('Delete')),('M',_('Move' school = forms.ChoiceField() def __init__(self, *args, **kwargs): super(deletionForm, self).__init__(*args, **kwargs) self.base_fields['school'] = forms.ChoiceField(choices=self.initial['school']) print self.base_fields['school'].choices For testing it return a tuple with two indexes. In this print it shows the correct data in both select input, but in the form it only appears in the last select. This the output for the print command: (school's names) [(1, u'ABC'), (3, u'xpto')] [(1, u'ABC'), (3, u'xpto')] This is the select output in the rendered html: ... ... HCT xpto ... What am I missing? Thanks After I sent it, somehow, it starts working by itself. Now it doesn't work again. I've changed the form to only create the school field in the init but still doesn't work My current code is now like this: class deletionForm(forms.Form): pupil_id = forms.IntegerField(widget=forms.HiddenInput) pupil = forms.CharField() action = forms.ChoiceField(widget=forms.RadioSelect(), choices=(('D',_('Delete')),('M',_('Move' #school = forms.ChoiceField() def __init__(self, *args, **kwargs): super(deletionForm, self).__init__(*args, **kwargs) print self.initial if self.initial: m_choices = self.initial['school'] self.base_fields['school'] = forms.ChoiceField(choices = m_choices) If I print in the end of __init__ it's correct. But in the rendered html doesn't appear nothing in the first select. But like this, because I'm only creating the school field in the init, in the first formset doesn't appear the select. Before it was empty, now it doesn't exists. I spent all the afternoon googling for this, somebody know whats happening? By the way, how can I put a selected attribute in one of the choices? In googling it says to put what I want to be default in value but doesn't do anything Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
formset issue
Hi I have a formset and I'm passing initial data: DeletionFormset = formset_factory(deletionForm, extra=0) formset = DeletionFormset(initial=make_initial_data(instance)) The initial data arrives to the constructor fine. When the construct bilds the forms it overrides a ChoiceField with a tuple wich comes on the make_initial_data return def make_initial_data(i): schools = School.objects.exclude(id=i.id) s = [] for school in schools: s.append((school.id, school.name)) n_schools = tuple(s) pupils = Pupil.objects.filter(school=i) ret = [] for pupil in pupils: ret.append({ 'pupil_id': pupil.id, 'pupil': pupil.user.get_full_name(), 'school': n_schools, }) return ret So far all works fine. This is my form used in the formset: class deletionForm(forms.Form): pupil_id = forms.IntegerField(widget=forms.HiddenInput) pupil = forms.CharField() action = forms.ChoiceField(widget=forms.RadioSelect(), choices=(('D',_('Delete')),('M',_('Move' school = forms.ChoiceField() def __init__(self, *args, **kwargs): super(deletionForm, self).__init__(*args, **kwargs) self.base_fields['school'] = forms.ChoiceField(choices=self.initial['school']) print self.base_fields['school'].choices For testing it return a tuple with two indexes. In this print it shows the correct data in both select input, but in the form it only appears in the last select. This the output for the print command: (school's names) [(1, u'ABC'), (3, u'xpto')] [(1, u'ABC'), (3, u'xpto')] This is the select output in the rendered html: ... ... HCT xpto ... What am I missing? Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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: FormSet issue. Pre-populating from queryset
Malcolm, Thanks for the comment. I truly understand where this is coming from. This issue is completely my issue of taking things for granted. I have been coding around in Django for a while now (hooked on in the 0.95 days), and have been coding most of the form stuff (including inlines) all by hand. When I noticed the Formset class, I tried to make it work in the wrong place. Brian pointed me to the inlineformsets documentation and I have not been happier. It really is easy now to build forms with inlines. This stuff has come a long way, and life will be much easier than it was before. Thanks for Django, Maeck --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
On Thu, 2008-12-11 at 20:55 -0800, maeck wrote: [...] > Now I can pass the fieldnames as values('parent') for now, It would be > easier if initial did not care if the _id is provided or not. > Or am I missing something else? What you're missing, or rather, assuming, is that querysets are ideal or intended to be used directly as initial data, and that isn't the case. Forms are separate pieces of code from models and shouldn't have to understand all the foibles of models (i.e. enforce loose coupling). If you want to pass in the results of a queryset directly, it's easy, but you do have to take care and pass in the right values by specifying the names of the fields. You know the models you are using, so writing down their field names isn't particularly onerous. It happens that values(), by default, returns "parent_id" for historical reasons. However, we set things up so that you an also ask it to return "parent" by specifying that in the fields list. So use that functionality. If you still think that's all just nit-picking and we should compromise to avoid the inconvenience, realise how bad it is: first you have to teach Form's initial data that when they expect "foo", it might really be spelt "foo_id". The form doesn't know this is a foreign key, it just knows it's a choice field. But since "_id" isn't the only possible suffix, now the form has to be able to handle any possible suffix -- "foo_blah", for example -- since the database column name can be specified in the model. So we're looking at all sorts of possible alternate names for the key. Thus, the form really needs to have reference to the full model to inspect all that. Now we're passing in models and tying the form to the model and it's starting to look like you really should be using ModelFormSet. Short version: it's just not worth all the complexity. We have ModelFormSets for standard model-> form conversions and an easy way to produce sequences of dictionaries if you have other situations where a FormSet is more useful. These things have a way of being much more complicated than they look on the surface. :-) Regards, Malcolm --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
On Thu, Dec 11, 2008 at 9:55 PM, maeckwrote: > Now I can pass the fieldnames as values('parent') for now, It would be > easier if initial did not care if the _id is provided or not. > Or am I missing something else? You shouldn't be using a regular formset. Django provides model formsets that know how to deal with this internally. Go read up on inline formsets which is a layer on top of model formsets. http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets -- Brian Rosner http://oebfare.com --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
Thanks Malcolm, Just figured out the values transformation on querysets myself. Nevertheless, in my experience there seems to be an issue with foreignkeys when using queryset values in combination with formsets. Values returns keys like 'parent_id', however formsets expect the fieldname as 'parent'. (I am using Django 1.0.2) Now I can pass the fieldnames as values('parent') for now, It would be easier if initial did not care if the _id is provided or not. Or am I missing something else? Maeck --~--~-~--~~~---~--~~ 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: FormSet issue. Pre-populating from queryset
On Thu, 2008-12-11 at 20:19 -0800, maeck wrote: > The example below is a snippet from a view where I use a form to show > 'Parent' and a formset to show its 'Children'. > If I get the children as a queryset and pass it on to the formsets > initial property, it errors out with: 'Parent' object is not iterable > > InlineFormSet = formset_factory(InlineForm) > data = Parent.objects.get(id = data_id) > form = ParentForm(instance=data) > inlinedata= Child.objects.filter(parent_id = data_id) > inlineform= InlineChildFormSet(initial=inlinedata) Initial for data should be a something that is, or at least behaves like, a sequence of dictionaries. A queryset is behaves like a sequence, but the elements of that sequence don't behave like dictionaries (they're objects -- in this case, Parent objects). So whilst, "for key in obj:" works when "obj" is a dictionary, it doesn't work when "obj" is a Django Model subclass (e.g. a Parent). Since the requirement is to pass in a list of dictionaries, using the values() method on your inlinedata call will get you a long way there. Regards, Malcolm --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
FormSet issue. Pre-populating from queryset
The example below is a snippet from a view where I use a form to show 'Parent' and a formset to show its 'Children'. If I get the children as a queryset and pass it on to the formsets initial property, it errors out with: 'Parent' object is not iterable InlineFormSet = formset_factory(InlineForm) data= Parent.objects.get(id = data_id) form= ParentForm(instance=data) inlinedata = Child.objects.filter(parent_id = data_id) inlineform = InlineChildFormSet(initial=inlinedata) However, the following works (same data, just a list of dicts): InlineFormSet = formset_factory(InlineForm) data= Parent.objects.get(id = data_id) form= ParentForm(instance=data) inlinedata = [ {'a':10, 'b':20} {'a':11, 'b':21} ] inlineform = InlineChildFormSet(initial=inlinedata) What am I missing in the first example? --~--~-~--~~~---~--~~ 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: formset issue
Something like this might work I think: attachment_formset = BookingAttachmentFormSet(prefix = "attachments") for form in attachment_formset.forms: form.fields['attachment'].queryset = Attachment.objects.filter(user=request.user) Koen On 2 sep, 13:05, patrickk <[EMAIL PROTECTED]> wrote: > hmm, if the data won´t be received twice this might be a solution. > do you have any idea about the syntax? > > attachment_formset = BookingAttachmentFormSet(prefix="attachments") > for form in attachment_formset: > ??? how do I set initial_data here ??? > > thanks, > patrick > > On Sep 1, 3:39 pm, koenb <[EMAIL PROTECTED]> wrote: > > > Are you sure the data will be retrieved twice ? I thought those > > queryset definitions were lazy, so they are only executed when the > > widgets are rendered. > > Hmm, I'll try that out some time to check. > > > Koen > > > On 1 sep, 14:18, patrickk <[EMAIL PROTECTED]> wrote: > > > > that seems to be possible. on the other hand, it also seems to be a > > > strange way to go: instantiating the formset (retrieving all data/ > > > querysets) and then changing the querysets? performance-wise, this is > > > probably not a good solution. and I doubt that this is a good decision > > > design-wise ... > > > > thanks, > > > patrick > > > > On Sep 1, 1:51 pm, koenb <[EMAIL PROTECTED]> wrote: > > > > > I have not tried this, but can't you just in your view instantiate the > > > > formset and then loop over the forms and set the queryset on your > > > > field ? > > > > > Koen > > > > > On 1 sep, 09:24, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > found a solution: with using threadlocals, it´s possible to define a > > > > > custom manager which only returns the data assigned to the currently > > > > > logged-in user. since threadlocals has been considered a "hack" > > > > > recently by one of the main django-developers (I think it was > > > > > malcolm), this is probably not the best thing to do though. > > > > > > On Aug 31, 4:35 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > > sorry for being so annoying with this issue, but I´m asking this one > > > > > > more time. > > > > > > > - with using inlineformset_factory, my problem is that limiting the > > > > > > choices to the currently logged-in user seems impossible. I´ve just > > > > > > tried to write a custom manger with "use_for_related_fields = True", > > > > > > but it´s also not possible to limit the basic queryset to the > > > > > > current > > > > > > user. > > > > > > > so - before going back to pure forms and re-writing the main part of > > > > > > my application, I´m just asking this question for the last time ... > > > > > > > thanks, > > > > > > patrick > > > > > > > On Aug 27, 9:04 am, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > > > anyone? > > > > > > > > On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > > > > scenario: users are uploading documents (e.g. images, files, > > > > > > > > ...). > > > > > > > > these documents are saved in a model "Attachment" assigned to > > > > > > > > the > > > > > > > > currently logged-in "User". now, every user has the possibility > > > > > > > > to > > > > > > > > attach documents to a blog-entry. these attachments are saved > > > > > > > > in a > > > > > > > > model "BlogEntryAttachment" assigned to a "BlogEntry". > > > > > > > > the best way to achieve this is probably using formsets. when > > > > > > > > writing > > > > > > > > a BlogEntry, the user has the option to attach his/her > > > > > > > > documents to > > > > > > > > that BlogEntry. to achieve this, I have to limit the choices > > > > > > > > (of a > > > > > > > > select-field) to the currenlty logged-in User. > > > > > > > > > questions & notes: > > > > > > > > ### how can I limit the choices of a select-field using > > > > > > > > formsets? > > > > > > > > ### I've tried inlineformset_factory, but there's no argument > > > > > > > > "initial". besides that, I´m not sure how to use "initial" for > > > > > > > > limiting a queryset ... > > > > > > > > ### with using formset_factory on the other hand, I could use a > > > > > > > > ModelChoiceField with > > > > > > > > queryset=Attachment.objects.filter(user=user), > > > > > > > > but how do I get the "user" there? > > > > > > > > ### Overriding __init__ within BlogEntryAttachmentForm and > > > > > > > > passing the > > > > > > > > currently logged-in user seems possible, but then I also have to > > > > > > > > override the BaseFormSets __init__ and _construct_form, which > > > > > > > > just > > > > > > > > seems ugly and far too complicated. > > > > > > > > > MODELS: > > > > > > > > > class Attachment(models.Model): > > > > > > > > > user = models.ForeignKey('auth.User') > > > > > > > > title = models.CharField('Title', max_length=100, > > > > > > > > blank=True, > > > > > > > > null=True) > > > > > > > > filename = models.CharField('Filename', max_length=100, > > > > > > > > blank=True, null=True) > > > > > > > >
Re: formset issue
hmm, if the data won´t be received twice this might be a solution. do you have any idea about the syntax? attachment_formset = BookingAttachmentFormSet(prefix="attachments") for form in attachment_formset: ??? how do I set initial_data here ??? thanks, patrick On Sep 1, 3:39 pm, koenb <[EMAIL PROTECTED]> wrote: > Are you sure the data will be retrieved twice ? I thought those > queryset definitions were lazy, so they are only executed when the > widgets are rendered. > Hmm, I'll try that out some time to check. > > Koen > > On 1 sep, 14:18, patrickk <[EMAIL PROTECTED]> wrote: > > > that seems to be possible. on the other hand, it also seems to be a > > strange way to go: instantiating the formset (retrieving all data/ > > querysets) and then changing the querysets? performance-wise, this is > > probably not a good solution. and I doubt that this is a good decision > > design-wise ... > > > thanks, > > patrick > > > On Sep 1, 1:51 pm, koenb <[EMAIL PROTECTED]> wrote: > > > > I have not tried this, but can't you just in your view instantiate the > > > formset and then loop over the forms and set the queryset on your > > > field ? > > > > Koen > > > > On 1 sep, 09:24, patrickk <[EMAIL PROTECTED]> wrote: > > > > > found a solution: with using threadlocals, it´s possible to define a > > > > custom manager which only returns the data assigned to the currently > > > > logged-in user. since threadlocals has been considered a "hack" > > > > recently by one of the main django-developers (I think it was > > > > malcolm), this is probably not the best thing to do though. > > > > > On Aug 31, 4:35 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > sorry for being so annoying with this issue, but I´m asking this one > > > > > more time. > > > > > > - with using inlineformset_factory, my problem is that limiting the > > > > > choices to the currently logged-in user seems impossible. I´ve just > > > > > tried to write a custom manger with "use_for_related_fields = True", > > > > > but it´s also not possible to limit the basic queryset to the current > > > > > user. > > > > > > so - before going back to pure forms and re-writing the main part of > > > > > my application, I´m just asking this question for the last time ... > > > > > > thanks, > > > > > patrick > > > > > > On Aug 27, 9:04 am, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > > anyone? > > > > > > > On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > > > scenario: users are uploading documents (e.g. images, files, ...). > > > > > > > these documents are saved in a model "Attachment" assigned to the > > > > > > > currently logged-in "User". now, every user has the possibility to > > > > > > > attach documents to a blog-entry. these attachments are saved in a > > > > > > > model "BlogEntryAttachment" assigned to a "BlogEntry". > > > > > > > the best way to achieve this is probably using formsets. when > > > > > > > writing > > > > > > > a BlogEntry, the user has the option to attach his/her documents > > > > > > > to > > > > > > > that BlogEntry. to achieve this, I have to limit the choices (of a > > > > > > > select-field) to the currenlty logged-in User. > > > > > > > > questions & notes: > > > > > > > ### how can I limit the choices of a select-field using formsets? > > > > > > > ### I've tried inlineformset_factory, but there's no argument > > > > > > > "initial". besides that, I´m not sure how to use "initial" for > > > > > > > limiting a queryset ... > > > > > > > ### with using formset_factory on the other hand, I could use a > > > > > > > ModelChoiceField with > > > > > > > queryset=Attachment.objects.filter(user=user), > > > > > > > but how do I get the "user" there? > > > > > > > ### Overriding __init__ within BlogEntryAttachmentForm and > > > > > > > passing the > > > > > > > currently logged-in user seems possible, but then I also have to > > > > > > > override the BaseFormSets __init__ and _construct_form, which just > > > > > > > seems ugly and far too complicated. > > > > > > > > MODELS: > > > > > > > > class Attachment(models.Model): > > > > > > > > user = models.ForeignKey('auth.User') > > > > > > > title = models.CharField('Title', max_length=100, blank=True, > > > > > > > null=True) > > > > > > > filename = models.CharField('Filename', max_length=100, > > > > > > > blank=True, null=True) > > > > > > > path = models.CharField('Path', max_length=200, blank=True, > > > > > > > null=True) > > > > > > > ... > > > > > > > > class BlogEntryAttachment(models.Model): > > > > > > > > blogentry = models.ForeignKey(BlogEntry) > > > > > > > attachment = models.ForeignKey(Attachment) > > > > > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more
Re: formset issue
Are you sure the data will be retrieved twice ? I thought those queryset definitions were lazy, so they are only executed when the widgets are rendered. Hmm, I'll try that out some time to check. Koen On 1 sep, 14:18, patrickk <[EMAIL PROTECTED]> wrote: > that seems to be possible. on the other hand, it also seems to be a > strange way to go: instantiating the formset (retrieving all data/ > querysets) and then changing the querysets? performance-wise, this is > probably not a good solution. and I doubt that this is a good decision > design-wise ... > > thanks, > patrick > > On Sep 1, 1:51 pm, koenb <[EMAIL PROTECTED]> wrote: > > > I have not tried this, but can't you just in your view instantiate the > > formset and then loop over the forms and set the queryset on your > > field ? > > > Koen > > > On 1 sep, 09:24, patrickk <[EMAIL PROTECTED]> wrote: > > > > found a solution: with using threadlocals, it´s possible to define a > > > custom manager which only returns the data assigned to the currently > > > logged-in user. since threadlocals has been considered a "hack" > > > recently by one of the main django-developers (I think it was > > > malcolm), this is probably not the best thing to do though. > > > > On Aug 31, 4:35 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > sorry for being so annoying with this issue, but I´m asking this one > > > > more time. > > > > > - with using inlineformset_factory, my problem is that limiting the > > > > choices to the currently logged-in user seems impossible. I´ve just > > > > tried to write a custom manger with "use_for_related_fields = True", > > > > but it´s also not possible to limit the basic queryset to the current > > > > user. > > > > > so - before going back to pure forms and re-writing the main part of > > > > my application, I´m just asking this question for the last time ... > > > > > thanks, > > > > patrick > > > > > On Aug 27, 9:04 am, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > anyone? > > > > > > On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > > scenario: users are uploading documents (e.g. images, files, ...). > > > > > > these documents are saved in a model "Attachment" assigned to the > > > > > > currently logged-in "User". now, every user has the possibility to > > > > > > attach documents to a blog-entry. these attachments are saved in a > > > > > > model "BlogEntryAttachment" assigned to a "BlogEntry". > > > > > > the best way to achieve this is probably using formsets. when > > > > > > writing > > > > > > a BlogEntry, the user has the option to attach his/her documents to > > > > > > that BlogEntry. to achieve this, I have to limit the choices (of a > > > > > > select-field) to the currenlty logged-in User. > > > > > > > questions & notes: > > > > > > ### how can I limit the choices of a select-field using formsets? > > > > > > ### I've tried inlineformset_factory, but there's no argument > > > > > > "initial". besides that, I´m not sure how to use "initial" for > > > > > > limiting a queryset ... > > > > > > ### with using formset_factory on the other hand, I could use a > > > > > > ModelChoiceField with queryset=Attachment.objects.filter(user=user), > > > > > > but how do I get the "user" there? > > > > > > ### Overriding __init__ within BlogEntryAttachmentForm and passing > > > > > > the > > > > > > currently logged-in user seems possible, but then I also have to > > > > > > override the BaseFormSets __init__ and _construct_form, which just > > > > > > seems ugly and far too complicated. > > > > > > > MODELS: > > > > > > > class Attachment(models.Model): > > > > > > > user = models.ForeignKey('auth.User') > > > > > > title = models.CharField('Title', max_length=100, blank=True, > > > > > > null=True) > > > > > > filename = models.CharField('Filename', max_length=100, > > > > > > blank=True, null=True) > > > > > > path = models.CharField('Path', max_length=200, blank=True, > > > > > > null=True) > > > > > > ... > > > > > > > class BlogEntryAttachment(models.Model): > > > > > > > blogentry = models.ForeignKey(BlogEntry) > > > > > > attachment = models.ForeignKey(Attachment) > > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: formset issue
that seems to be possible. on the other hand, it also seems to be a strange way to go: instantiating the formset (retrieving all data/ querysets) and then changing the querysets? performance-wise, this is probably not a good solution. and I doubt that this is a good decision design-wise ... thanks, patrick On Sep 1, 1:51 pm, koenb <[EMAIL PROTECTED]> wrote: > I have not tried this, but can't you just in your view instantiate the > formset and then loop over the forms and set the queryset on your > field ? > > Koen > > On 1 sep, 09:24, patrickk <[EMAIL PROTECTED]> wrote: > > > found a solution: with using threadlocals, it´s possible to define a > > custom manager which only returns the data assigned to the currently > > logged-in user. since threadlocals has been considered a "hack" > > recently by one of the main django-developers (I think it was > > malcolm), this is probably not the best thing to do though. > > > On Aug 31, 4:35 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > sorry for being so annoying with this issue, but I´m asking this one > > > more time. > > > > - with using inlineformset_factory, my problem is that limiting the > > > choices to the currently logged-in user seems impossible. I´ve just > > > tried to write a custom manger with "use_for_related_fields = True", > > > but it´s also not possible to limit the basic queryset to the current > > > user. > > > > so - before going back to pure forms and re-writing the main part of > > > my application, I´m just asking this question for the last time ... > > > > thanks, > > > patrick > > > > On Aug 27, 9:04 am, patrickk <[EMAIL PROTECTED]> wrote: > > > > > anyone? > > > > > On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > > scenario: users are uploading documents (e.g. images, files, ...). > > > > > these documents are saved in a model "Attachment" assigned to the > > > > > currently logged-in "User". now, every user has the possibility to > > > > > attach documents to a blog-entry. these attachments are saved in a > > > > > model "BlogEntryAttachment" assigned to a "BlogEntry". > > > > > the best way to achieve this is probably using formsets. when writing > > > > > a BlogEntry, the user has the option to attach his/her documents to > > > > > that BlogEntry. to achieve this, I have to limit the choices (of a > > > > > select-field) to the currenlty logged-in User. > > > > > > questions & notes: > > > > > ### how can I limit the choices of a select-field using formsets? > > > > > ### I've tried inlineformset_factory, but there's no argument > > > > > "initial". besides that, I´m not sure how to use "initial" for > > > > > limiting a queryset ... > > > > > ### with using formset_factory on the other hand, I could use a > > > > > ModelChoiceField with queryset=Attachment.objects.filter(user=user), > > > > > but how do I get the "user" there? > > > > > ### Overriding __init__ within BlogEntryAttachmentForm and passing the > > > > > currently logged-in user seems possible, but then I also have to > > > > > override the BaseFormSets __init__ and _construct_form, which just > > > > > seems ugly and far too complicated. > > > > > > MODELS: > > > > > > class Attachment(models.Model): > > > > > > user = models.ForeignKey('auth.User') > > > > > title = models.CharField('Title', max_length=100, blank=True, > > > > > null=True) > > > > > filename = models.CharField('Filename', max_length=100, > > > > > blank=True, null=True) > > > > > path = models.CharField('Path', max_length=200, blank=True, > > > > > null=True) > > > > > ... > > > > > > class BlogEntryAttachment(models.Model): > > > > > > blogentry = models.ForeignKey(BlogEntry) > > > > > attachment = models.ForeignKey(Attachment) > > > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: formset issue
I have not tried this, but can't you just in your view instantiate the formset and then loop over the forms and set the queryset on your field ? Koen On 1 sep, 09:24, patrickk <[EMAIL PROTECTED]> wrote: > found a solution: with using threadlocals, it´s possible to define a > custom manager which only returns the data assigned to the currently > logged-in user. since threadlocals has been considered a "hack" > recently by one of the main django-developers (I think it was > malcolm), this is probably not the best thing to do though. > > On Aug 31, 4:35 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > sorry for being so annoying with this issue, but I´m asking this one > > more time. > > > - with using inlineformset_factory, my problem is that limiting the > > choices to the currently logged-in user seems impossible. I´ve just > > tried to write a custom manger with "use_for_related_fields = True", > > but it´s also not possible to limit the basic queryset to the current > > user. > > > so - before going back to pure forms and re-writing the main part of > > my application, I´m just asking this question for the last time ... > > > thanks, > > patrick > > > On Aug 27, 9:04 am, patrickk <[EMAIL PROTECTED]> wrote: > > > > anyone? > > > > On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > > scenario: users are uploading documents (e.g. images, files, ...). > > > > these documents are saved in a model "Attachment" assigned to the > > > > currently logged-in "User". now, every user has the possibility to > > > > attach documents to a blog-entry. these attachments are saved in a > > > > model "BlogEntryAttachment" assigned to a "BlogEntry". > > > > the best way to achieve this is probably using formsets. when writing > > > > a BlogEntry, the user has the option to attach his/her documents to > > > > that BlogEntry. to achieve this, I have to limit the choices (of a > > > > select-field) to the currenlty logged-in User. > > > > > questions & notes: > > > > ### how can I limit the choices of a select-field using formsets? > > > > ### I've tried inlineformset_factory, but there's no argument > > > > "initial". besides that, I´m not sure how to use "initial" for > > > > limiting a queryset ... > > > > ### with using formset_factory on the other hand, I could use a > > > > ModelChoiceField with queryset=Attachment.objects.filter(user=user), > > > > but how do I get the "user" there? > > > > ### Overriding __init__ within BlogEntryAttachmentForm and passing the > > > > currently logged-in user seems possible, but then I also have to > > > > override the BaseFormSets __init__ and _construct_form, which just > > > > seems ugly and far too complicated. > > > > > MODELS: > > > > > class Attachment(models.Model): > > > > > user = models.ForeignKey('auth.User') > > > > title = models.CharField('Title', max_length=100, blank=True, > > > > null=True) > > > > filename = models.CharField('Filename', max_length=100, > > > > blank=True, null=True) > > > > path = models.CharField('Path', max_length=200, blank=True, > > > > null=True) > > > > ... > > > > > class BlogEntryAttachment(models.Model): > > > > > blogentry = models.ForeignKey(BlogEntry) > > > > attachment = models.ForeignKey(Attachment) > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: formset issue
found a solution: with using threadlocals, it´s possible to define a custom manager which only returns the data assigned to the currently logged-in user. since threadlocals has been considered a "hack" recently by one of the main django-developers (I think it was malcolm), this is probably not the best thing to do though. On Aug 31, 4:35 pm, patrickk <[EMAIL PROTECTED]> wrote: > sorry for being so annoying with this issue, but I´m asking this one > more time. > > - with using inlineformset_factory, my problem is that limiting the > choices to the currently logged-in user seems impossible. I´ve just > tried to write a custom manger with "use_for_related_fields = True", > but it´s also not possible to limit the basic queryset to the current > user. > > so - before going back to pure forms and re-writing the main part of > my application, I´m just asking this question for the last time ... > > thanks, > patrick > > On Aug 27, 9:04 am, patrickk <[EMAIL PROTECTED]> wrote: > > > anyone? > > > On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > > scenario: users are uploading documents (e.g. images, files, ...). > > > these documents are saved in a model "Attachment" assigned to the > > > currently logged-in "User". now, every user has the possibility to > > > attach documents to a blog-entry. these attachments are saved in a > > > model "BlogEntryAttachment" assigned to a "BlogEntry". > > > the best way to achieve this is probably using formsets. when writing > > > a BlogEntry, the user has the option to attach his/her documents to > > > that BlogEntry. to achieve this, I have to limit the choices (of a > > > select-field) to the currenlty logged-in User. > > > > questions & notes: > > > ### how can I limit the choices of a select-field using formsets? > > > ### I've tried inlineformset_factory, but there's no argument > > > "initial". besides that, I´m not sure how to use "initial" for > > > limiting a queryset ... > > > ### with using formset_factory on the other hand, I could use a > > > ModelChoiceField with queryset=Attachment.objects.filter(user=user), > > > but how do I get the "user" there? > > > ### Overriding __init__ within BlogEntryAttachmentForm and passing the > > > currently logged-in user seems possible, but then I also have to > > > override the BaseFormSets __init__ and _construct_form, which just > > > seems ugly and far too complicated. > > > > MODELS: > > > > class Attachment(models.Model): > > > > user = models.ForeignKey('auth.User') > > > title = models.CharField('Title', max_length=100, blank=True, > > > null=True) > > > filename = models.CharField('Filename', max_length=100, > > > blank=True, null=True) > > > path = models.CharField('Path', max_length=200, blank=True, > > > null=True) > > > ... > > > > class BlogEntryAttachment(models.Model): > > > > blogentry = models.ForeignKey(BlogEntry) > > > attachment = models.ForeignKey(Attachment) > > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: formset issue
sorry for being so annoying with this issue, but I´m asking this one more time. - with using inlineformset_factory, my problem is that limiting the choices to the currently logged-in user seems impossible. I´ve just tried to write a custom manger with "use_for_related_fields = True", but it´s also not possible to limit the basic queryset to the current user. so - before going back to pure forms and re-writing the main part of my application, I´m just asking this question for the last time ... thanks, patrick On Aug 27, 9:04 am, patrickk <[EMAIL PROTECTED]> wrote: > anyone? > > On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > > > scenario: users are uploading documents (e.g. images, files, ...). > > these documents are saved in a model "Attachment" assigned to the > > currently logged-in "User". now, every user has the possibility to > > attach documents to a blog-entry. these attachments are saved in a > > model "BlogEntryAttachment" assigned to a "BlogEntry". > > the best way to achieve this is probably using formsets. when writing > > a BlogEntry, the user has the option to attach his/her documents to > > that BlogEntry. to achieve this, I have to limit the choices (of a > > select-field) to the currenlty logged-in User. > > > questions & notes: > > ### how can I limit the choices of a select-field using formsets? > > ### I've tried inlineformset_factory, but there's no argument > > "initial". besides that, I´m not sure how to use "initial" for > > limiting a queryset ... > > ### with using formset_factory on the other hand, I could use a > > ModelChoiceField with queryset=Attachment.objects.filter(user=user), > > but how do I get the "user" there? > > ### Overriding __init__ within BlogEntryAttachmentForm and passing the > > currently logged-in user seems possible, but then I also have to > > override the BaseFormSets __init__ and _construct_form, which just > > seems ugly and far too complicated. > > > MODELS: > > > class Attachment(models.Model): > > > user = models.ForeignKey('auth.User') > > title = models.CharField('Title', max_length=100, blank=True, > > null=True) > > filename = models.CharField('Filename', max_length=100, > > blank=True, null=True) > > path = models.CharField('Path', max_length=200, blank=True, > > null=True) > > ... > > > class BlogEntryAttachment(models.Model): > > > blogentry = models.ForeignKey(BlogEntry) > > attachment = models.ForeignKey(Attachment) > > > > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: formset issue
anyone? On Aug 26, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote: > scenario: users are uploading documents (e.g. images, files, ...). > these documents are saved in a model "Attachment" assigned to the > currently logged-in "User". now, every user has the possibility to > attach documents to a blog-entry. these attachments are saved in a > model "BlogEntryAttachment" assigned to a "BlogEntry". > the best way to achieve this is probably using formsets. when writing > a BlogEntry, the user has the option to attach his/her documents to > that BlogEntry. to achieve this, I have to limit the choices (of a > select-field) to the currenlty logged-in User. > > questions & notes: > ### how can I limit the choices of a select-field using formsets? > ### I've tried inlineformset_factory, but there's no argument > "initial". besides that, I´m not sure how to use "initial" for > limiting a queryset ... > ### with using formset_factory on the other hand, I could use a > ModelChoiceField with queryset=Attachment.objects.filter(user=user), > but how do I get the "user" there? > ### Overriding __init__ within BlogEntryAttachmentForm and passing the > currently logged-in user seems possible, but then I also have to > override the BaseFormSets __init__ and _construct_form, which just > seems ugly and far too complicated. > > MODELS: > > class Attachment(models.Model): > > user = models.ForeignKey('auth.User') > title = models.CharField('Title', max_length=100, blank=True, > null=True) > filename = models.CharField('Filename', max_length=100, > blank=True, null=True) > path = models.CharField('Path', max_length=200, blank=True, > null=True) > ... > > class BlogEntryAttachment(models.Model): > > blogentry = models.ForeignKey(BlogEntry) > attachment = models.ForeignKey(Attachment) > --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
formset issue
scenario: users are uploading documents (e.g. images, files, ...). these documents are saved in a model "Attachment" assigned to the currently logged-in "User". now, every user has the possibility to attach documents to a blog-entry. these attachments are saved in a model "BlogEntryAttachment" assigned to a "BlogEntry". the best way to achieve this is probably using formsets. when writing a BlogEntry, the user has the option to attach his/her documents to that BlogEntry. to achieve this, I have to limit the choices (of a select-field) to the currenlty logged-in User. questions & notes: ### how can I limit the choices of a select-field using formsets? ### I've tried inlineformset_factory, but there's no argument "initial". besides that, I´m not sure how to use "initial" for limiting a queryset ... ### with using formset_factory on the other hand, I could use a ModelChoiceField with queryset=Attachment.objects.filter(user=user), but how do I get the "user" there? ### Overriding __init__ within BlogEntryAttachmentForm and passing the currently logged-in user seems possible, but then I also have to override the BaseFormSets __init__ and _construct_form, which just seems ugly and far too complicated. MODELS: class Attachment(models.Model): user = models.ForeignKey('auth.User') title = models.CharField('Title', max_length=100, blank=True, null=True) filename = models.CharField('Filename', max_length=100, blank=True, null=True) path = models.CharField('Path', max_length=200, blank=True, null=True) ... class BlogEntryAttachment(models.Model): blogentry = models.ForeignKey(BlogEntry) attachment = models.ForeignKey(Attachment) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---