User based objects

9 views
Skip to first unread message

Praveen Krishna R

unread,
Jul 8, 2011, 9:20:15 AM7/8/11
to django...@googlegroups.com
Hi,

This is one of my Model which has an owner, column - user.

class MiscList(models.Model):
    name = models.CharField(max_length= 30)
    user = models.ForeignKey(User);
    count = models.IntegerField()
    createdate = models.DateTimeField(auto_now_add = True)
    def __unicode__(self):
        return self.name

In one of my forms I would like to have this list filtered by the currently logged in user.
Anyone of you have solved a scenario like this. 
Since this is not a view I am not able to pass request.user object to the form, in any way, what is the 
preferred way of doing this ?

--
Thanks and Regards,
Praveen Krishna R

Shawn Milochik

unread,
Jul 8, 2011, 9:22:39 AM7/8/11
to django...@googlegroups.com
You can certainly pass request.user to the form from your view.

Praveen Krishna R

unread,
Jul 8, 2011, 10:28:30 AM7/8/11
to django...@googlegroups.com
Thank you, Shawn, I didn't knew that! I'm trying on that way now!

On Fri, Jul 8, 2011 at 6:52 PM, Shawn Milochik <sh...@milochik.com> wrote:
You can certainly pass request.user to the form from your view.

--
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.

Shawn Milochik

unread,
Jul 8, 2011, 11:06:08 AM7/8/11
to django...@googlegroups.com
On Fri, Jul 8, 2011 at 10:28 AM, Praveen Krishna R
<rpravee...@gmail.com> wrote:
> Thank you, Shawn, I didn't knew that! I'm trying on that way now!
>

You're welcome. The one 'gotcha' is that you're going to have to
remove the user from the kwargs before you call the __init__ of the
superclass. Otherwise you'll get an 'unexpected keyword argument'
exception.

Example:


def __init__(self, *args, **kwargs):

self.user = kwargs.pop('user')

super(MyForm, self).__init__(*args, **kwargs)


You can then use self.user in your form's functions for filtering --
probably starting right in __init__ after the super() call to set
self.fields['field_name'].choices to be whatever they should be.

Praveen Krishna R

unread,
Jul 8, 2011, 2:44:01 PM7/8/11
to django...@googlegroups.com
Shawn, Once again thank you very much for your help,
here is my code, if anybody could make use of it (I have changed my actual model/form names)! 

class SampleForm(forms.Form):
    head = forms.CharField()
    body = forms.CharField(widget = forms.Textarea)
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop("user")
        super(SampleForm, self).__init__(*args, **kwargs)
        self.fields['mychoicefield'] = forms.ModelChoiceField(queryset = MySampleModel.objects.filter(user__username__iexact = self.user.username))

in the view I use
new_form = SampleForm(user = request.user)


--
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.

Reply all
Reply to author
Forward
0 new messages