Re: How do templates automatically convert dates to local timezone?

2016-08-04 Thread João Sampaio
You should probably look in the code of models.DateTimeField,
models.DateField and models.TimeField.

On Wed, Aug 3, 2016 at 6:06 PM, Robert Rollins 
wrote:

> Perhaps I wasn't clear. I know *what* Django does with timezone-aware
> dates. I just want to know *how* it does the conversion from the UTC-stored
> datetime in the DB to the localtime-displayed datetime in the templates.
> Which function(s) does the template system call to do the conversion?
>
> On Wednesday, August 3, 2016 at 1:49:45 PM UTC-7, Constantine Covtushenko
> wrote:
>>
>> Hi Robert,
>>
>> You have touched a very simple but always confusing questions - storing
>> dates and showing them to users in different places.
>>
>> As I know Django uses that in very straightforward way.
>> It always converts dates to UTC.
>> Flow that converts them back to different users in different time-zones
>> is adjusted through settings.
>>
>> Please check settings for your testing environment.
>> More information you can find on that `i18` documentation page
>> .
>>
>> Regards,
>> I hope that helps.
>>
>>
>> On Wed, Aug 3, 2016 at 11:36 PM, Robert Rollins 
>> wrote:
>>
>>> I'm writing tests that assert dates are being properly rendered on a
>>> certain page, but the tests are failing because the date value on the model
>>> object is in UTC, and the date is rendered in local time on the template.
>>> I'd like to run that date value through the same mechanism by which the
>>> template converts it to local time, so that my test does the same thing as
>>> the template.
>>>
>>> I've looked around, and unfortunately been unable to find the code that
>>> does the automatic conversion. The "date" template filter doesn't do it,
>>> and I'm too ignorant of the nitty gritty details of django template system
>>> to know what else I should be looking for.
>>>
>>> Any help would be appreciated.
>>>
>>> --
>>> 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 https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/b4f5a0d6-8aea-47a9-969e-45c7a407a104%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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/540c4257-7c82-434a-98a6-ab0fed40478f%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO_YKa2BKL-Gq9fgsyH%3Dm%3DPJ7mt9WT90Ucun9TotN2BT2LmUdg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


QuerySet for related models

2016-08-04 Thread M Hashmi


I know that count only can be applied for an IntegerField or FloatField but 
I've created a ForeignKey to another model which contains hitcounts. Now I 
need to filter results by max count from related model and I couldn't dot 
it.


models.py:


class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=20)
active = models.BooleanField(default=True)
categories = models.ManyToManyField('Category', blank=True)
default = models.ForeignKey('Category', related_name='default_category', 
null=True, blank=True)
hits = models.ForeignKey(HitCount)

objects = ProductManager()

class Meta:
ordering = ["-title", '-hits']


The idea is to access Hitcount field hits to count max value and sort
 list by max count. Below is Hitcount model installed as third party 
reusable app djagno-hitcount.

 HitCount(models.Model):
hits = models.PositiveIntegerField(default=0)
modified = models.DateTimeField(auto_now=True)
content_type = models.ForeignKey(ContentType, 
related_name="content_type_set_for_%(class)s", on_delete=models.CASCADE)
object_pk = models.PositiveIntegerField('object ID')
content_object = GenericForeignKey('content_type', 'object_pk')

objects = HitCountManager()

class Meta:
ordering = ('-hits',)
get_latest_by = "modified"
verbose_name = _("hit count")
verbose_name_plural = _("hit counts")
unique_together = ("content_type", "object_pk")
db_table = "hitcount_hit_count"


I need to call products in multiple apps with different list and in 
my mainpage views I am trying to use filter option like below:

from django.db.models import Max, Sum, Count
product1 = Product.objects.all().annotate(hits=Count('-hits__hits'))[:6]

or

product1 = Product.objects.all().order_by('-hits__hits')[:6]

and with few different filter options. I couldn't make it work so advise 
would be appreciated. 

-- 
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/39e610bf-0558-4a8d-8491-f9324d8986f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread Constantine Covtushenko
Hi M Hashmi,

As I see your model scheme built with meaning that 1 record of HitCount
relates to many records of Product.
And in your queries you try to select all products that relate to 5 topmost
HitCounts.

Am I correct?

Will be waiting for your response.


On Thu, Aug 4, 2016 at 6:41 PM, M Hashmi  wrote:

> I know that count only can be applied for an IntegerField or FloatField
> but I've created a ForeignKey to another model which contains hitcounts.
> Now I need to filter results by max count from related model and I couldn't
> dot it.
>
>
> models.py:
>
>
> class Product(models.Model):
> title = models.CharField(max_length=120)
> description = models.TextField(blank=True, null=True)
> price = models.DecimalField(decimal_places=2, max_digits=20)
> active = models.BooleanField(default=True)
> categories = models.ManyToManyField('Category', blank=True)
> default = models.ForeignKey('Category', related_name='default_category', 
> null=True, blank=True)
> hits = models.ForeignKey(HitCount)
>
> objects = ProductManager()
>
> class Meta:
> ordering = ["-title", '-hits']
>
>
> The idea is to access Hitcount field hits to count max value and sort
>  list by max count. Below is Hitcount model installed as third party
> reusable app djagno-hitcount.
>
>  HitCount(models.Model):
> hits = models.PositiveIntegerField(default=0)
> modified = models.DateTimeField(auto_now=True)
> content_type = models.ForeignKey(ContentType, 
> related_name="content_type_set_for_%(class)s", on_delete=models.CASCADE)
> object_pk = models.PositiveIntegerField('object ID')
> content_object = GenericForeignKey('content_type', 'object_pk')
>
> objects = HitCountManager()
>
> class Meta:
> ordering = ('-hits',)
> get_latest_by = "modified"
> verbose_name = _("hit count")
> verbose_name_plural = _("hit counts")
> unique_together = ("content_type", "object_pk")
> db_table = "hitcount_hit_count"
>
>
> I need to call products in multiple apps with different list and in
> my mainpage views I am trying to use filter option like below:
>
> from django.db.models import Max, Sum, Count
> product1 = Product.objects.all().annotate(hits=Count('-hits__hits'))[:6]
>
> or
>
> product1 = Product.objects.all().order_by('-hits__hits')[:6]
>
> and with few different filter options. I couldn't make it work so advise
> would be appreciated.
>
> --
> 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/39e610bf-0558-4a8d-8491-f9324d8986f1%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK52boXyqDJMFndLHALhrXnpV5%3Dj-mH8xiX45XJkw0F7HR9Srg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread M Hashmi
My field hits=models.ForeignKey(Hitcount) means that my Product model has a 
related  model with multiple fields and all the products in Product model 
will have one or more hit records. Instance of Product model will save a 
hit from end user by session/ip/user etc.

-- 
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/81557c06-29ac-44d0-b76c-b3fea37c83f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread Todor Velichkov

>
> My field hits=models.ForeignKey(Hitcount) means that my Product model has 
> a related  model with multiple fields and all the products in Product model 
> will have one or more hit records. Instance of Product model will save a 
> hit from end user by session/ip/user etc.

Honestly, I don't understand that. 

The HitCount class already has a GenericForeignKey, maybe you are looking 
for a Reverse Generic Relation 

 
class in order to get the Hits for a product.

On Thursday, August 4, 2016 at 10:42:58 PM UTC+3, M Hashmi wrote:
>
> My field hits=models.ForeignKey(Hitcount) means that my Product model has 
> a related  model with multiple fields and all the products in Product model 
> will have one or more hit records. Instance of Product model will save a 
> hit from end user by session/ip/user etc.
>

-- 
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/a681e317-a71e-41fb-93e7-eaf411a15729%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread Constantine Covtushenko
Please check my notes below:

On Thu, Aug 4, 2016 at 10:42 PM, M Hashmi  wrote:

> My field hits=models.ForeignKey(Hitcount) means that my Product model has
> a related  model with multiple fields and all the products in Product model
> will have one or more hit records.
>
Sorry but I do not see how Product model will have many hit records.
May be you should change it on:

hits = models.ManyToMany(Hitcount)



> Instance of Product model will save a hit from end user by session/ip/user
> etc.
>
> --
> 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/81557c06-29ac-44d0-b76c-b3fea37c83f2%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK52boXSLH1E9CA0cAF6gJp0LowWs3Znj%2BQXEC12_6HFXkwedQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


WSGI question

2016-08-04 Thread Jonty Needham
Sorry if this isn't for here, but I've somehow got the wrong version of
python running with mod_wsgi --2.7 instead of 3.4, so none of the 3.4
libraries work, like collections or datetime.

Where do I look to change this?

-- 
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/CANOQRbxgUWQsX88fOeprnYUK9xb%2B5BB_rprbMfD%2BBOdM4D3dqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: WSGI question

2016-08-04 Thread John
Hi Jonty,

You can only have one version of mod_wsgi installed per apache instance,
so you need to uninstall the 2.7 version and install the 3.4 version. On
Ubuntu, the libapache2-mod-wsgi-py3 package is what you are looking to
have installed.

John

On 05/08/16 00:37, Jonty Needham wrote:
> Sorry if this isn't for here, but I've somehow got the wrong version
> of python running with mod_wsgi --2.7 instead of 3.4, so none of the
> 3.4 libraries work, like collections or datetime.
>
> Where do I look to change this?
> -- 
> 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/CANOQRbxgUWQsX88fOeprnYUK9xb%2B5BB_rprbMfD%2BBOdM4D3dqA%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 
https://groups.google.com/d/msgid/django-users/e5806164-36dc-36f8-b300-426c6beabbb8%40martinhome.org.uk.
For more options, visit https://groups.google.com/d/optout.


Re: parallel test running throwing pickleError

2016-08-04 Thread Jonas Trappenberg
You can debug by adding code like this to django/test/runner.py just after 
it determined the args (around line 315 in django 1.9):

import pickle
for arg in args:
print(arg)
pickle.dumps(arg)

I found that pickle raises the same exceptions as cPickle, but, since it's 
all Python, lets you set breakpoints around the exception to find out 
what's going on.

The ModuleImportFailure for example comes from python's system-wide 
unittest/loader.py from a function called _make_failed_import_test where 
it's dynamically created and hence can't be pickled. Setting a breakpoint 
inside that method around the error message should give you the required 
pointers to fix the import and thus the pickle exception.

For anyone else coming here and debugging pickle exceptions: another 
failure I ran into was from calling unittest.TestCase.addCleanup() with an 
instancemethod, e.g. something like self.addCleanup(self.remove_things). 
Making self.remove_things a global function fixed this too.

Hope this helps!


On Monday, February 29, 2016 at 12:13:08 PM UTC-5, girish ramnani wrote:
>
> when running the django test in parallel using the runner.py . 
> _pickle.PicklingError: Can't pickle  'unittest.loader.ModuleImportFailure'>: attribute lookup 
> ModuleImportFailure on unittest.loader failed
>
> error is thrown, and when the runner is run using a single process. the 
> test suite executes.
>
> I am using a new virtualenv with django installed in editable mode.
>

-- 
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/3989ea02-ce27-4283-b24f-b121d364afea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread M Hashmi
Thanks Todor as this make senseall I need is to get the Hitcount's hits 
presented as local variable to Product model so in queryset I can set 
order_by. I will try this and will post results.

On Thursday, August 4, 2016 at 2:07:53 PM UTC-7, Todor Velichkov wrote:
>
> My field hits=models.ForeignKey(Hitcount) means that my Product model has 
>> a related  model with multiple fields and all the products in Product model 
>> will have one or more hit records. Instance of Product model will save a 
>> hit from end user by session/ip/user etc.
>
> Honestly, I don't understand that. 
>
> The HitCount class already has a GenericForeignKey, maybe you are looking 
> for a Reverse Generic Relation 
> 
>  
> class in order to get the Hits for a product.
>
> On Thursday, August 4, 2016 at 10:42:58 PM UTC+3, M Hashmi wrote:
>>
>> My field hits=models.ForeignKey(Hitcount) means that my Product model has 
>> a related  model with multiple fields and all the products in Product model 
>> will have one or more hit records. Instance of Product model will save a 
>> hit from end user by session/ip/user etc.
>>
>

-- 
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/79f9bb72-363d-40c2-848d-3b3dc9ca9bcd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread M Hashmi
Thank you Constantine,

I got this mistake fixed and this was really bad mistake from my side. Now 
with your proposed scenario I am trying to get it work with 
"trending_products = Product.objects.all().order_by('hits__hits')[:6]" but 
still it isn't giving me sort by max hits as I used annotate method with Max
as well. Also did count for hits. Any suggestions on this now.

Thank you for your response.

On Thursday, August 4, 2016 at 2:10:48 PM UTC-7, Constantine Covtushenko 
wrote:
>
> Please check my notes below:
>
> On Thu, Aug 4, 2016 at 10:42 PM, M Hashmi  > wrote:
>
>> My field hits=models.ForeignKey(Hitcount) means that my Product model has 
>> a related  model with multiple fields and all the products in Product model 
>> will have one or more hit records. 
>>
> Sorry but I do not see how Product model will have many hit records.
> May be you should change it on:
>
> hits = models.ManyToMany(Hitcount)
>
>  
>
>> Instance of Product model will save a hit from end user by 
>> session/ip/user etc.
>>
>> -- 
>> 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 https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/81557c06-29ac-44d0-b76c-b3fea37c83f2%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/57953851-750f-4592-8fe3-515bae3ab03f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread M Hashmi
Hello Todor,

I followed your directions and 
used https://docs.djangoproject.com/ja/1.9/ref/contrib/contenttypes/ for 
reference but I got stuck at error 'GenericForeignKey' object has not 
attribute 'get_lookup'. I tried 
Product.objects.aggregate(Count('hits'))[:6]. In my models.py I got 
following code:

hits = GenericRelation(HitCount, content_type_field='content_object', 
object_id_field='object_pk',)

class Meta:
ordering = ["-title"]
Removed the -hits from ordering section and rest of the code is same. I 
searched it on google but it showed some Django based bugs.
Any suggestions?
Thanks,



On Thursday, August 4, 2016 at 2:07:53 PM UTC-7, Todor Velichkov wrote:
>
> My field hits=models.ForeignKey(Hitcount) means that my Product model has 
>> a related  model with multiple fields and all the products in Product model 
>> will have one or more hit records. Instance of Product model will save a 
>> hit from end user by session/ip/user etc.
>
> Honestly, I don't understand that. 
>
> The HitCount class already has a GenericForeignKey, maybe you are looking 
> for a Reverse Generic Relation 
> 
>  
> class in order to get the Hits for a product.
>
> On Thursday, August 4, 2016 at 10:42:58 PM UTC+3, M Hashmi wrote:
>>
>> My field hits=models.ForeignKey(Hitcount) means that my Product model has 
>> a related  model with multiple fields and all the products in Product model 
>> will have one or more hit records. Instance of Product model will save a 
>> hit from end user by session/ip/user etc.
>>
>

-- 
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/2a7be23a-c227-460d-89f2-d7ffd52d4123%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread Constantine Covtushenko
Hi M Hashmi,

I believe that you are looking a way to use aggregation on related model.
You should try the following snitppet:
`trending_products = Product.objects.aggregate(hit=Max('hits__hits'))[:6]`
(see more info here
)

This will give you list of products where each product will have 'hit' is
the MAX value of all hits set for this product.

Also I do not see any reason to use 'ContentType' relation from your
HitCount model at all.
If your origin intent was to have hits with any Model in your
system(project) then you should built 'joins with aggregation' queries
based on Reverse Generic Relation as said Todor. But in this case Product
hits field becomes unnecessary as it duplicates relations.

On Fri, Aug 5, 2016 at 5:59 AM, M Hashmi  wrote:

> Hello Todor,
>
> I followed your directions and used
> https://docs.djangoproject.com/ja/1.9/ref/contrib/contenttypes/ for
> reference but I got stuck at error 'GenericForeignKey' object has not
> attribute 'get_lookup'. I tried
> Product.objects.aggregate(Count('hits'))[:6]. In my models.py I got
> following code:
>
> hits = GenericRelation(HitCount, content_type_field='content_object',
> object_id_field='object_pk',)
>
> class Meta:
> ordering = ["-title"]
> Removed the -hits from ordering section and rest of the code is same. I
> searched it on google but it showed some Django based bugs.
> Any suggestions?
> Thanks,
>
>
>
> On Thursday, August 4, 2016 at 2:07:53 PM UTC-7, Todor Velichkov wrote:
>>
>> My field hits=models.ForeignKey(Hitcount) means that my Product model has
>>> a related  model with multiple fields and all the products in Product model
>>> will have one or more hit records. Instance of Product model will save a
>>> hit from end user by session/ip/user etc.
>>
>> Honestly, I don't understand that.
>>
>> The HitCount class already has a GenericForeignKey, maybe you are
>> looking for a Reverse Generic Relation
>> 
>> class in order to get the Hits for a product.
>>
>> On Thursday, August 4, 2016 at 10:42:58 PM UTC+3, M Hashmi wrote:
>>>
>>> My field hits=models.ForeignKey(Hitcount) means that my Product model
>>> has a related  model with multiple fields and all the products in Product
>>> model will have one or more hit records. Instance of Product model will
>>> save a hit from end user by session/ip/user etc.
>>>
>> --
> 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/2a7be23a-c227-460d-89f2-d7ffd52d4123%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK52boXEMAx6ihuMaBDE_we3UQ7sPuizhe3WeGqaTsJSXyLKpg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: QuerySet for related models

2016-08-04 Thread M Hashmi
Thanks for reply and I tried this before same exact code but I got stuck at 
"unhashable type" error.
I searched it thoroughly but then I gave up on it to have some guidance 
from kind people like you.

If you believe this is how it should work then please provide me some quick 
ref for the error. I also changed it like:
trending_products = list(Product.objects.aggregate(hit=Max('hits__hits')))
It didn't work.

Thanks again.



On Thursday, August 4, 2016 at 10:44:02 PM UTC-7, Constantine Covtushenko 
wrote:
>
> Hi M Hashmi,
>
> I believe that you are looking a way to use aggregation on related model.
> You should try the following snitppet:
> `trending_products = Product.objects.aggregate(hit=Max('hits__hits'))[:6]` 
> (see more info here 
> )
>
> This will give you list of products where each product will have 'hit' is 
> the MAX value of all hits set for this product.
>
> Also I do not see any reason to use 'ContentType' relation from your 
> HitCount model at all.
> If your origin intent was to have hits with any Model in your 
> system(project) then you should built 'joins with aggregation' queries 
> based on Reverse Generic Relation as said Todor. But in this case Product 
> hits field becomes unnecessary as it duplicates relations.
>
> On Fri, Aug 5, 2016 at 5:59 AM, M Hashmi 
> > wrote:
>
>> Hello Todor,
>>
>> I followed your directions and used 
>> https://docs.djangoproject.com/ja/1.9/ref/contrib/contenttypes/ for 
>> reference but I got stuck at error 'GenericForeignKey' object has not 
>> attribute 'get_lookup'. I tried 
>> Product.objects.aggregate(Count('hits'))[:6]. In my models.py I got 
>> following code:
>>
>> hits = GenericRelation(HitCount, content_type_field='content_object', 
>> object_id_field='object_pk',)
>>
>> class Meta:
>> ordering = ["-title"]
>> Removed the -hits from ordering section and rest of the code is same. I 
>> searched it on google but it showed some Django based bugs.
>> Any suggestions?
>> Thanks,
>>
>>
>>
>> On Thursday, August 4, 2016 at 2:07:53 PM UTC-7, Todor Velichkov wrote:
>>>
>>> My field hits=models.ForeignKey(Hitcount) means that my Product model 
 has a related  model with multiple fields and all the products in Product 
 model will have one or more hit records. Instance of Product model will 
 save a hit from end user by session/ip/user etc.
>>>
>>> Honestly, I don't understand that. 
>>>
>>> The HitCount class already has a GenericForeignKey, maybe you are 
>>> looking for a Reverse Generic Relation 
>>> 
>>>  
>>> class in order to get the Hits for a product.
>>>
>>> On Thursday, August 4, 2016 at 10:42:58 PM UTC+3, M Hashmi wrote:

 My field hits=models.ForeignKey(Hitcount) means that my Product model 
 has a related  model with multiple fields and all the products in Product 
 model will have one or more hit records. Instance of Product model will 
 save a hit from end user by session/ip/user etc.

>>> -- 
>> 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 https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/2a7be23a-c227-460d-89f2-d7ffd52d4123%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8896d210-1e8f-40d5-8580-cc5b15db51d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.