Re: User based objects
*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) ** * On Fri, Jul 8, 2011 at 8:36 PM, Shawn Milochikwrote: > On Fri, Jul 8, 2011 at 10:28 AM, Praveen Krishna R > 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. > > -- > 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 > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- Thanks and Regards, *Praveen Krishna R* -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: User based objects
On Fri, Jul 8, 2011 at 10:28 AM, Praveen Krishna Rwrote: > 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. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: User based objects
*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 Milochikwrote: > 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-users@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. > > -- Thanks and Regards, *Praveen Krishna R* -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: User based objects
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-users@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.
User based objects
*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* -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.