Re: limit_choices_to: getting access to current instance

2007-01-31 Thread qhfgva

Well, either I'm not able to follow your example or we are talking
about different things.  In any case here is what I got working as a
first draft.  As a big benefit to me is the fact that I now understand
these inner workings a little better.  I was hoping to get away with
simple wrappers to generic views but now I don't think that will be
the case but it also doesn't worry me.  :)

This is closely adapted from:  http://code.pui.ch/2007/01/07/using-
djangos-newforms/

from django import newforms as forms
from django.newforms import widgets
from django.template import loader, Context
from django.http import HttpResponse

def create_update_animal(request, animal_id=None):
animal = None
if animal_id:
animal = Animal.objects.get(id=animal_id)
animal_form = forms.models.form_for_instance(animal)
else:
animal_form = forms.models.form_for_model(Animal)

if animal:
available_protocols = Protocol.allowed_for_animal(animal)
else:
available_protocols = Protocol.active_list()

protocol_choices = [('','')] + [(p.id,p.number) for p in
available_protocols]
animal_form.base_fields['protocol'].widget.choices =
protocol_choices
if animal:
animal_form.base_fields['protocol'].widget.initial =
animal.protocol_id

if request.method == 'POST':
form = animal_form(request.POST)

if form.is_valid():
form.save()
return HttpResponseRedirect("/")
else:
form = animal_form()
t = loader.get_template('tracker/animal_form.html')

c = Context({ 'form': form, })

return HttpResponse(t.render(c))


If there is a way to do the above just using limit_choices_to, I'd
still like to understand that method.  Also if I'm doing anything
particularly hacky feel free to mock me (but also instruct me as to a
better way).

thanks.


On Jan 30, 5:04 pm, "quentinsf" <[EMAIL PROTECTED]> wrote:
> Sorry, the reference to 'Display' should have been to 'MyObject'


--~--~-~--~~~---~--~~
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: limit_choices_to: getting access to current instance

2007-01-30 Thread quentinsf

Sorry, the reference to 'Display' should have been to 'MyObject'


--~--~-~--~~~---~--~~
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: limit_choices_to: getting access to current instance

2007-01-30 Thread quentinsf

I'm not quite sure if this is helpful - it's not a general solution on 
the ForeignKey specification so wouldn't work in admin, for example, 
but it worked for me with my own form...

I was creating a view for a 'change' form.  One parameter passed to 
the view (from the URL) was an organisation identifier.I wanted to 
restrict the choices in a ForeignKey form field to those which were 
relevant to that organisation.  This seemed to work:

 def myobject_edit(request, org_id, obj_id):
 org = get_object_or_404(Organisation, pk=org_id)
 manip = Display.ChangeManipulator(obj_id)
 manip['otherobj'].choices = [
 (e['id'], e['name']) for e in 
OtherObj.objects.filter(org__exact=org).values()
 ]
 ...


--~--~-~--~~~---~--~~
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: limit_choices_to: getting access to current instance

2007-01-30 Thread [EMAIL PROTECTED]

As far as I understand, you could certainly implement the same 
functionality in a view using newforms to validate the input. To do it 
in the model, something like the patch suggested in the ticket or the 
approach linked to at the end of the comments on that ticket would be 
needed. The code here shows the crucial ingredient:

def get_limit_choices_to(self):
limiters = {}
if self.limit_choices_to:
for id in self.limit_choices_to:
value = self.limit_choices_to[id]
if callable(value):
value = value()
limiters[id] = value
return limiters

The check for whether limit_choices_to is a callable() is what you 
need: the ability to have limit_choices_to evaluated at runtime rather 
than just an array stuck into the model that is checked against. If 
you look into the Django model API documentation example that uses 
models.LazyDate() and do some hunting around code.djangoproject.com 
for discussion about LazyDate, you'll see that people have done some 
pretty odd things to delay the checking of limit_choices_to until the 
model class is actually instantiated.

IN SHORT: ;-) the get_assigned_tasks_id_list(blah, blah) is just a 
stand in for whatever code you might want to run when the model is 
instantiated to check what should allowed as input.

On Jan 29, 1:57 pm, "qhfgva" <[EMAIL PROTECTED]> wrote:
> Thanks for the response.  I'm not sure if this exactly covers my case
> but I like this feature.  From the example given in the ticket:
>
> def assigned_tasks():
>return get_assigned_tasks_id_list(blah, blah)
>
> class TimeRecord(models.Model):
>task = models.ForeignKey(Task, limit_choices_to = {'id__in':
> assigned_tasks})
>
> I would like the assigned_tasks function to be have a reference to and
> be able to calculate values based on the current instance of
> TimeRecord.Perhaps that is what blah or blah are but I'm guessing
> not.
>
> Do I need to do something like work directly with newforms to achieve
> something like this?
>
> On Jan 29, 6:54 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> wrote:
>
> > See patch on ticket .
>
> > On Jan 29, 8:31 am, "qhfgva" <[EMAIL PROTECTED]> wrote:
>
> > > If my original post was not entirely clear, I'm interested in any
> > > method available to limit_choices_to that lets you dynamically create
> > > the list of available options as a function of the current model
> > > instance.
>
> > > thanks.
>
> > > On Jan 28, 4:28 pm, "qhfgva" <[EMAIL PROTECTED]> wrote:
>
> > > > I currently have a drop down for a model where I'd only like to show
> > > > choices that are currently "active".  That's easy enough with:
>
> > > > foo  = models.ForeignKey(Foo,
> > > > limit_choices_to={'active__exact':True})
>
> > > > But if someone is working with a model where the "foo" that they had
> > > > previously selected has been subsequently set to inactive they should
> > > > be able to keep that foo (they are "grandfathered" in).
>
> > > > In effect I'd like limit_choices_to to have a list of foo.active ==
> > > > True plus the current selection they have from the past if it is not
> > > > an active choice.
>
> > > > If I could make a method of Foo such as Foo.allowed_choice_for_x  and
> > > > then pass it an instance of the current object, then it seems like I
> > > > could easily generate the specific drop downlist on the fly but I'm
> > > > not sure how to do that (or if it's possible).
>
> > > > Thoughts?


--~--~-~--~~~---~--~~
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: limit_choices_to: getting access to current instance

2007-01-29 Thread qhfgva

Thanks for the response.  I'm not sure if this exactly covers my case 
but I like this feature.  From the example given in the ticket:

def assigned_tasks():
   return get_assigned_tasks_id_list(blah, blah)

class TimeRecord(models.Model):
   task = models.ForeignKey(Task, limit_choices_to = {'id__in': 
assigned_tasks})


I would like the assigned_tasks function to be have a reference to and 
be able to calculate values based on the current instance of 
TimeRecord.Perhaps that is what blah or blah are but I'm guessing 
not.

Do I need to do something like work directly with newforms to achieve 
something like this?

On Jan 29, 6:54 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> 
wrote:
> See patch on ticket .
>
> On Jan 29, 8:31 am, "qhfgva" <[EMAIL PROTECTED]> wrote:
>
> > If my original post was not entirely clear, I'm interested in any
> > method available to limit_choices_to that lets you dynamically create
> > the list of available options as a function of the current model
> > instance.
>
> > thanks.
>
> > On Jan 28, 4:28 pm, "qhfgva" <[EMAIL PROTECTED]> wrote:
>
> > > I currently have a drop down for a model where I'd only like to show
> > > choices that are currently "active".  That's easy enough with:
>
> > > foo  = models.ForeignKey(Foo,
> > > limit_choices_to={'active__exact':True})
>
> > > But if someone is working with a model where the "foo" that they had
> > > previously selected has been subsequently set to inactive they should
> > > be able to keep that foo (they are "grandfathered" in).
>
> > > In effect I'd like limit_choices_to to have a list of foo.active ==
> > > True plus the current selection they have from the past if it is not
> > > an active choice.
>
> > > If I could make a method of Foo such as Foo.allowed_choice_for_x  and
> > > then pass it an instance of the current object, then it seems like I
> > > could easily generate the specific drop downlist on the fly but I'm
> > > not sure how to do that (or if it's possible).
>
> > > Thoughts?


--~--~-~--~~~---~--~~
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: limit_choices_to: getting access to current instance

2007-01-29 Thread [EMAIL PROTECTED]

See patch on ticket .

On Jan 29, 8:31 am, "qhfgva" <[EMAIL PROTECTED]> wrote:
> If my original post was not entirely clear, I'm interested in any
> method available to limit_choices_to that lets you dynamically create
> the list of available options as a function of the current model
> instance.
>
> thanks.
>
> On Jan 28, 4:28 pm, "qhfgva" <[EMAIL PROTECTED]> wrote:
>
> > I currently have a drop down for a model where I'd only like to show
> > choices that are currently "active".  That's easy enough with:
>
> > foo  = models.ForeignKey(Foo,
> > limit_choices_to={'active__exact':True})
>
> > But if someone is working with a model where the "foo" that they had
> > previously selected has been subsequently set to inactive they should
> > be able to keep that foo (they are "grandfathered" in).
>
> > In effect I'd like limit_choices_to to have a list of foo.active ==
> > True plus the current selection they have from the past if it is not
> > an active choice.
>
> > If I could make a method of Foo such as Foo.allowed_choice_for_x  and
> > then pass it an instance of the current object, then it seems like I
> > could easily generate the specific drop downlist on the fly but I'm
> > not sure how to do that (or if it's possible).
>
> > Thoughts?


--~--~-~--~~~---~--~~
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: limit_choices_to: getting access to current instance

2007-01-29 Thread qhfgva

If my original post was not entirely clear, I'm interested in any 
method available to limit_choices_to that lets you dynamically create 
the list of available options as a function of the current model 
instance.

thanks.

On Jan 28, 4:28 pm, "qhfgva" <[EMAIL PROTECTED]> wrote:
> I currently have a drop down for a model where I'd only like to show
> choices that are currently "active".  That's easy enough with:
>
> foo  = models.ForeignKey(Foo,
> limit_choices_to={'active__exact':True})
>
> But if someone is working with a model where the "foo" that they had
> previously selected has been subsequently set to inactive they should
> be able to keep that foo (they are "grandfathered" in).
>
> In effect I'd like limit_choices_to to have a list of foo.active ==
> True plus the current selection they have from the past if it is not
> an active choice.
>
> If I could make a method of Foo such as Foo.allowed_choice_for_x  and
> then pass it an instance of the current object, then it seems like I
> could easily generate the specific drop downlist on the fly but I'm
> not sure how to do that (or if it's possible).
>
> Thoughts?


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



limit_choices_to: getting access to current instance

2007-01-28 Thread qhfgva

I currently have a drop down for a model where I'd only like to show 
choices that are currently "active".  That's easy enough with:

foo  = models.ForeignKey(Foo, 
limit_choices_to={'active__exact':True})

But if someone is working with a model where the "foo" that they had 
previously selected has been subsequently set to inactive they should 
be able to keep that foo (they are "grandfathered" in).

In effect I'd like limit_choices_to to have a list of foo.active == 
True plus the current selection they have from the past if it is not 
an active choice.

If I could make a method of Foo such as Foo.allowed_choice_for_x  and 
then pass it an instance of the current object, then it seems like I 
could easily generate the specific drop downlist on the fly but I'm 
not sure how to do that (or if it's possible).

Thoughts?


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