On Fri, Apr 3, 2009 at 2:22 PM, mjlissner <mjliss...@gmail.com> wrote:

>
> I've been trying for the past week or so to make a rather simple view
> work the way I think it should, and I keep failing miserably.
>
> What I'd like to do is to extend the user model with an app by
> allowing a user to supply one piece of information about themselves,
> their twitter user name. I could do this with the AUTH_PROFILE_MODULE
> setting, but I want to extend the user model with other apps in the
> future, and not to use the normal way here.
>
> So, it seems to me that I need to use user as a foreignkey, and that I
> need to add a piece of information about them to the database.
>
> So far, I've come up with a view that looks like this:
>
>
> @login_required
> def index(request):
>    # If they did a post, then submit the data & redirect the user to /
> modules
>    if request.method == 'POST':
>
>        form = twitterForm(request.POST)
>        if form.is_valid():
>            #Then we save the data to the database
>            form = form.cleaned_data
>
>            current_user = get_object_or_404(request.user,
> pk=request.user.id)
>
>            t = TwitterModel(
>                user= current_user,
>                twitter_username= form['twitter_username'],
>            )
>
>            t.save()
>
>            return HttpResponseRedirect('/modules/')
>
>
>
>    # If they did not do a post, show them the form.
>    else:
>        form = twitterForm()
>
>    return render_to_response('twitter/index.html', {'form': form})
>
>
> That's pretty basic. The problem is that it tries to do an insert
> EVERY time it is completed, which isn't what I intended. If the insert
> has already been done, it needs to do an update, and vice versa.
>
> Can somebody make sense of where I went wrong here? I'm new to django,
> and this is giving me a huge headache (metaphorically, that is).
> >
>
2 things.  One the get_object_or_404 isn't necessary since request.user is
already the user object, there's no need to query for it again.  Second the
reason you always get an INSERT is that you are constructing a new object,
perhaps you should take a look at get_or_create:
http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-kwargs

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

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

Reply via email to