Lets talk about a dynamic limit_choices_to again :)

10 views
Skip to first unread message

Dmitri Fedortchenko

unread,
Nov 23, 2007, 5:05:28 PM11/23/07
to Django developers
I found this ticket:
http://code.djangoproject.com/ticket/2445

It's not seen much activity for a while, but in september jkocherhans
posted some news which I found interesting. So since I needed this
functionality, I wrote a patch for it.

The patch is still shaky on edit_inline objects, but it works for
normal models. I'm working on getting it to work on edit_inlines too
(somehow hehe).


Basically the idea is that you can define:
def choices_for__FIELD(self):
return ModelClass.objects.filter(relation_name=self.relation)

This function will be called when it is time to display or otherwise
fetch the choices for this particular row.
The patch also allows you to use normal QuerySets with the choices=
attribute for a any field (so you don't need to use the function if
the choices are more or less static).

Basically this eliminates the need for limit_choices_to alltogether
and allows for much more advanced functionality for the admin and
model-based forms.

The nice part is that the patch is perhaps 10 lines of code, and does
not break any existing model definitions.

William Waites

unread,
Nov 25, 2007, 1:04:25 PM11/25/07
to django-d...@googlegroups.com
On Fri, Nov 23, 2007 at 02:05:28PM -0800, Dmitri Fedortchenko wrote:
>
> I found this ticket:
> http://code.djangoproject.com/ticket/2445
>
> It's not seen much activity for a while, but in september jkocherhans
> posted some news which I found interesting. So since I needed this
> functionality, I wrote a patch for it.
>
> Basically the idea is that you can define:
> def choices_for__FIELD(self):
> return ModelClass.objects.filter(relation_name=self.relation)

This looks like quite a clean way to accomplish dynamic choices. I've
needed things like this as well and often have resorted to rather less
elegant hacks.

Any chance of getting this or something similar pulled into the trunk?

Cheers,
-w

Dmitri Fedortchenko

unread,
Nov 25, 2007, 3:29:36 PM11/25/07
to django-d...@googlegroups.com
I totally agree, I think it was a great idea and I am glad to help in implementing it!

William: if you don't have edit_inline objects that need this functionality, you should try my patch. I'd love to hear some feedback! :)

It makes it super easy to do something that no admin can be without: IE the ability to limit selections based on the currently open object. Like if you are editing a user from a certain country, you should only be able to assign a city to that user which is bound to the user's country etc. I've always missed that functionality in django, and now it's here with a minimum of fuss.

However... I've spent the last few days trying to get this working together with edit_inline.
Unfortunately, that code is quite complicated. I could even call it a mess, but it might just be my own incompetence.

The model code is deeply intertwined with oldforms code and the line between them is often very blurry. I am guessing there is work going on in that area. I think I am close to getting it working with edit_inline, it may involve a hack or two, but hopefully it can be done.

I'll post more once I have something. If anyone who is more familiar with edit_inline code has some suggestions, please chime in :)

//Dmitri
Reply all
Reply to author
Forward
0 new messages