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" <[email protected] 
> <javascript:>> 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 <select> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> 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
>>  
>> <https://groups.google.com/d/msgid/django-users/5bdd87b6-9cc6-4490-8b85-d25ecfe8ed16%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> 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 [email protected].
To post to this group, send email to [email protected].
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.

Reply via email to