For example you can use `thread locals` to store there request object

On Fri, Nov 7, 2008 at 10:58, Rob Hudson <[EMAIL PROTECTED]> wrote:

>
> I have the following:
>
> ## MODELS
>
> class Category(models.Model):
>    user = models.ForeignKey(User)
>    name = models.CharField(max_length=200)
>
> class Link(models.Model):
>    user = models.ForeignKey(User)
>    category = models.ForeignKey(Category)
>    name = models.CharField(max_length=255)
>    url = models.URLField(max_length=255)
>
> ## FORMS
>
> class CategoryField(forms.Field):
>    def clean(self, value):
>        if not value:
>            raise forms.ValidationError('This field is required.')
>        try:
>            category = Category.objects.get(user=???, name=value)
>        except Category.DoesNotExist:
>            category = Category(user=???, name=value)
>            category.save()
>        return category
>
> class LinkForm(ModelForm):
>    category = CategoryField()
>    class Meta:
>        model = Link
>        exclude = ('user', 'url')
>
> ## VIEWS
>
> def create(request):
>    if request.method == 'POST':
>        form = LinkForm(request.POST)
>        if form.is_valid():
>            link = form.save(commit=False)
>            link.user = request.user
>            link.save()
>            return HttpResponseRedirect(reverse('link_list'))
>    else:
>        form = LinkForm()
>
>    return render_to_response(
>        'links/link_form.html', {
>            'form': form,
>        },
>        context_instance=RequestContext(request)
>    )
>
> What I don't see is a way to get the user up into my field the way I
> have things set up.  I originally didn't have a user FK on the
> Category model but decided I wanted it so that when adding new Links,
> the Categories can be filtered by user so each user sees only their
> categories.
>
> I'm stuck here.  Any help is much appreciated.
>
> Thanks,
> Rob
> >
>

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