Re: About using Django Auth with my app, Auto saving the User

2011-03-24 Thread Amanjeev Sethi
Thanks a lot.

-- 
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: About using Django Auth with my app, Auto saving the User

2011-03-21 Thread Lior Sion
If instead of printing "not valid" you'll print the form itself with
the errors (as described here for example:
http://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template)
you would see what the errors are. Alternatively, read about form
validation (http://docs.djangoproject.com/en/dev/ref/forms/
validation/) and try to understand why it's failing..

On Mar 20, 8:05 pm, AJ  wrote:
> No, the form submits, with other values too, to the view but it just
> does not validate.
>
> AJ

-- 
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: About using Django Auth with my app, Auto saving the User

2011-03-20 Thread AJ
No, the form submits, with other values too, to the view but it just
does not validate.

AJ

-- 
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: About using Django Auth with my app, Auto saving the User

2011-03-20 Thread AJ
My form is not valid and I cannot figure out why. I am using Django's
ModelForm to generate a form (As above). I printout out the
request.POST and all it has is

"csrfmiddlewaretokenname"

Just putting it here.

-- 
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: About using Django Auth with my app, Auto saving the User

2011-03-19 Thread AJ
OK. I believe that the form is invalid.

 "if post_form.is_valid():"

actually fails and it prints "not working".

I am using the default form that Django creates from

class PostForm(ModelForm):
class Meta:
model = Post

Is it not good?

On Mar 18, 10:10 pm, AJ  wrote:
> I have tried both solutions but could not achieve the results. here is
> the actual code that I started working with. This is the simplest
> version that I did to test this feature.
>
> 
> Model--
> class Post (models.Model):
>     name = models.CharField(max_length=1000, help_text="required, name
> of the post")
>     user = models.ForeignKey(User, unique=False, help_text="user this
> post belongs to")
>
>         def save(self, *args, **kwargs):
>                 curruser = kwargs.pop('user')
>                 self.user = curruser
>                 super(Post, self).save(*args, **kwargs)
>
>         def __unicode__(self):
>                 return self.name
>
> class PostForm(ModelForm):
>         class Meta:
>                 model = Post
> ./
> Model--
>
> View--
>
> @login_required
> def create_post (request):
>         if request.method == 'POST':
>                 post_form = PostForm(request.POST)
>                 if post_form.is_valid():
>                         post = post_form.save(commit = False)
>                         post.user = request.user
>                         post.save()
>                         return render_to_response('home.html')
>                 else:
>                         return HttpResponse('not working')
>         else:
>                 post_form = PostForm()
>                 return render_to_response('create.html', 
> {'post_form':post_form })
>
> ./
> 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.



Re: About using Django Auth with my app, Auto saving the User

2011-03-18 Thread AJ
I have tried both solutions but could not achieve the results. here is
the actual code that I started working with. This is the simplest
version that I did to test this feature.


Model--
class Post (models.Model):
name = models.CharField(max_length=1000, help_text="required, name
of the post")
user = models.ForeignKey(User, unique=False, help_text="user this
post belongs to")

def save(self, *args, **kwargs):
curruser = kwargs.pop('user')
self.user = curruser
super(Post, self).save(*args, **kwargs)

def __unicode__(self):
return self.name

class PostForm(ModelForm):
class Meta:
model = Post
./
Model--

View--

@login_required
def create_post (request):
if request.method == 'POST':
post_form = PostForm(request.POST)
if post_form.is_valid():
post = post_form.save(commit = False)
post.user = request.user
post.save()
return render_to_response('home.html')
else:
return HttpResponse('not working')
else:
post_form = PostForm()
return render_to_response('create.html', {'post_form':post_form 
})

./
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.



Re: About using Django Auth with my app, Auto saving the User

2011-03-17 Thread Shawn Milochik
Here's an alternative way to do it.

Something like (in view):

post.save(user = request.user)

And in your save override:

user = kwargs.pop('user')
self.user = user
super(Post, self).save(*args, **kwargs)

The reason you pop off the 'user' value instead of just reading it is
that the parent object would otherwise fail due to an unexpected
keyword argument.

Of course, this now requires you to write all your code that could
ever save an instance of a Post model to pass a user, otherwise you'll
get an exception. I just mention that because you have the field
defined as required. You could either get rid of the requirement or
ensure you always pass 'user.'

Shawn

-- 
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: About using Django Auth with my app, Auto saving the User

2011-03-17 Thread AJ
My apologies, I guess that was a rude thing to write but believe me,
my intention was not to be rude at all. I know that no one here owes
me an answer and I respect that folks still help. Please accept my
apologies if I came off as a rude person in my last message.

Thanks a lot for your help.

--AJ


On Mar 17, 10:39 am, Daniel Roseman  wrote:
> On Thursday, March 17, 2011 2:33:50 PM UTC, AJ wrote:
>
> > No one?
>
> > On Mar 16, 10:05 pm, AJ  wrote:
> > > I have a model like this:
>
> > > class Post (models.Model):
> > >     name = models.CharField(max_length=1000, help_text="required, name
> > > of the post")
> > >     description = models.TextField(blank=True)
> > >     custom_hashed_url = models.CharField(unique=True, max_length=1000,
> > > editable=False)
>
> > > def save(self, *args, **kwargs):
> > >         #How to save User here?
> > >         super(Model, self).save()
>
> > > View code:
>
> > > if not errors:
> > >     f = PostForm(request.POST)
> > >     f.save()
>
> > > There is an old post that I followed but could not do it. And also
> > > this post is old.
> >http://www.b-list.org/weblog/2006/nov/02/django-tips-auto-populated-f...
>
> > > Even though I have the user field as FK, I get this error: 'Cannot
> > > assign None: "MyModel.user" does not allow null values.'
>
> > > This essentially means (IMHO) that my View does not send the User
> > > along. How can I auto populate the user field with Django User, the
> > > currently logged in user.
>
> Don't be rude - it's only a few hours since your initial query. Perhaps all
> those who knew the answer were busy, or haven't looked at the list since you
> posted.
>
> Anyway, the answer is that you don't do this in the model. You do ti in the
> view:
>
>     form = PostForm(request.POST)
>     if form.is_valid():
>         post = form.save(commit=False)
>         post.user = request.user
>         post.save()
>
> Of course, your posted model code doesn't actually include a FK to User - I
> assume you have one, because otherwise you wouldn't have got that error. It
> would have been helpful to post the actual code.
> --
> DR.

-- 
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: About using Django Auth with my app, Auto saving the User

2011-03-17 Thread Daniel Roseman
On Thursday, March 17, 2011 2:33:50 PM UTC, AJ wrote:
>
> No one? 
>
> On Mar 16, 10:05 pm, AJ  wrote: 
> > I have a model like this: 
> > 
> > class Post (models.Model): 
> > name = models.CharField(max_length=1000, help_text="required, name 
> > of the post") 
> > description = models.TextField(blank=True) 
> > custom_hashed_url = models.CharField(unique=True, max_length=1000, 
> > editable=False) 
> > 
> > def save(self, *args, **kwargs): 
> > #How to save User here? 
> > super(Model, self).save() 
> > 
> > View code: 
> > 
> > if not errors: 
> > f = PostForm(request.POST) 
> > f.save() 
> > 
> > There is an old post that I followed but could not do it. And also 
> > this post is old.
> http://www.b-list.org/weblog/2006/nov/02/django-tips-auto-populated-f... 
> > 
> > Even though I have the user field as FK, I get this error: 'Cannot 
> > assign None: "MyModel.user" does not allow null values.' 
> > 
> > This essentially means (IMHO) that my View does not send the User 
> > along. How can I auto populate the user field with Django User, the 
> > currently logged in user.


Don't be rude - it's only a few hours since your initial query. Perhaps all 
those who knew the answer were busy, or haven't looked at the list since you 
posted.

Anyway, the answer is that you don't do this in the model. You do ti in the 
view:

form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()

Of course, your posted model code doesn't actually include a FK to User - I 
assume you have one, because otherwise you wouldn't have got that error. It 
would have been helpful to post the actual code. 
--
DR.

-- 
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: About using Django Auth with my app, Auto saving the User

2011-03-17 Thread AJ
No one?

On Mar 16, 10:05 pm, AJ  wrote:
> I have a model like this:
>
> class Post (models.Model):
>     name = models.CharField(max_length=1000, help_text="required, name
> of the post")
>     description = models.TextField(blank=True)
>     custom_hashed_url = models.CharField(unique=True, max_length=1000,
> editable=False)
>
> def save(self, *args, **kwargs):
>         #How to save User here?
>         super(Model, self).save()
>
> View code:
>
> if not errors:
>     f = PostForm(request.POST)
>     f.save()
>
> There is an old post that I followed but could not do it. And also
> this post is 
> old.http://www.b-list.org/weblog/2006/nov/02/django-tips-auto-populated-f...
>
> Even though I have the user field as FK, I get this error: 'Cannot
> assign None: "MyModel.user" does not allow null values.'
>
> This essentially means (IMHO) that my View does not send the User
> along. How can I auto populate the user field with Django User, the
> currently logged in user.

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



About using Django Auth with my app, Auto saving the User

2011-03-16 Thread AJ
I have a model like this:

class Post (models.Model):
name = models.CharField(max_length=1000, help_text="required, name
of the post")
description = models.TextField(blank=True)
custom_hashed_url = models.CharField(unique=True, max_length=1000,
editable=False)

def save(self, *args, **kwargs):
#How to save User here?
super(Model, self).save()

View code:

if not errors:
f = PostForm(request.POST)
f.save()

There is an old post that I followed but could not do it. And also
this post is old. 
http://www.b-list.org/weblog/2006/nov/02/django-tips-auto-populated-fields/

Even though I have the user field as FK, I get this error: 'Cannot
assign None: "MyModel.user" does not allow null values.'

This essentially means (IMHO) that my View does not send the User
along. How can I auto populate the user field with Django User, the
currently logged in user.

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