Adding a one field to user model: trying not to create custom one)

2015-05-18 Thread Ilya Kazakevich
Hello,

I want to add just a one field to my model, and this field participates in 
user representation ( I use monkeypatch to change __str__).

Django manual tells me to create profile with one-to-one relation in this 
case. Well, it may work. But if I have  with 250 users, I face 250 
"SELECT" queries to my db (N+1). I need to tell "UserManager" somehow to 
use "select_related". But how can I do that with out of new ugly 
monkeypatching?
Does there is an official (recommended) way to do that? If no, how  can I 
use one-to-one user profile if I have "list of users" webpage with out of 
N+1? How to add one field to user model not extending it?

I am really unhappy with idea of using custom user model.
Thanks.


-- 
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/5bdd87b6-9cc6-4490-8b85-d25ecfe8ed16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a one field to user model: trying not to create custom one)

2015-05-18 Thread Carl Meyer
Hello Ilya,

On 05/18/2015 04:58 PM, Ilya Kazakevich wrote:
> I want to add just a one field to my model, and this field participates
> in user representation ( I use monkeypatch to change __str__).
> 
> Django manual tells me to create profile with one-to-one relation in
> this case. Well, it may work. But if I have  with 250 users, I
> face 250 "SELECT" queries to my db (N+1). I need to tell "UserManager"
> somehow to use "select_related". But how can I do that with out of new
> ugly monkeypatching?
> Does there is an official (recommended) way to do that? If no, how  can
> I use one-to-one user profile if I have "list of users" webpage with out
> of N+1? How to add one field to user model not extending it?

I am not aware of a good solution to this problem other than manually
adding the .select_related() to your query on the list-of-users page.

> I am really unhappy with idea of using custom user model.

Why?

If it's because this is an existing project and the prospect of
migrating your existing data to a custom user model is daunting, I
totally understand that. It's doable, but hard.

If this is a new project, I think that there is no good reason to avoid
a custom user model. Every new project should always use a custom user
model, even if to begin with it just inherits from AbstractUser and
doesn't change or add anything. This way in the future you have control
over your user model and can modify it as needed without monkeypatching.

Carl

-- 
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/555A72A0.2070105%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: OpenPGP digital signature


Re: Adding a one field to user model: trying not to create custom one)

2015-05-18 Thread James Schneider
The point of using a OneToOne relation to create a 'profile' is that the
profile is meant to contain information that is only accessed on an
individual basis (such as displaying a single users' address, etc.), and
generally not involved on bulk queries as you describe.

If your __str__() method in your primary user model accesses an attribute
via a foreign key lookup (ie OneToOne), then you'll get the behavior you
are describing. You want the data that you'll need all or most of the time
in the primary model to avoid that situation. In this case, I would suggest
you look at the attribute you're referencing and move it directly into the
primary user model.

I'm a little worried about what you mean by 'monkey patching'. Did you just
override the __str__() methods on your primary model?

-James
On May 18, 2015 3:58 PM, "Ilya Kazakevich"  wrote:

> Hello,
>
> I want to add just a one field to my model, and this field participates in
> user representation ( I use monkeypatch to change __str__).
>
> Django manual tells me to create profile with one-to-one relation in this
> case. Well, it may work. But if I have  with 250 users, I face 250
> "SELECT" queries to my db (N+1). I need to tell "UserManager" somehow to
> use "select_related". But how can I do that with out of new ugly
> monkeypatching?
> Does there is an official (recommended) way to do that? If no, how  can I
> use one-to-one user profile if I have "list of users" webpage with out of
> N+1? How to add one field to user model not extending it?
>
> I am really unhappy with idea of using custom user model.
> Thanks.
>
>
>  --
> 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/5bdd87b6-9cc6-4490-8b85-d25ecfe8ed16%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/CA%2Be%2BciVa3Fqte4584jOfT99vQE4CfprbyrBnwEL%2BwuxapGRcew%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a one field to user model: trying not to create custom one)

2015-05-19 Thread Ilya Kazakevich
Hello.


> I am not aware of a good solution to this problem other than manually 
> adding the .select_related() to your query on the list-of-users page. 
>
Oh :((
 

>
> > I am really unhappy with idea of using custom user model. 
>
> Why? 
>
> If it's because this is an existing project and the prospect of 
> migrating your existing data to a custom user model is daunting, I 
> totally understand that. It's doable, but hard. 
>
Yes, this project already deployed and has some data. Sure I can solve 
this, but I feel that changing user model will make my app less reusable. 

But I will probably stay with new model. There is something wrong with 
Django in this area. There should be some easy and elegant way to add one 
field to auth_user. I wonder why Django developers do not care about it.

 

-- 
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/ee9e0497-f939-47b9-87cc-df1ee70f1373%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a one field to user model: trying not to create custom one)

2015-05-19 Thread Ilya Kazakevich
Hello.

On Tuesday, May 19, 2015 at 2:26:10 AM UTC+3, James Schneider wrote:
>
> The point of using a OneToOne relation to create a 'profile' is that the 
> profile is meant to contain information that is only accessed on an 
> individual basis (such as displaying a single users' address, etc.), and 
> generally not involved on bulk queries as you describe.
>
So, "profile" idea is useless here.
 

> If your __str__() method in your primary user model accesses an attribute 
> via a foreign key lookup (ie OneToOne), then you'll get the behavior you 
> are describing. You want the data that you'll need all or most of the time 
> in the primary model to avoid that situation. In this case, I would suggest 
> you look at the attribute you're referencing and move it directly into the 
> primary user model.
>
> I'm a little worried about what you mean by 'monkey patching'. Did you 
> just override the __str__() methods on your primary model?
>
Yes) I have app with hacky code in its AppConfig#ready() method.
This code does the following

user_model = get_user_model()
try:
fields = settings.FIELDS
user_model.__str__ = UserPresenter(fields, user_model.__str__).as_func()
user_model._meta.ordering = fields
except AttributeError:
pass  # No setting, do not touch user
 
UserPresenter class just checks for passed fields and returns string if 
they exist. 

So, I only need to add this app to INSTALLED_APPS and set 
FIELDS=["last_name", "first_name"] in my settings.py and it works.

I have the same for "get_absolute_url" method as well.

It is not very pythonic way to do something, but it works. 

 

> -James
> On May 18, 2015 3:58 PM, "Ilya Kazakevich"  > wrote:
>
>> Hello,
>>
>> I want to add just a one field to my model, and this field participates 
>> in user representation ( I use monkeypatch to change __str__).
>>
>> Django manual tells me to create profile with one-to-one relation in this 
>> case. Well, it may work. But if I have  with 250 users, I face 250 
>> "SELECT" queries to my db (N+1). I need to tell "UserManager" somehow to 
>> use "select_related". But how can I do that with out of new ugly 
>> monkeypatching?
>> Does there is an official (recommended) way to do that? If no, how  can I 
>> use one-to-one user profile if I have "list of users" webpage with out of 
>> N+1? How to add one field to user model not extending it?
>>
>> I am really unhappy with idea of using custom user model.
>> Thanks.
>>
>>
>>  -- 
>> 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...@googlegroups.com .
>> To post to this group, send email to django...@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/5bdd87b6-9cc6-4490-8b85-d25ecfe8ed16%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/5d3c50e9-fc1c-4bf8-be01-2ad9dc1a0ee3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a one field to user model: trying not to create custom one)

2015-05-19 Thread Carl Meyer
On Tuesday, May 19, 2015 at 8:18:24 AM UTC-6, Ilya Kazakevich wrote:
>
> > I am really unhappy with idea of using custom user model. 
>>
>> Why? 
>>
>> If it's because this is an existing project and the prospect of 
>> migrating your existing data to a custom user model is daunting, I 
>> totally understand that. It's doable, but hard. 
>>
> Yes, this project already deployed and has some data. Sure I can solve 
> this, but I feel that changing user model will make my app less reusable. 
>

I don't think "will make my app less reusable" is a valid reason to avoid a 
custom User model. A lot of thought went into the custom User model system 
to ensure that it preserves reusability. That's why things like 
`django.contrib.auth.get_user_model()` and friends exist.
 

> But I will probably stay with new model. There is something wrong with 
> Django in this area. There should be some easy and elegant way to add one 
> field to auth_user. I wonder why Django developers do not care about it.
>

Django developers do care about the problem, and the preferred solution 
(after many years of discussion) is the custom User model. I believe that 
it's a better solution than introducing a hacky way to monkeypatch fields 
onto a built-in model. I think every Django project should use a custom 
User model.

Carl

-- 
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/d2f5e133-47a4-4255-a016-03d4b8a14d5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.