Creating an object using Foreign Key for Users but filtered by group

16 views
Skip to first unread message

Vibhu Rishi

unread,
May 5, 2014, 9:52:58 AM5/5/14
to django...@googlegroups.com
Hi

I have Users and based on some requirements, I have created a few User Groups . e.g. I have a group of moderators.

Now, I am trying to create a form where a normal user can message the moderators.

So, I have a model to be used by the form something like :

class Message(models.Model):
     moderator = models.ForeignKey(User)
     user= models.ForeignKey(User)
     text = models.TextField(max_length=1000)
     date = models.DateField(default=datetime.now())

I want the moderator field to have keys to only those people who are in the group where name='Moderator'

I am not sure how to filter the ForeignKey in this case.


Why I want to do this --> is so that the form will have a dropdown list of moderators to choose from by the user when it is displayed. User selects the moderator, and enteres the message text - and sends it.

Regards,
Vibhu

--
Simplicity is the ultimate sophistication. - Leonardo da Vinci
Life is really simple, but we insist on making it complicated. - Confucius

Helton Alves

unread,
May 5, 2014, 10:22:24 AM5/5/14
to django...@googlegroups.com
Hi brother,

so, I don't know if understand very well.
but I think that this is answer for your problems.


vlw.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPiONwkfs%3DcTc5dMVVKdZAeRvR0vOTz6%2B03oL1FiG_%2BumJBOeg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--

Helton Alves 
Desenvolvedor web
Graduado em Sistemas de Informação - FACIMP
Cursando Metodologia do Ensino Superior - INESPO

Carlos Futino

unread,
May 5, 2014, 10:25:03 AM5/5/14
to django...@googlegroups.com
Hi,

you could change the QuerySet of the ModelChoiceField. I'd do it in the forms __init__ method. Like this:

class MessageForm(forms.ModelForm):
def __init__(self,*args,**kwargs):
super(MessageForm,self). __init__(*args,**kwargs)
self.moderator.queryset = User.objects.filter(...*Place your query here*...)

 
 class Meta:
model=Message 

Simon Charette

unread,
May 5, 2014, 11:21:50 AM5/5/14
to django...@googlegroups.com
You could use the `ForeignKey.limit_choices_to` option:

class Message(models.Model):
     moderator = models.ForeignKey(User, limit_choices_to={'groups__name': 'Moderator'})

     user= models.ForeignKey(User)
     text = models.TextField(max_length=1000)
     date = models.DateField(default=datetime.now())


Vibhu Rishi

unread,
May 6, 2014, 2:08:16 AM5/6/14
to django...@googlegroups.com
Hi Simon,

this is exactly what I was looking for ! It works perfectly.

I had gone through the documentation for limit_choices_to but I was not able to figure out how to do the groups part.

Vibhu


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages