Rob Hudson 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

Everything you have setup looks ok, i do it exactly like this to get the
users in a field so it's not exactly clear to me what's wrong.
I suppose you have enabled the AuthenticationMiddleware in the settings
as well as added django.contrib.auth & django.contrib.sessions to the apps ?
Also did you do a syncdb and check the Link table to verify that it has
a user field?

Regards,
Benedict


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