On Nov 7, 9:02 am, "Alex Koshelev" <[EMAIL PROTECTED]> wrote:
> For example you can use `thread locals` to store there request object

Really, don't. As James has posted here many times, this was only ever
at best a horrible hack, and is now completely unnecessary.

In response to the OP, I'm not sure what the code is doing in the
CategoryField's clean() method. It doesn't seem to be the right place
to be saving changes to objects - for instance, what if you category
field is valid, but other fields on the form fail validation?

Instead of doing it there, I would do it in the Form's save() method.
To get access to the user, override the __init__ method of your form
to take a request parameter, and cache this as self.request. Something
like (untested):

class LinkForm(forms.ModelForm):
    class Meta:
        model = Link
        exclude = ('user', 'url')

    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(LinkForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        self.cleaned_data['category'] =
Category.objects.get_or_create(
            category=self.cleaned_data['category'],
            user=request.user
        )
        obj = super(LinkForm, self).save(*args, **kwargs)

--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to