"django.db.models.query.QuerySet" vs "django.db.models.QuerySet"

2015-09-29 Thread Ilya Kazakevich
In Django 1.6 it QuerySet was in package "django.db.models.query.QuerySet". 
But since 1.7 it also may be imported with "django.db.models.QuerySet".

Is "django.db.models.query.QuerySet" deprecated since 1.7? What is the 
"official" way to import QuerySet? I can't find any documentation regard 
this subject.

-- 
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/6ebbd9ab-b0e8-486f-aa33-88005a6aa256%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
>>  
>> <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 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 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.


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: best way change template base my app

2015-05-15 Thread Ilya Kazakevich
You also may use widgets instead of "extends": 
http://sniplates.readthedocs.org/en/latest/


On Friday, May 15, 2015 at 6:50:40 AM UTC+3, sacrac wrote:
>
> Thank Andre Luis :)
>
> On Wed, May 13, 2015 at 1:41 PM, André Luiz  > wrote:
>
>> extends should always be on first line of file and it accepts a variable 
>> as argument so you can define which template should be used on your view.
>>
>> # views.py
>> def home(request):
>> if request.session.type == 1:
>>  base = 'base.html'
>> else:
>> base = 'base_other.html'
>>
>> context = {
>> 'base': base,
>> }
>>
>> return render(request, 'template.html', context)
>>
>> # template.html
>> {% extends base %}
>>
>> if you need it on every template maybe you should use a context processor 
>> to define your base variable.
>>
>> 2015-05-13 15:11 GMT-03:00 carlos >:
>>
>>> Hi, is posible change my base template with if tag
>>> example:
>>>
>>> {% if request.session.type = '1' %}
>>> {% extends "base.html"%}
>>> {% else %}
>>>{% extends "base_other.html"%}
>>> {%endif%}
>>>
>>> i have a error like that
>>>
>>> TemplateSyntaxError at /monito/cult/
>>>
>>> Invalid block tag: 'endif'
>>>
>>>
>>> will to do change the base template my app
>>>
>>> Cheers
>>>
>>> -- 
>>> 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/CAM-7rO3DONtDe_d%2BHeBef446Mr%3DvSiKK7z_W8pyNyPq7Pbt3PA%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...@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/CAL6LtmfZTMpfG%3Djh_uMtp63hZ%2BDsmz92_8UP81x0zuGfqp%2BO0w%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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fd0c960d-98d6-44aa-9fd4-8b722cc43c00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: What is the ideal web server to use with Django?

2015-05-15 Thread Ilya Kazakevich
Hi.

I believe the best installation is Apache + wsgi and nginx for static. 

In some scenarios Apache performance may be your bottleneck, but:
1) there are a lot of ways to tune it. Read about "Multi-Processing 
Modules" for example.
2) 99% of web applications do have different bottlenecks, not the web 
server.
3) really huge installations should use clusters hence should be scalable 
horizontally. In such scenarios web server performance is not an issue 
(unless you work for Google or Facebook) :)


On Wednesday, May 13, 2015 at 4:15:18 AM UTC+3, akshat wrote:
>
> I am new to Django. I am building a app which will have to handle several 
> concurrent requests. Which web server is suitable for this? I have read 
> that Apache's performance degrades on high load.
>

-- 
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/eed0c413-9cf6-4ceb-91ef-95a85c42ec3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


forcing user model to return first_name last_name if exists

2015-04-28 Thread Ilya Kazakevich
Hello,

I have many places in my app where user (from user model) is displayed: 
templates, forms, fliters, tables etc.
It is displayed as username everywhere.

I want it to be displayed as first_name/last_name.

I can do that with monkey patching:

@receiver(request_started)
def patch(*args, **kwargs):
get_user_model().__str__ = my_smart_str_func

I use "request_started" to make sure all models are loaded. (BTW, does 
there is something like "models loaded" signal?).

How ever, I feel it a little bit hacky. There should be more pythonic way 
to do that. Am I miss something?

-- 
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/b483b4a8-ee4a-4cdb-ad81-769fabf4b0ec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Form to create several objects with one-to-one relation

2015-04-23 Thread Ilya Kazakevich
Well, I found solution.

``CreateWithInlinesView `` works perfectly with ``OneToOneField`` (after 
all, 1-to-1 is just a foreign key with constraint), but my main model here 
is ``Comment``, not ``Task``. So I should set ``Comment`` as ``model`` 
field in this view and ``Task`` as ``inline``. It looks silly. I will 
create custom form or review my model structure.


On Wednesday, April 22, 2015 at 3:38:30 PM UTC+3, Ilya Kazakevich wrote:
>
> Thank you, that may work, but I feel that I reinventing wheel here.
>
> Actually, there are inline_formset  and CreateWithInlinesView  (from 
> django extras) and they do exactly what I want, but they only support 
> ForeignKey, not OneToOne. Looks like I need to extend them to support 
> OneToOne.
>
>
> On Wednesday, April 22, 2015 at 2:40:56 AM UTC+3, Vijay Khemlani wrote:
>>
>> What about and old-school DetailView?
>>
>> def get_context_data -> Creates the two forms and puts them in the context
>>
>> def post -> Validates and process the forms
>>
>>
>>
>> On Tue, Apr 21, 2015 at 7:41 PM, Ilya Kazakevich  
>> wrote:
>>
>>> Hello,
>>>
>>> I have several models with one-to-one relation. For example
>>>
>>> class Task(models.Model):
>>> initial_comment = models.OneToOneField('Comment')
>>># A pack of other fields
>>>
>>> class Comment(models.Model)
>>> body = RichTextField()
>>># A pack of other fields
>>>
>>>
>>> I want to create "create view" based on form, that gives user ability to 
>>> create task and initial comment there.
>>>
>>> 1) I can't use CreateView because it is based on only one model
>>> 2) I can't use ModelForm because it is based on only one model
>>> 3) I can create several forms, but I can't join them into one formset 
>>> (forms are different)
>>> 4) I feel "inlineformset_factory" (InlineFormSet) should be used here, 
>>> but I am not sure it suits best. Is there any 3rd party Django app to do 
>>> that?
>>>
>>> Sure I can create form myself, but I do not want to copy/paste all 
>>> fields, their types, localized labels, validations and so on. I just want 
>>> to list their names (like "fields" attibute).
>>> If you wonder why do I need one-to-one: Comments are used heavily in 
>>> other places and have different relations with different models.
>>>
>>> Thank you.
>>>
>>> Ilya.
>>>
>>>  -- 
>>> 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/e5254d86-c237-4192-bf1e-4e2d96722a9f%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/e5254d86-c237-4192-bf1e-4e2d96722a9f%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 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/4558d41f-cb8f-47de-8482-ad4fbab05def%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Best practice for passing JSON objects to a template

2015-04-22 Thread Ilya Kazakevich
What about putting it into ?

On Thursday, April 9, 2015 at 8:50:50 PM UTC+3, Eric Plumb wrote:
>
> Hi Djangoers!
>
> Sometimes in the course of human events it becomes necessary to encode a 
> JSON object directly into a template.  We all prefer AJAX and REST APIs and 
> the rest of the TOFLAs, but in the cases where it has to be part of the 
> template, I'm wondering if there's an accepted canonical best-practice way 
> to do so and remain safe from XSS attacks and other nastiness.
>
> I'm aware of the following two methods:
>
> 1. HTML attribute loaded by jQuery's $.data()
>
> # view
> return { ... {'my_obj': mark_safe(escape(json.dumps(obj))) } ... }
>
> # template
> 
...
> > # JS > var myObj = $('div').data('my-object'); // implicitly calls JSON.parse() > on the encoded object > > 2. Explicitly parsed JS object > > # view > return { ... {'my_obj': mark_safe(escapejs(json.dumps(obj))) } ... } > > # template >