Hi all,

I've been using django for some time now and I must say that it's my favorite framework. But lately I've run into a problem. Let's say I have the following models:

class Contact(models.Model):
       id = models.AutoField(primary_key=True)
       user = models.ForeignKey(User, editable=False)
name = models.CharField(max_length=255, default='', null=True, blank=True)
       groups = models.ManyToManyField('Group', null=True, blank=True)

       class Meta:
               unique_together = ("user", "name")

       def __unicode__(self):
               return u'%s' % (self.name)

class Group(models.Model):
       id = models.AutoField(primary_key=True)
       user = models.ForeignKey(User, editable=False)
       name = models.CharField(max_length=255, null=False, blank=False)

       class Meta:
               unique_together = ("user", "name")

       def __unicode__(self):
               return u'%s' % (self.name)

I'm using ModelForm to create the forms used to insert new instances of both these models into my database. But, as the models made clear, I want to have the contacts and the groups distinct to each user in django. This part is working. Now, when I'm going to add a new contact, for example, I want to set the initial value of the field "groups", which is the ManytoMany field, to be only the groups belonging to that user. I've searched the mail list and saw that I had to pass a list of group id's to be the initial value of the field:

       group_ids = []
       for group in Group.objects.filter(user=request.user):
               group_ids.append(group.id)

   And then, create the form:

form = ContactForm(request.POST, request.FILES, initial={'groups': group_ids})

When I do this, the form will render ok, but, instead of only showing the groups belonging to that user, it will still show all the groups in the model(belonging to other users too), the only difference being that, the groups that belong to the request.user, showing already selected, and the other groups(the ones that shouldn't be showing) not selected. I don't know if this is possible without having to override the queryset that the modelform make. Any ideas?

Thanks in advance,

--
Giancarlo Razzolini
http://lock.razzolini.adm.br
Linux User 172199
Red Hat Certified Engineer no:804006389722501
Verify:https://www.redhat.com/certification/rhce/current/
Moleque Sem Conteudo Numero #002
OpenBSD 4.5
Ubuntu 9.04 Jaunty Jackalope
4386 2A6F FFD4 4D5F 5842  6EA0 7ABE BBAB 9C0E 6B85

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

Reply via email to