Re: FORM: How to specify current user as value for a form ForeignKey referencing logged on user

2015-03-09 Thread felix

El 08/03/15 14:40, Ryan Alexander escribió:

Hey there,

This seems like something that should be simple to do but I'm having a 
heck of a time with it and google searches don't reference anything 
that's helping me out.


I have a form that a user fills out, and it creates a db row based on 
the input.  Model as below:


class Accounts(models.Model):
authid = models.CharField(max_length=128)
authtoken = models.CharField(max_length=128)
provider = models.ForeignKey(Provider)
user = models.ForeignKey(User, unique=True)


How do I feed the current user value to the form?  Form code:

class AccountForm(forms.ModelForm):

  provider = forms.ModelChoiceField(queryset=Provider.objects.all(), 
help_text="Provider")

  authid = forms.CharField(max_length=256, help_text="AuthID")

  authtoken = forms.CharField(max_length=256, help_text="AuthToken")

   user = 

If you are using a class based view like CreateView you can set the 
current user in the form_valid method like this:


def form_valid(self, form):
form.instance.user = self.request.user

Furthermore you don't need to define a modelform when using CreateView.


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/54FDC3BF.3080403%40epepm.cupet.cu.
For more options, visit https://groups.google.com/d/optout.


Re: FORM: How to specify current user as value for a form ForeignKey referencing logged on user

2015-03-08 Thread Vijay Khemlani
If you are using a modelform then you don't need to re-define the original
model fields, in this case

Class AccountForm(forms.ModelForm):
class Meta:
model = Accounts
fields = ['authid', 'authtoken', 'provider']

You leave the "user" field out as it is not part of the form itself

Then in your view you can do something like this:

form = AccountForm(self.request.POST)
if form.is_valid():
instance = form.instance
instance.user = self.request.user
instance.save()

On Sun, Mar 8, 2015 at 3:40 PM, Ryan Alexander 
wrote:

> Hey there,
>
> This seems like something that should be simple to do but I'm having a
> heck of a time with it and google searches don't reference anything that's
> helping me out.
>
> I have a form that a user fills out, and it creates a db row based on the
> input.  Model as below:
>
> class Accounts(models.Model):
> authid = models.CharField(max_length=128)
> authtoken = models.CharField(max_length=128)
> provider = models.ForeignKey(Provider)
> user = models.ForeignKey(User, unique=True)
>
>
> How do I feed the current user value to the form?  Form code:
>
> class AccountForm(forms.ModelForm):
>
>  provider = forms.ModelChoiceField(queryset=Provider.objects.all(), 
> help_text="Provider")
>
>  authid = forms.CharField(max_length=256, help_text="AuthID")
>
>  authtoken = forms.CharField(max_length=256, help_text="AuthToken")
>
>   user = 
>
>
> Many thanks in advance,
>
> Ryan
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/03432bf4-e20b-4e64-a5a6-a7781fe94f07%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei0TTSy04Tahx-%2B86Lf0%3D0VJXdSpBpSQGaC-4K%2B-JE_5Vg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.