Discussion:
Using limit_choices_to on a ManyToMany Field
Greg
2007-08-02 18:23:31 UTC
Permalink
I've looked everywhere for information on how to use the
'limit_choices_to' attribute...but can't seem to find the answer.

I have the following models:

class Choice(models.Model):
choice = models.ForeignKey(Collection, edit_inline=models.TABULAR,
num_in_admin=5)
size = models.ForeignKey(Size, core=True)
price = models.ForeignKey(Price, core=True)

def __str__(self,):
return str((self.size, self.price))

class Style(models.Model):
name = models.CharField(maxlength=200, core=True)
color = models.CharField(maxlength=100)
image = models.ImageField(upload_to='site_media/')
theslug = models.SlugField(prepopulate_from=('name',))
manufacturer = models.ForeignKey(Manufacturer)
sandp = models.ManyToManyField(Choice, limit_choices_to =
{'choice__id': self.collection.id})
collection = models.ForeignKey(Collection,
edit_inline=models.TABULAR, num_in_admin=6)

def __str__(self,):
return self.name

//////////////

Notice my sandp field in my Style class. We'll it contains the
limit_choices_to attribute. Everything works fine When I have:

sandp = models.ManyToManyField(Choice, limit_choices_to =
{'choice__id': 2})

It returns only the rows that contain 2 in the choice_id field

/////////////////////////

However, I need that '2' to do be dynamic. I'm not sure how to do
this. I tried 'self.collection.id' but that does not work. I need to
be able to get what ever the id is for the collection that the style
is tied to.

//////////////

I've looked at all the documentation and I can't seem to find the
answer. Any help would GREATLY be appreciated.

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-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Greg
2007-08-02 23:11:16 UTC
Permalink
Come on....I know that somebody can figure this out

sandp = models.ManyToManyField(Choice, limit_choices_to =
{'choice__id': self.collection.id})

What do I need to get the collection id? Will 'self.collection.id'
work?

I forgot to add my collection class. Here it is:

class Collection(models.Model):
name = models.CharField(maxlength=200)
collectionslug = models.SlugField(prepopulate_from=["name"])
description = models.TextField(maxlength=1000)
manufacturer = models.ForeignKey(Manufacturer)


Thanks for your help
Post by Greg
I've looked everywhere for information on how to use the
'limit_choices_to' attribute...but can't seem to find the answer.
choice = models.ForeignKey(Collection, edit_inline=models.TABULAR,
num_in_admin=5)
size = models.ForeignKey(Size, core=True)
price = models.ForeignKey(Price, core=True)
return str((self.size, self.price))
name = models.CharField(maxlength=200, core=True)
color = models.CharField(maxlength=100)
image = models.ImageField(upload_to='site_media/')
theslug = models.SlugField(prepopulate_from=('name',))
manufacturer = models.ForeignKey(Manufacturer)
sandp = models.ManyToManyField(Choice, limit_choices_to =
{'choice__id': self.collection.id})
collection = models.ForeignKey(Collection,
edit_inline=models.TABULAR, num_in_admin=6)
return self.name
//////////////
Notice my sandp field in my Style class. We'll it contains the
sandp = models.ManyToManyField(Choice, limit_choices_to =
{'choice__id': 2})
It returns only the rows that contain 2 in the choice_id field
/////////////////////////
However, I need that '2' to do be dynamic. I'm not sure how to do
this. I tried 'self.collection.id' but that does not work. I need to
be able to get what ever the id is for the collection that the style
is tied to.
//////////////
I've looked at all the documentation and I can't seem to find the
answer. Any help would GREATLY be appreciated.
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-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Russell Keith-Magee
2007-08-03 00:36:06 UTC
Permalink
Post by Greg
Come on....I know that somebody can figure this out
Greg - seriously, it's been 6 hours since your original message. Give
people a chance to _read_ your message before you complain about the
fact that nobody has answered.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...