Re: Building correct queryset

2017-05-26 Thread Горобец Дмитрий
Score field is on Brand model.

пятница, 26 мая 2017 г., 3:19:56 UTC+5 пользователь Melvyn Sopacua написал:
>
> On Monday 22 May 2017 15:26:59 Todor Velichkov wrote:
>
> > Hello, Дмитрий,
>
> > you can try this one, but w/o further optimizations it may be a very
>
> > slow query.
>
> > 
>
> > qs = Product.objects.filter(
>
> > #Where score is greater or equal
>
> > #to the 4th max score from its group
>
> > score__gte=Subquery(
>
> > (Product.objects
>
> > .filter(brand=OuterRef('brand'))
>
> > .values('score')
>
> > .order_by('-score')[3:4]
>
> > )
>
> > )
>
> > ).order_by('-score')
>
>  
>
> Yeah, that's how I read it too. But the code says score is on Brand model, 
> not Product. Which is correct?
>
> -- 
>
> Melvyn Sopacua
>

-- 
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/5838210b-f4ea-4338-ab2c-7c6e706acae2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Building correct queryset

2017-05-24 Thread Горобец Дмитрий
Thank you, Todor. I'll try.

вторник, 23 мая 2017 г., 3:27:00 UTC+5 пользователь Todor Velichkov написал:
>
> Hello, Дмитрий,
> you can try this one, but w/o further optimizations it may be a very slow 
> query.
>
> qs = Product.objects.filter(
> #Where score is greater or equal
> #to the 4th max score from its group
> score__gte=Subquery(
> (Product.objects
> .filter(brand=OuterRef('brand'))
> .values('score')
> .order_by('-score')[3:4]
> )
> )
> ).order_by('-score')
>
>
>
>
>  
>
> On Friday, May 19, 2017 at 12:32:49 PM UTC+3, Горобец Дмитрий wrote:
>>
>> Hello. I have these two models. I have to show 4 or less products on a page 
>> for each brand ordered by score. Please, help construct correct queryset.
>>
>>
>>
>> class Brand(models.Model):
>> title = models.CharField(
>> verbose_name='Название бренда',
>> max_length=64,
>> unique=True,
>> db_index=True,
>> error_messages={
>> 'unique': 'Бренд с таким именем уже существует.'
>> }
>> )
>>
>> slug = AutoSlugField(
>> verbose_name='Адрес страницы бренда',
>> populate_from='title',
>> editable=True,
>> unique=True,
>> slugify=custom_slugify,
>> db_index=True
>> )
>>
>> owner = models.OneToOneField(
>> to=settings.AUTH_USER_MODEL,
>> verbose_name='Владелец',
>> on_delete=models.PROTECT,
>> )
>>
>>
>> score = models.DecimalField(
>> verbose_name='Рейтинг бренда',
>> blank=True,
>> null=True,
>> max_digits=19,
>> decimal_places=18,
>> )
>>
>>
>>
>> class Product(models.Model):
>> title = models.CharField(
>> verbose_name='Название изделия',
>> max_length=255,
>> db_index=True,
>> )
>>
>> slug = AutoSlugField(
>> verbose_name='Адрес страницы изделия',
>> populate_from='title',
>> editable=False,
>> unique_with='brand',
>> slugify=custom_slugify,
>> db_index=True,
>> )
>>
>> brand = models.ForeignKey(
>> to=Brand,
>> verbose_name='Бренд',
>> related_name='products',
>> on_delete=models.CASCADE,
>> )
>>
>>
>>

-- 
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/adfc3475-6bd9-43a2-9891-ed23d67d3b19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Building correct queryset

2017-05-21 Thread Горобец Дмитрий
Andrew, your solution works great for one brand. But I have more than one.

суббота, 20 мая 2017 г., 0:20:18 UTC+5 пользователь Andrew Beales написал:
>
> brand = Brand.objects.get(title='mybrand')
> products = Product.objects.filter(brand=brand).order_by('-score')[:4]
>
> ...gets you the top 4 products in order for the brand
>
> On Friday, May 19, 2017 at 10:32:49 AM UTC+1, Горобец Дмитрий wrote:
>>
>> Hello. I have these two models. I have to show 4 or less products on a page 
>> for each brand ordered by score. Please, help construct correct queryset.
>>
>>
>>
>> class Brand(models.Model):
>> title = models.CharField(
>> verbose_name='Название бренда',
>> max_length=64,
>> unique=True,
>> db_index=True,
>> error_messages={
>> 'unique': 'Бренд с таким именем уже существует.'
>> }
>> )
>>
>> slug = AutoSlugField(
>> verbose_name='Адрес страницы бренда',
>> populate_from='title',
>> editable=True,
>> unique=True,
>> slugify=custom_slugify,
>> db_index=True
>> )
>>
>> owner = models.OneToOneField(
>> to=settings.AUTH_USER_MODEL,
>> verbose_name='Владелец',
>> on_delete=models.PROTECT,
>> )
>>
>>
>> score = models.DecimalField(
>> verbose_name='Рейтинг бренда',
>> blank=True,
>> null=True,
>> max_digits=19,
>> decimal_places=18,
>> )
>>
>>
>>
>> class Product(models.Model):
>> title = models.CharField(
>> verbose_name='Название изделия',
>> max_length=255,
>> db_index=True,
>> )
>>
>> slug = AutoSlugField(
>> verbose_name='Адрес страницы изделия',
>> populate_from='title',
>> editable=False,
>> unique_with='brand',
>> slugify=custom_slugify,
>> db_index=True,
>> )
>>
>> brand = models.ForeignKey(
>> to=Brand,
>> verbose_name='Бренд',
>> related_name='products',
>> on_delete=models.CASCADE,
>> )
>>
>>
>>

-- 
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/8d486975-7f0d-40e1-82e3-64bc5259b518%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Building correct queryset

2017-05-19 Thread Горобец Дмитрий


Hello. I have these two models. I have to show 4 or less products on a page for 
each brand ordered by score. Please, help construct correct queryset.



class Brand(models.Model):
title = models.CharField(
verbose_name='Название бренда',
max_length=64,
unique=True,
db_index=True,
error_messages={
'unique': 'Бренд с таким именем уже существует.'
}
)

slug = AutoSlugField(
verbose_name='Адрес страницы бренда',
populate_from='title',
editable=True,
unique=True,
slugify=custom_slugify,
db_index=True
)

owner = models.OneToOneField(
to=settings.AUTH_USER_MODEL,
verbose_name='Владелец',
on_delete=models.PROTECT,
)


score = models.DecimalField(
verbose_name='Рейтинг бренда',
blank=True,
null=True,
max_digits=19,
decimal_places=18,
)



class Product(models.Model):
title = models.CharField(
verbose_name='Название изделия',
max_length=255,
db_index=True,
)

slug = AutoSlugField(
verbose_name='Адрес страницы изделия',
populate_from='title',
editable=False,
unique_with='brand',
slugify=custom_slugify,
db_index=True,
)

brand = models.ForeignKey(
to=Brand,
verbose_name='Бренд',
related_name='products',
on_delete=models.CASCADE,
)


-- 
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/9ec086f9-c91f-4a8e-b474-9b1d89d1ef2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django app crashes with out of memory

2016-10-10 Thread Горобец Дмитрий
Hello. 

Thanks for advices. 

Crash occures, when I run Django command through python manage.py 
my_command_name.

It doesn't relate to nginx and even gunicorn, because, again, it's Django 
command, which I call from shell.

I switched off redis caching (I use cacheops), but it' didn't help too.

пятница, 7 октября 2016 г., 13:21:48 UTC+5 пользователь Горобец Дмитрий 
написал:
>
> Hello.
>
> I have VPS with 2Gb RAM with django, gunicorn, mysql, nginx and redis. 
>
> My app crashes with out of memory error, when I run django command on 
> model, which has approximately 7 million records. It takes each premise 
> object and updates one field by checking value with regular expression. 
> Please, help optimize that command.
>
> class Command(BaseCommand):
> help = 'Updates premise.number_order'
>
> def handle(self, *args, **options):
> for premise in Premise.objects.iterator():
> premise.number_order = premise.set_number_order()
> premise.save()
>
> self.stdout.write('Finished')
>
>
> # Method of Premise model
> def set_number_order(self):
> tr = {
> 'А': '.10',
> 'A': '.10',
> 'Б': '.20',
> 'В': '.30',
> 'Г': '.40',
> 'Д': '.50',
> 'Е': '.60',
> 'Ж': '.70',
> 'З': '.80',
> 'И': '.90',
> }
>
> only_digit = re.compile(r'^(?P[0-9]{1,9})$')
> digit_with_separator = 
> re.compile(r'^(?P[0-9]{1,9})(?P[-|/])(?P\w+)$')
> digit_with_letter = 
> re.compile(r'^(?P[0-9]{1,9})(?P[А-Яа-я]+)')
> result = 0
> title = self.title.strip().upper()
>
> if only_digit.match(title):
> number = only_digit.match(title).group('number')
> result = number + '.00'
>
> elif digit_with_separator.match(title):
> number = digit_with_separator.match(title).group('number')
> rest = digit_with_separator.match(title).group('rest')
> if rest[0].isalpha():
> floating = tr.get(rest[0], '.90')
> result = number + floating
>
> elif rest[0].isdigit():
> try:
> if rest[1].isdigit():
> result = number + '.{}'.format(rest[:2])
> else:
> result = number + '.0{}'.format(rest[0])
> except IndexError:
> result = number + '.0{}'.format(rest[0])
>
> elif digit_with_letter.match(title):
> number = digit_with_letter.match(title).group('number')
> letter = digit_with_letter.match(title).group('letter')[0]
>
> floating = tr.get(letter, '.90')
> result = number + floating
>
> return Decimal(result)
>

-- 
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/2e87323c-294c-4b82-89dd-783e36e23291%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Django app crashes with out of memory

2016-10-07 Thread Горобец Дмитрий
Hello.

I have VPS with 2Gb RAM with django, gunicorn, mysql, nginx and redis. 

My app crashes with out of memory error, when I run django command on 
model, which has approximately 7 million records. It takes each premise 
object and updates one field by checking value with regular expression. 
Please, help optimize that command.

class Command(BaseCommand):
help = 'Updates premise.number_order'

def handle(self, *args, **options):
for premise in Premise.objects.iterator():
premise.number_order = premise.set_number_order()
premise.save()

self.stdout.write('Finished')


# Method of Premise model
def set_number_order(self):
tr = {
'А': '.10',
'A': '.10',
'Б': '.20',
'В': '.30',
'Г': '.40',
'Д': '.50',
'Е': '.60',
'Ж': '.70',
'З': '.80',
'И': '.90',
}

only_digit = re.compile(r'^(?P[0-9]{1,9})$')
digit_with_separator = 
re.compile(r'^(?P[0-9]{1,9})(?P[-|/])(?P\w+)$')
digit_with_letter = 
re.compile(r'^(?P[0-9]{1,9})(?P[А-Яа-я]+)')
result = 0
title = self.title.strip().upper()

if only_digit.match(title):
number = only_digit.match(title).group('number')
result = number + '.00'

elif digit_with_separator.match(title):
number = digit_with_separator.match(title).group('number')
rest = digit_with_separator.match(title).group('rest')
if rest[0].isalpha():
floating = tr.get(rest[0], '.90')
result = number + floating

elif rest[0].isdigit():
try:
if rest[1].isdigit():
result = number + '.{}'.format(rest[:2])
else:
result = number + '.0{}'.format(rest[0])
except IndexError:
result = number + '.0{}'.format(rest[0])

elif digit_with_letter.match(title):
number = digit_with_letter.match(title).group('number')
letter = digit_with_letter.match(title).group('letter')[0]

floating = tr.get(letter, '.90')
result = number + floating

return Decimal(result)

-- 
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/6ccbf103-c24b-4e3a-982d-4e5db0f01972%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Code review and perfomance tips

2016-07-15 Thread Горобец Дмитрий
Hello, everybody.

I'm working on one project where I need to render cities on the page in 1-4 
columns like this. I'm rendering cities using 'regroup' filter:

A D   M U
AberdeenDanbury   MacombUkiah
Akron Delano   Mandan   Urbana
Manhattan

BH P W
BabylonHaledon   Pace   Waipio
BangorHanover   Walker

C   I   R
Calhoun   Indiana  Raleigh
Calipatria   Irondale Ridge
Cheval Riverdale

Main part of code looks:

def get_columns(location):
qs = Location.objects.filter(parent=location, active=True)
qs = qs.annotate(first_char=Upper(Substr('name', 1, 1)))
locations = qs.values()

first_chars = qs.distinct('first_char').order_by('first_char')
first_chars = first_chars.values_list('first_char', flat=True)

first_chars_count = len(first_chars)
num_of_columns = 4 if (first_chars_count / 4) >= 1 else first_chars_count
chars_per_column = math.ceil(first_chars_count / num_of_columns)

start = 0
stop = chars_per_column

for i in range(num_of_columns):
yield [location for location in locations 
   if location['first_char'] in first_chars[start:stop]]

start += chars_per_column
stop += chars_per_column



It can be rewritten using QuerySet filter, but this approach adds more queries 
in database:


def get_columns(location):
qs = Location.objects.filter(parent=location, active=True)
qs = qs.annotate(first_char=Upper(Substr('name', 1, 1)))

first_chars = qs.distinct('first_char').order_by('first_char')
first_chars = first_chars.values_list('first_char', flat=True)

first_chars_count = len(first_chars)
num_of_columns = 4 if (first_chars_count / 4) >= 1 else first_chars_count
chars_per_column = math.ceil(first_chars_count / num_of_columns)

start = 0
stop = chars_per_column

for i in range(num_of_columns):
yield qs.filter(first_char__in=first_chars[start:stop])

start += chars_per_column
stop += chars_per_column


Which is better? Filter using list comprehension or using queryset filter? 


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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a935f4fb-df22-4917-bed2-41e41c4c6749%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


URL design

2016-06-03 Thread Горобец Дмитрий
Hello, guys.

I have the following URL patterns in my urls.py module and I'd like to 
refactor them:

url(r'^(?P[a-zA-Z0-9_\-]+)/(?P[a-zA-Z0-9_\-]+)/$',
 views.item_list, name="item_location_category"),
url(r'^(?P[a-zA-Z0-9_\-]+)/(?P[a-zA-Z0-9_\-]+)/(?P[a-zA-Z0-9_\-]+)/$',
 views.item_list),
url(r'^(?P[a-zA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-9_\-]+)/(?P[a-zA-Z0-9_\-]+)/$',
 views.item_list)


Example:

http://mysite.com/london/cars/

http://mysite.com/london/cars/new/

http://mysite.com/london/cars/new/audi/

http://mysite.com/london/cars/new/audi/a4/

http://mysite.com/london/cars/new/audi/a4/2016/

http://mysite.com/london/cars/new/audi/a4/2016/white/ etc...


And it shouldn't be query parameter.


Does anyone have any suggestions?


Thank you.

-- 
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/123e209e-3921-4399-affc-be8b4c67c6d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Best bootstrap plugins-addons in django-bootstrap project?

2016-03-10 Thread Горобец Дмитрий
http://bootstraptour.com/

четверг, 10 марта 2016 г., 17:57:34 UTC+5 пользователь setivo...@gmail.com 
написал:
>
> I found useful bootstrap-datetimepicker, may be someone known yet?
>

-- 
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/8ef892b7-96a7-4a9a-a057-1b83f73f7296%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Declare REST clients in django project

2016-02-17 Thread Горобец Дмитрий
Hello!

How do you declare different REST clients in settings of django project?

Is it a right way to declare clients directly in settings.py?
MAILCHIMP_CLIENT = mailchimp.Mailchimp(MAILCHIMP_API_KEY)
TWILIO_CLIENT = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)


-- 
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/1a5f128a-36a2-46f1-96ea-56f915cda8a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Redefine RelatedManager

2016-01-21 Thread Горобец Дмитрий
Hello.

Is there any way to redefine a relatedmanager for one field with 
foreignkey? 

For example:

class Contract(models.Model):
   name = models.CharField(max_length=255, db_index=True)
   owner = models.ForeignKey(settings.AUTH_USER_MODEL, 
related_name='contracts')

class AppUser(AbstractUser):
account_manager = models.ForeignKey('self', null=True, blank=True, 
related_name='clients')

@property
def is_manager(self):
return self.groups.filter(name='managers').exists()

@property
def is_client(self):
return self.groups.filter(name='clients').exists()


manager = AppUser.objects.get(id=1)
manager.contracts.all()

client = AppUser.objects.get(id=2)
client.contracts.all()

When I call manager.contracts.all(), I s that associated witwant to get 
contracth this manager and his clients contracts. And when I call 
client.contracts.all(), I want to get only client's contracts. 




-- 
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/e6159779-b3d6-4a4a-8751-6b99ff9ea4f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.