On 28 ., 05:56, "Jeremy Dunck" <jdu... gmail.com> wrote:
> On 12/27/06, Vasily Sulatskov <redvas... gmail.com> wrote:
>
>
>
>
>
> > I have models similar to following:
>
> > class User(models.Model):
> > name = models.CharField(maxlength=100)
>
> > class Keyword(models.Model):
> > name = models.CharField(maxlength=100)
> > user = models.ForeignKey(User)
>
> > class Article(models.Model):
> > name = models.CharField(maxlength=100)
> > user = models.ForeignKey(User)
> > keywords = model.ManyToManyField(Keyword,
> > filter_interface=models.HORIZONTAL)
>
> > Admin works but displays on Article edit page
Keywords of all users,
> > while I want it to display Keywords only of the
same User only.This isn't exactly what you asked for, but
perhaps it'll help:http://code.djangoproject.com/wiki/CookBookPredicates
Thanks for the reply but that didn't help.
I find a workaround for this problem, imho it's even clunky
enough to
be called kludge
class RelatedManyToManyField(models.ManyToManyField):
def __init__(self, *args, **kwds):
self.related_set_name = kwds.pop('related_set_name')
self.__manipulator = None
models.ManyToManyField.__init__(self, *args, **kwds)
def get_manipulator_fields(self, opts, manipulator,
change,
name_prefix='', rel=False, follow=True):
self.__manipulator = manipulator
return super(RelatedManyToManyField,
self).get_manipulator_fields(
opts, manipulator, change, name_prefix, rel,
follow)
def get_choices_default(self):
if hasattr(self.__manipulator, 'original_object'):
profile =
self.__manipulator.original_object.user_profile
objects = list(getattr(profile,
self.related_set_name).all())
choices = [(obj.id, str(obj)) for obj in
objects]
else:
# Fallback for AddManipulator case
# Possible solution
#choices = models.Field.get_choices(self,
include_blank=False)
# I dont't want any choices until object is
fully created
choices = []
self.__manipulator = None
return choices
I examinded calls django makes during form creation and find
out that
call to get_manipulator_fields() is the last call which have
information about manipulator it's related to and therefore
can access
it's original_object. So I overrided
get_manipulator_fields() to store
manipulator and used that information in
get_choices_default().
It works for me.
Is there any better solution?
Or perhaps it's possible to improve this solution to the
point it will
make way to mainline Django?
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|