manytomany manager and get_query_set

2009-09-02 Thread bnl

hi Folks

I have a model which has in it some stuff that looks like:

class Simulation(models.Model):
...
boundaryCondition=models.ManyToManyField
('SimCoupling',blank=True,null=True)
...

class Coupling(models.Model):
   ...

class SimCoupling(models.Model):
...
original=models.ForeignKey(Coupling)
...

In the shell I can do

> from protoq.models import *
> s=Simulation.objects.get(id=13)
> bc=s.boundaryCondition

and obviously bc is a manytomany manager.

Now, what I want to do is loop over those boundaryCondition related
objects. I understand how to get a list of dictionary versions, but I
want the actual SimCoupling instances themselves. I thought I could
get the query set and just ask for the objects ...

but:

>>> bc.values()
[{'targetInput_id': 1, 'component_id': 1, 'couplingFreqUnits_id': 19,
'couplingType_id': 14, 'original_id': 1, 'manipulation': u'asdf',
'id': 28, 'couplingFreq': 122}]
>>> bc.all()
[]
>>> bc.get_query_set()
[]
>>> bc.get_query_set().all()
[]
>>> bc.get_query_set().values()
[{'targetInput_id': 1, 'component_id': 1, 'couplingFreqUnits_id': 19,
'couplingType_id': 14, 'original_id': 1, 'manipulation': u'asdf',
'id': 28, 'couplingFreq': 122}]

I think I'm being stupid, and having one of those "woods and trees"
moments. How do I get to a list of the objects or something I can
iterate over? I know it's going to be obvious, and last week I
probably knew it ... :-(

(I wanted to be able to get a list like this:
 bclist= [m.original for m in bc.something]
I know I can get this the hard way from the dict ... but ...)

Thanks in advance,
Bryan





--~--~-~--~~~---~--~~
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: looping over forms in a formset

2009-08-11 Thread bnl

Thanks.

Before I suggest anything concrete for the docs, let's see if my
understanding  is now right:

The problem from my point of view was that I didn't think I had any
hidden fields. Hence I didn't loop over them, and didn't think of
them  - yes, despite the massive hint in the error message ... :-(

I think the hidden field in question was in fact the id of the
instance (which exists for the previously saved objects) and was zero
for a "blank" form in the formset ... and I think this only shows up
as an issue when looping over forms in a formset, because the formset
machinery has set up, modified, and uses this hidden field - and I
didn't know it was there.

(Or is it always there for anything generated by a ModelForm, and I've
never spotted it because I always get my id from my url?)

(Is this really an edge case? It seems like something one would want
to do for nearly any interesting formset ... lay it out nicely ...)

Cheers
Bryan

On Aug 11, 9:45 am, Malcolm Tredinnick 
wrote:
> On Tue, 2009-08-11 at 01:12 -0700,bnlwrote:
>
> [...]
>
> > Apologies that I'm not asking my questions in the way you'd like,
> > but believe me, I am cutting it down a lot ... and I appreciate that
> > it's still not obvious where the errors are (I would have found
> > them otherwise). In this case, I had cut it down to just field, and
> > it
> > exhibited the problem ... I shouldn't have included the extra line
> > which was just to show why I wanted to do it ...
>
> Trimming a problem report to the minimum required and no further is part
> science and part black art, so there are going to be times when you just
> get unlucky. In this case, however, the problem is you aren't including
> details so that I or anybody else can actually reproduce the problem. So
> you end up in a position where you have to hope the particular error
> message triggers a "we've seen that before" thought in somebody's head.
>
> A good problem report or request for help contains enough information to
> repeat the problem. Which means, in this case, the form class containing
> the field.
>
>
>
> > It would seem that the advice to loop over hidden fields in the
> > template could be promoted to the documentation.
>
> Well, we already document, in the main formset documentation, including
> the management form if you're doing manual template layout
> (http://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-a-f...) 
> and we document including hidden fields if you're doing iteration over form 
> fields, in the main forms documentation, 
> (http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-hidde...) so 
> this isn't undocumented territory.
>
> However, if you feel there's a clearer way to do this without giving it
> undue prominence -- bearing in mind it's an edge case, so shouldn't
> obscure the more regular usage cases or weigh those sections down with
> heavy details -- then please do create a patch and attach it to a
> ticket. Many of our documentation improvements are generated by people
> trying to make something clearer.
>
> 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: looping over forms in a formset

2009-08-11 Thread bnl

Hi Malcolm

Thanks for looking in at this. There is some magic aura about this
group.
Both times I've posted to it, I've found other folks solutions that I
could
not find before I finally, in desperation, post ... and then magically
they
appear ... (In this case, I finally found the combination of search
terms
that popped the answer to the top ... and that only a few minutes
after I posted).

So, the solution was to loop over hidden fields per formset form
within
the template and then it worked fine.

Apologies that I'm not asking my questions in the way you'd like,
but believe me, I am cutting it down a lot ... and I appreciate that
it's still not obvious where the errors are (I would have found
them otherwise). In this case, I had cut it down to just field, and
it
exhibited the problem ... I shouldn't have included the extra line
which was just to show why I wanted to do it ...

It would seem that the advice to loop over hidden fields in the
template could be promoted to the documentation. It's not an
obvious thing to do ... especially since the hidden fields in
question seem to be per form management (and one
can't write out a management_form per form within a
formset and this seems to be fundamentally the same issue).

Cheers
Bryan


On 11 Aug, 02:35, Malcolm Tredinnick  wrote:
> On Mon, 2009-08-10 at 08:59 -0700,bnlwrote:
> > hi folks
>
> > I have a formset, which I want to display using a customised template
> > for the forms.
>
> > This works:
>
> > {{formset.mangement_form}}
> > {% for form in formset.forms %}
> >   {{form}}
> > {% endfor %}
>
> > The following displays ok, let's me edit one form, but then I can't
> > update it or add another, failling with a muMultiValueDictKeyError
> > at 
> > "Key 'form-0-id' not found in  > 1.0 blew up with a keyerror).
>
> > {{formset.management_forms}}
> > {% for form in formset.forms %}
> > {{form.title.errors}}{{form.title}}
> > etc looping over the fields in a nice way ...
> > {% endfor %}
>
> > Looking at the posted form in an editor, I see the following (with
> > django 1.0) in the form that works:
> > 
> > and
> > 
> > in the one that doesn't.
>
> Since the one that doesn't work doesn't have any value associated with
> it, it won't be submitted as a form element (which is why you are not
> seeing it as a key in the form data). In other words, it's being
> rendered incorrectly. So it's working fine, given what is being
> rendered.
>
>
>
> > Any idea what I'm doing wrong? need to do?
>
> The details are hidden in the line you have written as "looping over the
> fields in a nice way".
>
> Construct a small self-contained example of a form and formset class and
> show exactly how you are attempting to display the form fields and we
> might have some chance of spotting the problem. Work hard on removing
> every detail that isn't necessary -- removing form elements and formset
> details as much as possible until the error disappears. Ideally, you'll
> end up with a form with only one field and it will become apparent what
> the problem is.
>
> 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: looping over forms in a formset

2009-08-10 Thread bnl

Why I didn't find this before? It seems Paul had the same problem, and
one has to loop
over the hidden fields in each form ...

 {% for hid in form.hidden_fields %} {{hid}}{% endfor %}




On Aug 10, 4:59 pm, bnl <bryannlawre...@googlemail.com> wrote:
> hi folks
>
> I have a formset, which I want to display using a customised template
> for the forms.
>
> This works:
>
> {{formset.mangement_form}}
> {% for form in formset.forms %}
>   {{form}}
> {% endfor %}
>
> The following displays ok, let's me edit one form, but then I can't
> update it or add another, failling with a muMultiValueDictKeyError
> at 
> "Key 'form-0-id' not found in  1.0 blew up with a keyerror).
>
> {{formset.management_forms}}
> {% for form in formset.forms %}
> {{form.title.errors}}{{form.title}}
> etc looping over the fields in a nice way ...
> {% endfor %}
>
> Looking at the posted form in an editor, I see the following (with
> django 1.0) in the form that works:
> 
> and
> 
> in the one that doesn't.
>
> Any idea what I'm doing wrong? need to do?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



looping over forms in a formset

2009-08-10 Thread bnl

hi folks

I have a formset, which I want to display using a customised template
for the forms.

This works:

{{formset.mangement_form}}
{% for form in formset.forms %}
  {{form}}
{% endfor %}

The following displays ok, let's me edit one form, but then I can't
update it or add another, failling with a muMultiValueDictKeyError
at 
"Key 'form-0-id' not found in 
and

in the one that doesn't.

Any idea what I'm doing wrong? need to do?





--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Getting at the fields querysets of a form in a formset

2009-08-04 Thread bnl

Hi Folks

I'm trying to move up from a complex form based on a model declaration
to a model formset built on the same idea.

I have a model which has some stuff that looks something like

class Vocab(models.Model):
''' Holds the values of a choice list aka vocabulary '''
name=models.CharField(max_length=64)
class Value(models.Model):
''' Vocabulary Values '''
value=models.CharField(max_length=64)
vocab=models.ForeignKey(Vocab)
class Coupling:
couplingType=model.ForeignKey(Vocab)
class CouplingForm(form.modelForm):
class Meta:
model=Coupling
class MyCouplingForm(CouplingForm):
def __init__(self,*args,**kwargs):
CouplingForm.__init__(self,*args,**kwargs)
vocab=Vocab.objects.get(name='couplingType')
self.fields['couplingType].queryset=Value.objects.filter
(vocab=vocab)

It's a bit more complex than that (understatement, I hope I haven't
mucked up the actuality in simplifying it), but I hope you get the
picture ...

Anyway, that works fine ... but now I want to build a formset not just
a form ...

Ideally, I want to apply the same logic to the formset that I do to
the form (via subclassing). However, I can't quite work out how to do
it.

I was thinking I should try

CouplingFormSet=modelformset_factory(Coupling)

and somehow subclass CouplingFormSet or pass some arguments to the
formset factory method, but I can't find any docs on how to  replicate
the above functionality (I can see that I can limit the queryset for
the Couplings, but not for the attributes, like couplingType).

How does one get to the fields of the underlying forms in a formset?

I've had a nosy at the django code, but reckon I'd be days
understanding it enough to work this out.

Has anyone done anything like this that could give me some advice, or
point me to a prevoius discussion on something like this?

Thanks in advance,
Bryan

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---