Thanks for the answer Malcolm. Maybe I was unclear where I put the line "related_contacts = Contact.objects.filter(projects_involved__title__startswith=title)". Sorry for that. It was in the "Project" model, not in a view. When I validate the code as follows I get the described error no matter if I use starts_with or startswith.
class Contact(models.Model): projects_involved = models.ManyToManyField('Project', blank=True) class Project(models.Model): title = models.CharField(max_length=100) related_contacts = Contact.objects.filter (projects_involved__title__startswith=title) client = models.ManyToManyField(Contact, related_name='client',blank=True, limit_choices_to=related_contacts) collaborators = models.ManyToManyField (Contact,related_name='collaborators', blank=True, limit_choices_to=related_contacts) Now I already see a second problem myself. In the docs it says "limit_choices_to has no effect when used on a ManyToManyField with an intermediate table". So my choices list would have no effect if it worked. Maybe you could give me some general advice on how to approach what I had mind. Thanks - Silvan On Nov 20, 2:10 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Wed, 2008-11-19 at 13:35 -0800, Silvano wrote: > > Dear all > > > I'm completely stuck with the following problem. > > > What I'm trying to accomplish is: I have an app with the model > > "Project" and another model "Contacts". In "Contacts" I have a m2m > > field called "projects_involved" relating to the "Project" model which > > allows to select projects the contact is involved with. > > In my "Project" model I have some m2m fields like "client" and > > "collaborators" where I want to store a relation to some of the > > contacts. In the admin interface the user should be provided with > > contacts related to the actual "Project" only. > > > class Contact(models.Model): > > projects_involved = models.ManyToManyField('Project', blank=True) > > > class Project(models.Model): > > title = models.CharField(max_length=100) > > client = models.ManyToManyField(Contact, related_name='client', > > blank=True) > > collaborators = models.ManyToManyField(Contact, > > related_name='collaborators', blank=True) > > > What I tried so far is to use a filter for constructing a choices list > > within the “Project” model. > > > related_contacts = Contact.objects.filter > > (projects_involved__title__starts_with=title) > > But that isn't really what you have in your view. The lookup type in > Django is "startswith" (no underscore). If you wrote the above, it would > complain that "title" is not a joinable field. > > > This gives me the following error in > > "/django/db/models/fields/__init__.py" line 102, in __cmp__ > > return cmp(self.creation_counter, other.creation_counter) > > AttributeError: 'str' object has no attribute 'creation_counter' > > That error message is normally related to model creation, not filtering. > So what happens when you run "manage.py validate"? I suspect it will > report that same error. In which case, you've left something out of your > example code, because when I cut-and-paste exactly what you've written, > I don't see that error. > > So run "manage.py validate" and make sure the same error is reported. > Then look at your code carefully to work out what is different between > what you have and what you've pasted here (start by pasting exactly what > you have here into a new project and verify it doesn't raise an error; > then slowly build up to what you have in the case that is causing the > error). > > 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?hl=en -~----------~----~----~----~------~----~------~--~---