On Tue, 2006-10-03 at 01:45 +0000, seemant wrote:
> Hi,
> 
> I'm sorry about that.  All the ManyToManyFields have this sorta thing:
> 
>     LEM = models.ManyToManyField (
>             Person,
>             related_name = 'LEM',
>             filter_interface = models.HORIZONTAL,
>             limit_choices_to = { 'role__pk': Role.objects.get(name =
> 'LEM').id},
>     )

Aah. This is important information; now the problem is clear.

For a start, this will behave a little differently from what you might
expect. The thing is, the query you have here is going to be evaluated
at *import* time, not when the limit_choices_to attribute is needed.
This is problematic for any longer running process, because if the Role
table is updates, the limit_choices_to attribute is not.

It is also why you have problems at the syncdb phase: merely in order to
import this model, Python needs to be able to query the Role model (the
queryset is being evaluated here because you are accessing the "id"
attribute). So if Role does not yet exist, you are going to be out of
luck. This isn't a Django problem, it's the way Python scoping works:
attribute values are evaluated at compile time. Django only gets to play
after the code has been imported (and hence compiled).

In order to have a limit_choices_to that is only evaluated at runtime,
which is probably closer to what you want, you need to do something
similar to models.LazyDate() in Django -- which is the example given in
the docs for the limit_choices_to attribute. That is, you need to create
an object that at the time it is needed to be used in a query, it will
return the list you want. This means, making the __str__ method compute
the value when it is called. See django/db/models/__init__.py (the
LazyDate class) for an example.

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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to