Re: Limiting choices for ModelForm's ForeignKey/ManyToMany

2012-01-06 Thread Kevin
Thanks, this was exactly what I was looking for, here's the code I
used:

form.fields["rate"].queryset =
Rate.objects.filter(company_id=the_company.id)

Although with my own fields, and it worked perfectly.

Something else that other users may use more than this type would be
something like this:

form.fields["rate"].queryset = Rate.objects.filter(owner_id=req.user)

The request object cannot be accessed when building the ModelForm
class, for setting a filter such as this.  Although this filter can be
easily done a different way.

On Jan 4, 2:45 am, <michael.pimmer@boehringer-ingelheim.com>
wrote:
> > I'm not sure if this will
>
> work, as the ModelForm's QuerySet for the choiceField cannot be
> changed dynamically during runtime, or can it?
>
> You can change the queryset directly in the view:
>
> http://stackoverflow.com/questions/291945/how-do-i-filter-foreignkey-...
> in-a-django-modelform
>
> Altough having this stored in the model would be more elegant in your case,
> perhaps anyone else has knows more..
>
> Michael
>
>
>
>
>
>
>
> -Original Message-
> From: django-users@googlegroups.com on behalf of Kevin
> Sent: Wed 1/4/2012 8:07 AM
> To: Django users
> Subject: Limiting choices for ModelForm's ForeignKey/ManyToMany
>
> Hello,
>
>   I have many models which have a foreign key to a main model. Eg:
>
> class MainModel:
>   ..
>   ..
>
> class VariousModels:
>   main = ForeignKey(MainModel)
>   ..
>   ..
>
> Now, these VariousModels sometimes have links to each other in the
> form of ForeignKey or ManyToMany.  I need to limit these ForeignKey
> and ManyToMany in various ModelForms to only display other
> VariousModels which share the same MainModel in common.  Here is what
> I tried to do and it didn't work:
>
> item = models.ForeignKey(Item, limit_choices_to={'main__pk':main},
> blank=True, null=True)
>
> I attempted different versions of this on the actual model.  I am now
> looking into how I can perform this using a ModelForm instead of
> placing the limits on the actual model.  I'm not sure if this will
> work, as the ModelForm's QuerySet for the choiceField cannot be
> changed dynamically during runtime, or can it?
>
> Basically, I am creating a multi-user/multi-section application.  The
> main model described above is a section which a user creates and
> manages.  The user should only see choices in the forms for objects
> associated with the current section they are editing.
>
> Perhaps I am going about this entirely wrong and this should be
> implemented in a different form, such as a permission.
>
> Any ideas on how this can be done would be very helpful, 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-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/django-users?hl=en.
>
>
>
>  winmail.dat
> 5KViewDownload

-- 
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: Limiting choices for ModelForm's ForeignKey/ManyToMany

2012-01-04 Thread Matt Schinckel
I've been playing around with a reusable/declarative syntax for doing this, 
as I seem to do it all of the time.

https://bitbucket.org/schinckel/django-filtered-form

You inherit from FilteredForm, and set either simple 'filters' or 
'instance_filters' attributes on the form class:

class MyModelForm(FilteredForm):
filters = {'staff': models.Q(deleted=False)}
instance_filters = {'staff': 'company.people'}

'filters' allows you to use Q objects to filter the related objects (in 
this case, only selecting non-deleted staff), and instance_filters allows 
you to set the queryset based on an attribute that the instance has. In the 
case above, it would set the queryset for staff to the MyModel's 
company.people attribute chain, and then filter those objects based on the 
deleted attribute.

Until about 5 minutes ago it was a private repo on bitbucket: I'm using it 
on a project (in production), but it needs cleaning up, documenting and 
packaging.

Matt.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/q3KkDec7XUQJ.
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: Limiting choices for ModelForm's ForeignKey/ManyToMany

2012-01-04 Thread michael.pimmer.ext
> I'm not sure if this will
work, as the ModelForm's QuerySet for the choiceField cannot be
changed dynamically during runtime, or can it?

You can change the queryset directly in the view:

http://stackoverflow.com/questions/291945/how-do-i-filter-foreignkey-choices-
in-a-django-modelform

Altough having this stored in the model would be more elegant in your case,
perhaps anyone else has knows more..

Michael


-Original Message-
From: django-users@googlegroups.com on behalf of Kevin
Sent: Wed 1/4/2012 8:07 AM
To: Django users
Subject: Limiting choices for ModelForm's ForeignKey/ManyToMany
 
Hello,

  I have many models which have a foreign key to a main model. Eg:

class MainModel:
  ..
  ..

class VariousModels:
  main = ForeignKey(MainModel)
  ..
  ..

Now, these VariousModels sometimes have links to each other in the
form of ForeignKey or ManyToMany.  I need to limit these ForeignKey
and ManyToMany in various ModelForms to only display other
VariousModels which share the same MainModel in common.  Here is what
I tried to do and it didn't work:

item = models.ForeignKey(Item, limit_choices_to={'main__pk':main},
blank=True, null=True)

I attempted different versions of this on the actual model.  I am now
looking into how I can perform this using a ModelForm instead of
placing the limits on the actual model.  I'm not sure if this will
work, as the ModelForm's QuerySet for the choiceField cannot be
changed dynamically during runtime, or can it?

Basically, I am creating a multi-user/multi-section application.  The
main model described above is a section which a user creates and
manages.  The user should only see choices in the forms for objects
associated with the current section they are editing.

Perhaps I am going about this entirely wrong and this should be
implemented in a different form, such as a permission.

Any ideas on how this can be done would be very helpful, 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-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.


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

<>

Limiting choices for ModelForm's ForeignKey/ManyToMany

2012-01-03 Thread Kevin
Hello,

  I have many models which have a foreign key to a main model. Eg:

class MainModel:
  ..
  ..

class VariousModels:
  main = ForeignKey(MainModel)
  ..
  ..

Now, these VariousModels sometimes have links to each other in the
form of ForeignKey or ManyToMany.  I need to limit these ForeignKey
and ManyToMany in various ModelForms to only display other
VariousModels which share the same MainModel in common.  Here is what
I tried to do and it didn't work:

item = models.ForeignKey(Item, limit_choices_to={'main__pk':main},
blank=True, null=True)

I attempted different versions of this on the actual model.  I am now
looking into how I can perform this using a ModelForm instead of
placing the limits on the actual model.  I'm not sure if this will
work, as the ModelForm's QuerySet for the choiceField cannot be
changed dynamically during runtime, or can it?

Basically, I am creating a multi-user/multi-section application.  The
main model described above is a section which a user creates and
manages.  The user should only see choices in the forms for objects
associated with the current section they are editing.

Perhaps I am going about this entirely wrong and this should be
implemented in a different form, such as a permission.

Any ideas on how this can be done would be very helpful, 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-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.