Re: Some newbie questions on CBVs and FBVs

2017-11-24 Thread 'Simon Connah' via Django users
 Hi,
First of all thank you very much for the reply. It was very useful. I'll 
re-read the documentation and see what I was missing out on.
The get_queryset() method was a very useful hint. That may well solve the issue 
I was having.
Simon.On Friday, 24 November 2017, 06:20:35 GMT, Matemática A3K 
 wrote:  
 
 

On Thu, Nov 23, 2017 at 8:49 PM, 'Simon Connah' via Django users 
 wrote:

Hi everyone,
First of all sorry for the newbie questions it has been a long time since I 
used Django and I think I have forgotten just about everything I once knew.
I'm working on a user app for a website. I can't use the built in user model as 
I need a little more flexibility and the ability to grow in the future if 
required.
Basically I have three problems.
1) I need some views to be limited to the user who created the view.

Users do no create Views, what you are meaning is the objects "created" by the 
user (or objects attributed or associated to the user if you want to be even 
more precise)
 
 For instance I have an UpdateUser view and I only want the currently logged in 
user to be able to change their own user model data. So say I have the username 
"djangouser" and I go to UpdateView the only row in the database that I can 
change on the database server is the one with the username "djangouser". I'm 
not sure what the best way to handle this problem is?

Set the queryset attribute of the UpdateView to 
User.objects.filter(name=self.request.user) or override the get_queryset() 
method in the view:def get_queryset(self):
base_qs = super(YourUpdateView, self).get_queryset()
return 
base_qs.filter(user=self.request.user)https://stackoverflow.com/questions/8594759/django-updateview-restrict-per-user
https://docs.djangoproject.com/en/1.11/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_queryset


I could do it manually in a function based view without any problem but I find 
CBVs to be really hard to customise when you want to do something that they 
were not specifically designed to do.

Overriding the queryset is a common pattern in CBV :)
 
 At that point I just end up writing a FBV instead and doing it all myself as 
it is quicker than trying to figure out all the mixins and all the methods of 
the class.


I must be missing something simple with CBVs. Any help would be appreciated 
with this :).
 You should read the whole topic:
https://docs.djangoproject.com/en/1.11/topics/class-based-views/intro/ 
and then read the 
reference:https://docs.djangoproject.com/en/1.11/ref/class-based-views/mixins/


2) In FBVs you get passed a variable to the function with the URL conf 
variables, for instance: (path('/blah//', view, name='blah') would 
result in the FBV recieving a username parameter. Where are these variables 
stored when using a CBV?

You should read the topic :)
 

3) Is it considered bad practice to use FBVs in Django?

IMO, no :)
 
 I just find them so much easier and quicker to write. I know that CBVs result 
in less code duplication but I find I constantly need the Django docs open to 
see what each CBV supports and which methods it has available. Where as with 
FBVs I just write the code and everything works.


CBV is a pattern, if you don't embrace it, it may get in your way :)
 
Anyway, thank you for any help :).
Simon.

-- 
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+unsubscribe@ googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/ group/django-users.
To view this discussion on the web visit https://groups.google.com/d/ 
msgid/django-users/1609714478. 2477254.1511480996285%40mail. yahoo.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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BFDnh%2BYZfaoRPAvZCeGr3L2uz6W%3DTgcVChAwJV%3DCmhC4Y9%2BQA%40mail.gmail.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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 

Re: Some newbie questions on CBVs and FBVs

2017-11-23 Thread Matemática A3K
On Thu, Nov 23, 2017 at 8:49 PM, 'Simon Connah' via Django users <
django-users@googlegroups.com> wrote:

> Hi everyone,
>
> First of all sorry for the newbie questions it has been a long time since
> I used Django and I think I have forgotten just about everything I once
> knew.
>
> I'm working on a user app for a website. I can't use the built in user
> model as I need a little more flexibility and the ability to grow in the
> future if required.
>
> Basically I have three problems.
>
> 1) I need some views to be limited to the user who created the view.
>

Users do no create Views, what you are meaning is the objects "created" by
the user (or objects attributed or associated to the user if you want to be
even more precise)


> For instance I have an UpdateUser view and I only want the currently
> logged in user to be able to change their own user model data. So say I
> have the username "djangouser" and I go to UpdateView the only row in the
> database that I can change on the database server is the one with the
> username "djangouser". I'm not sure what the best way to handle this
> problem is?
>

Set the queryset attribute of the UpdateView to
User.objects.filter(name=self.request.user) or override the get_queryset()
method in the view:

def get_queryset(self):
base_qs = super(YourUpdateView, self).get_queryset()
return base_qs.filter(user=self.request.user)

https://stackoverflow.com/questions/8594759/django-updateview-restrict-per-user

https://docs.djangoproject.com/en/1.11/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_queryset

>
> I could do it manually in a function based view without any problem but I
> find CBVs to be really hard to customise when you want to do something that
> they were not specifically designed to do.
>

Overriding the queryset is a common pattern in CBV :)


> At that point I just end up writing a FBV instead and doing it all myself
> as it is quicker than trying to figure out all the mixins and all the
> methods of the class.
>

> I must be missing something simple with CBVs. Any help would be
> appreciated with this :).
>

You should read the whole topic:
https://docs.djangoproject.com/en/1.11/topics/class-based-views/intro/
and then read the reference:
https://docs.djangoproject.com/en/1.11/ref/class-based-views/mixins/

>
> 2) In FBVs you get passed a variable to the function with the URL conf
> variables, for instance: (path('/blah//', view, name='blah')
> would result in the FBV recieving a username parameter. Where are these
> variables stored when using a CBV?
>

You should read the topic :)


>
> 3) Is it considered bad practice to use FBVs in Django?
>

IMO, no :)


> I just find them so much easier and quicker to write. I know that CBVs
> result in less code duplication but I find I constantly need the Django
> docs open to see what each CBV supports and which methods it has available.
> Where as with FBVs I just write the code and everything works.
>
>
CBV is a pattern, if you don't embrace it, it may get in your way :)


> Anyway, thank you for any help :).
>
> Simon.
>
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/1609714478.2477254.1511480996285%40mail.yahoo.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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BFDnh%2BYZfaoRPAvZCeGr3L2uz6W%3DTgcVChAwJV%3DCmhC4Y9%2BQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Some newbie questions on CBVs and FBVs

2017-11-23 Thread 'Simon Connah' via Django users
Hi everyone,
First of all sorry for the newbie questions it has been a long time since I 
used Django and I think I have forgotten just about everything I once knew.
I'm working on a user app for a website. I can't use the built in user model as 
I need a little more flexibility and the ability to grow in the future if 
required.
Basically I have three problems.
1) I need some views to be limited to the user who created the view. For 
instance I have an UpdateUser view and I only want the currently logged in user 
to be able to change their own user model data. So say I have the username 
"djangouser" and I go to UpdateView the only row in the database that I can 
change on the database server is the one with the username "djangouser". I'm 
not sure what the best way to handle this problem is?
I could do it manually in a function based view without any problem but I find 
CBVs to be really hard to customise when you want to do something that they 
were not specifically designed to do. At that point I just end up writing a FBV 
instead and doing it all myself as it is quicker than trying to figure out all 
the mixins and all the methods of the class.
I must be missing something simple with CBVs. Any help would be appreciated 
with this :).
2) In FBVs you get passed a variable to the function with the URL conf 
variables, for instance: (path('/blah//', view, name='blah') would 
result in the FBV recieving a username parameter. Where are these variables 
stored when using a CBV?
3) Is it considered bad practice to use FBVs in Django? I just find them so 
much easier and quicker to write. I know that CBVs result in less code 
duplication but I find I constantly need the Django docs open to see what each 
CBV supports and which methods it has available. Where as with FBVs I just 
write the code and everything works.
Anyway, thank you for any help :).
Simon.

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1609714478.2477254.1511480996285%40mail.yahoo.com.
For more options, visit https://groups.google.com/d/optout.