Re: django model method

2023-02-13 Thread Jason
there's alot to not like about the implementation here.  You're effectively 
hiding queries and db hits with those methods, since the naming does not 
give any indication that its a db hit.  So its really easy to forget that, 
and then after a while, you're wondering why your project is so slow and 
executing alot of unnecessary queries

for seen, likes and hotness counts, I'd rather denormalize  and have a 
count field for all those values.  Then either use a post-save signal to 
update the values, or use a recurring background task to regenerate the 
fields on an interval.

On Friday, February 10, 2023 at 9:02:16 AM UTC-5 Gabriel Araya Garcia wrote:

> Allways there is a way to improve the code, but  if that not run, then try 
> with CYTHON
>
> Gabriel Araya Garcia
> GMI - Desarrollo de Sistemas Informáticos
>
>
>
>
> El jue, 9 feb 2023 a las 10:09, Andréas Kühne () 
> escribió:
>
>> Not if you want it to be fast. Python itself is slow - so you should try 
>> to offload most of the computation on the database. So filtering would need 
>> to be done on database fields as well.
>>
>> Otherwise you would need to load all of the rows into memory and then do 
>> the filtering there.
>>
>> If you want to continue along the route you have taken now - you probably 
>> could solve it with annotated queries - something like this:
>>
>> https://stackoverflow.com/questions/1396264/how-to-sort-by-annotated-count-in-a-related-model-in-django
>>
>> Regards,
>>
>> Andréas
>>
>>
>> Den tors 9 feb. 2023 kl 13:21 skrev Chelsea Fan :
>>
>>> understood, is there any way to filter objects by method value?
>>>
>>> On Thu, Feb 9, 2023 at 4:42 PM Andréas Kühne  
>>> wrote:
>>>
 No.

 Ordering works by using the database to order the models. It therefore 
 needs to be a database field - otherwise the database can't order by the 
 field?

 Regards,

 Andréas


 Den tors 9 feb. 2023 kl 12:09 skrev Chelsea Fan >>> >:

> hello guys, Is it possible to use model method value to ordering model 
> objects in meta class?
>
> class Post(models.Model):
> title = models.CharField(max_length=255, verbose_name="ady")
> text = RichTextField(verbose_name="text")
> tagList = models.ManyToManyField(Tag, verbose_name="taglar", 
> related_query_name="tagList")
> image = models.ImageField(upload_to="postImage/", verbose_name=
> "surat")
> seen = models.ManyToManyField(UserId,verbose_name="görülen sany", 
> blank=True, related_name="gorulen")
> like = models.ManyToManyField(UserId,verbose_name="like sany", 
> blank=True)
> share = models.PositiveIntegerField(verbose_name="paýlaşylan sany", 
> null=True, blank=True, default="0")
> createdAt = models.DateTimeField(auto_now_add=True, 
> verbose_name="goşulan 
> güni")
>
> class Meta:
> verbose_name_plural="Makalalar"
> # ordering = ("-createdAt",)
> ordering = ["-hotness",]
>
> def __str__(self):
> return self.title
>
> def likes(self):
> return self.like.count()
>
> likes.short_description = "Like sany"
> likes.allow_tags = True
>
> def seens(self):
> return self.seen.count()
>
> seens.short_description = "Görülen sany"
> seens.allow_tags = True
>
> @property
> def hotness(self):
> return self.likes() + self.seens() + self.share
>
> -- 
> 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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com
>  
> 
> .
>
 -- 
 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 view this discussion on the web visit 
 https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com
  
 
 .

>>> -- 
>>> 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 view this 

Re: django model method

2023-02-10 Thread Gabriel Araya Garcia
Allways there is a way to improve the code, but  if that not run, then try
with CYTHON

Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos




El jue, 9 feb 2023 a las 10:09, Andréas Kühne ()
escribió:

> Not if you want it to be fast. Python itself is slow - so you should try
> to offload most of the computation on the database. So filtering would need
> to be done on database fields as well.
>
> Otherwise you would need to load all of the rows into memory and then do
> the filtering there.
>
> If you want to continue along the route you have taken now - you probably
> could solve it with annotated queries - something like this:
>
> https://stackoverflow.com/questions/1396264/how-to-sort-by-annotated-count-in-a-related-model-in-django
>
> Regards,
>
> Andréas
>
>
> Den tors 9 feb. 2023 kl 13:21 skrev Chelsea Fan <
> allaberdi16yazha...@gmail.com>:
>
>> understood, is there any way to filter objects by method value?
>>
>> On Thu, Feb 9, 2023 at 4:42 PM Andréas Kühne 
>> wrote:
>>
>>> No.
>>>
>>> Ordering works by using the database to order the models. It therefore
>>> needs to be a database field - otherwise the database can't order by the
>>> field?
>>>
>>> Regards,
>>>
>>> Andréas
>>>
>>>
>>> Den tors 9 feb. 2023 kl 12:09 skrev Chelsea Fan <
>>> allaberdi16yazha...@gmail.com>:
>>>
 hello guys, Is it possible to use model method value to ordering model
 objects in meta class?

 class Post(models.Model):
 title = models.CharField(max_length=255, verbose_name="ady")
 text = RichTextField(verbose_name="text")
 tagList = models.ManyToManyField(Tag, verbose_name="taglar",
 related_query_name="tagList")
 image = models.ImageField(upload_to="postImage/", verbose_name=
 "surat")
 seen = models.ManyToManyField(UserId,verbose_name="görülen sany",
 blank=True, related_name="gorulen")
 like = models.ManyToManyField(UserId,verbose_name="like sany",
 blank=True)
 share = models.PositiveIntegerField(verbose_name="paýlaşylan sany",
 null=True, blank=True, default="0")
 createdAt = models.DateTimeField(auto_now_add=True, 
 verbose_name="goşulan
 güni")

 class Meta:
 verbose_name_plural="Makalalar"
 # ordering = ("-createdAt",)
 ordering = ["-hotness",]

 def __str__(self):
 return self.title

 def likes(self):
 return self.like.count()

 likes.short_description = "Like sany"
 likes.allow_tags = True

 def seens(self):
 return self.seen.count()

 seens.short_description = "Görülen sany"
 seens.allow_tags = True

 @property
 def hotness(self):
 return self.likes() + self.seens() + self.share

 --
 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 view this discussion on the web visit
 https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com
 
 .

>>> --
>>> 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 view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAJwZnddOzyjuawMFe4pZtn4oj8YxZDBX_N1SK492G3RhENyVqg%40mail.gmail.com
>> 
>> .
>>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAK4qSCf02LMA9tN_AEijDnM58PcT1uk8Q0zmr%2BORBc_bZXwSaA%40mail.gmail.com
> 

Re: django model method

2023-02-09 Thread Vitaly Bogomolov
You can look towards  Django Q-objects 

. 

This filtering method is implemented in the MultiChoiceExt filter 
 of my 
DjangoAdminFilters library for using in Django admin.

For filtering in regular Django views, you can use this code as a starting 
point.

четверг, 9 февраля 2023 г. в 16:21:24 UTC+4, Chelsea Fan: 

> understood, is there any way to filter objects by method value?
>
> On Thu, Feb 9, 2023 at 4:42 PM Andréas Kühne  
> wrote:
>
>> No.
>>
>> Ordering works by using the database to order the models. It therefore 
>> needs to be a database field - otherwise the database can't order by the 
>> field?
>>
>> Regards,
>>
>> Andréas
>>
>>
>> Den tors 9 feb. 2023 kl 12:09 skrev Chelsea Fan :
>>
>>> hello guys, Is it possible to use model method value to ordering model 
>>> objects in meta class?
>>>
>>> class Post(models.Model):
>>> title = models.CharField(max_length=255, verbose_name="ady")
>>> text = RichTextField(verbose_name="text")
>>> tagList = models.ManyToManyField(Tag, verbose_name="taglar", 
>>> related_query_name="tagList")
>>> image = models.ImageField(upload_to="postImage/", verbose_name=
>>> "surat")
>>> seen = models.ManyToManyField(UserId,verbose_name="görülen sany", 
>>> blank=True, related_name="gorulen")
>>> like = models.ManyToManyField(UserId,verbose_name="like sany", blank
>>> =True)
>>> share = models.PositiveIntegerField(verbose_name="paýlaşylan sany", 
>>> null=True, blank=True, default="0")
>>> createdAt = models.DateTimeField(auto_now_add=True, 
>>> verbose_name="goşulan 
>>> güni")
>>>
>>> class Meta:
>>> verbose_name_plural="Makalalar"
>>> # ordering = ("-createdAt",)
>>> ordering = ["-hotness",]
>>>
>>> def __str__(self):
>>> return self.title
>>>
>>> def likes(self):
>>> return self.like.count()
>>>
>>> likes.short_description = "Like sany"
>>> likes.allow_tags = True
>>>
>>> def seens(self):
>>> return self.seen.count()
>>>
>>> seens.short_description = "Görülen sany"
>>> seens.allow_tags = True
>>>
>>> @property
>>> def hotness(self):
>>> return self.likes() + self.seens() + self.share
>>>
>>> -- 
>>> 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 view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com
>>>  
>>> 
>>> .
>>>
>> -- 
>> 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 view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com
>>  
>> 
>> .
>>
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/12296d6e-69f3-4f4b-8c12-2bc7ac141ba8n%40googlegroups.com.


Re: django model method

2023-02-09 Thread Andréas Kühne
Not if you want it to be fast. Python itself is slow - so you should try to
offload most of the computation on the database. So filtering would need to
be done on database fields as well.

Otherwise you would need to load all of the rows into memory and then do
the filtering there.

If you want to continue along the route you have taken now - you probably
could solve it with annotated queries - something like this:
https://stackoverflow.com/questions/1396264/how-to-sort-by-annotated-count-in-a-related-model-in-django

Regards,

Andréas


Den tors 9 feb. 2023 kl 13:21 skrev Chelsea Fan <
allaberdi16yazha...@gmail.com>:

> understood, is there any way to filter objects by method value?
>
> On Thu, Feb 9, 2023 at 4:42 PM Andréas Kühne 
> wrote:
>
>> No.
>>
>> Ordering works by using the database to order the models. It therefore
>> needs to be a database field - otherwise the database can't order by the
>> field?
>>
>> Regards,
>>
>> Andréas
>>
>>
>> Den tors 9 feb. 2023 kl 12:09 skrev Chelsea Fan <
>> allaberdi16yazha...@gmail.com>:
>>
>>> hello guys, Is it possible to use model method value to ordering model
>>> objects in meta class?
>>>
>>> class Post(models.Model):
>>> title = models.CharField(max_length=255, verbose_name="ady")
>>> text = RichTextField(verbose_name="text")
>>> tagList = models.ManyToManyField(Tag, verbose_name="taglar",
>>> related_query_name="tagList")
>>> image = models.ImageField(upload_to="postImage/", verbose_name=
>>> "surat")
>>> seen = models.ManyToManyField(UserId,verbose_name="görülen sany",
>>> blank=True, related_name="gorulen")
>>> like = models.ManyToManyField(UserId,verbose_name="like sany", blank
>>> =True)
>>> share = models.PositiveIntegerField(verbose_name="paýlaşylan sany",
>>> null=True, blank=True, default="0")
>>> createdAt = models.DateTimeField(auto_now_add=True, 
>>> verbose_name="goşulan
>>> güni")
>>>
>>> class Meta:
>>> verbose_name_plural="Makalalar"
>>> # ordering = ("-createdAt",)
>>> ordering = ["-hotness",]
>>>
>>> def __str__(self):
>>> return self.title
>>>
>>> def likes(self):
>>> return self.like.count()
>>>
>>> likes.short_description = "Like sany"
>>> likes.allow_tags = True
>>>
>>> def seens(self):
>>> return self.seen.count()
>>>
>>> seens.short_description = "Görülen sany"
>>> seens.allow_tags = True
>>>
>>> @property
>>> def hotness(self):
>>> return self.likes() + self.seens() + self.share
>>>
>>> --
>>> 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 view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com
>> 
>> .
>>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAJwZnddOzyjuawMFe4pZtn4oj8YxZDBX_N1SK492G3RhENyVqg%40mail.gmail.com
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCf02LMA9tN_AEijDnM58PcT1uk8Q0zmr%2BORBc_bZXwSaA%40mail.gmail.com.


Re: django model method

2023-02-09 Thread Chelsea Fan
understood, is there any way to filter objects by method value?

On Thu, Feb 9, 2023 at 4:42 PM Andréas Kühne 
wrote:

> No.
>
> Ordering works by using the database to order the models. It therefore
> needs to be a database field - otherwise the database can't order by the
> field?
>
> Regards,
>
> Andréas
>
>
> Den tors 9 feb. 2023 kl 12:09 skrev Chelsea Fan <
> allaberdi16yazha...@gmail.com>:
>
>> hello guys, Is it possible to use model method value to ordering model
>> objects in meta class?
>>
>> class Post(models.Model):
>> title = models.CharField(max_length=255, verbose_name="ady")
>> text = RichTextField(verbose_name="text")
>> tagList = models.ManyToManyField(Tag, verbose_name="taglar",
>> related_query_name="tagList")
>> image = models.ImageField(upload_to="postImage/", verbose_name=
>> "surat")
>> seen = models.ManyToManyField(UserId,verbose_name="görülen sany",
>> blank=True, related_name="gorulen")
>> like = models.ManyToManyField(UserId,verbose_name="like sany", blank=
>> True)
>> share = models.PositiveIntegerField(verbose_name="paýlaşylan sany",
>> null=True, blank=True, default="0")
>> createdAt = models.DateTimeField(auto_now_add=True, verbose_name="goşulan
>> güni")
>>
>> class Meta:
>> verbose_name_plural="Makalalar"
>> # ordering = ("-createdAt",)
>> ordering = ["-hotness",]
>>
>> def __str__(self):
>> return self.title
>>
>> def likes(self):
>> return self.like.count()
>>
>> likes.short_description = "Like sany"
>> likes.allow_tags = True
>>
>> def seens(self):
>> return self.seen.count()
>>
>> seens.short_description = "Görülen sany"
>> seens.allow_tags = True
>>
>> @property
>> def hotness(self):
>> return self.likes() + self.seens() + self.share
>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com
>> 
>> .
>>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAJwZnddOzyjuawMFe4pZtn4oj8YxZDBX_N1SK492G3RhENyVqg%40mail.gmail.com.


Re: django model method

2023-02-09 Thread Andréas Kühne
No.

Ordering works by using the database to order the models. It therefore
needs to be a database field - otherwise the database can't order by the
field?

Regards,

Andréas


Den tors 9 feb. 2023 kl 12:09 skrev Chelsea Fan <
allaberdi16yazha...@gmail.com>:

> hello guys, Is it possible to use model method value to ordering model
> objects in meta class?
>
> class Post(models.Model):
> title = models.CharField(max_length=255, verbose_name="ady")
> text = RichTextField(verbose_name="text")
> tagList = models.ManyToManyField(Tag, verbose_name="taglar",
> related_query_name="tagList")
> image = models.ImageField(upload_to="postImage/", verbose_name="surat"
> )
> seen = models.ManyToManyField(UserId,verbose_name="görülen sany",
> blank=True, related_name="gorulen")
> like = models.ManyToManyField(UserId,verbose_name="like sany", blank=
> True)
> share = models.PositiveIntegerField(verbose_name="paýlaşylan sany",
> null=True, blank=True, default="0")
> createdAt = models.DateTimeField(auto_now_add=True, verbose_name="goşulan
> güni")
>
> class Meta:
> verbose_name_plural="Makalalar"
> # ordering = ("-createdAt",)
> ordering = ["-hotness",]
>
> def __str__(self):
> return self.title
>
> def likes(self):
> return self.like.count()
>
> likes.short_description = "Like sany"
> likes.allow_tags = True
>
> def seens(self):
> return self.seen.count()
>
> seens.short_description = "Görülen sany"
> seens.allow_tags = True
>
> @property
> def hotness(self):
> return self.likes() + self.seens() + self.share
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com
> 
> .
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com.


django model method

2023-02-09 Thread Chelsea Fan
hello guys, Is it possible to use model method value to ordering model
objects in meta class?

class Post(models.Model):
title = models.CharField(max_length=255, verbose_name="ady")
text = RichTextField(verbose_name="text")
tagList = models.ManyToManyField(Tag, verbose_name="taglar",
related_query_name="tagList")
image = models.ImageField(upload_to="postImage/", verbose_name="surat")
seen = models.ManyToManyField(UserId,verbose_name="görülen sany", blank=
True, related_name="gorulen")
like = models.ManyToManyField(UserId,verbose_name="like sany", blank=
True)
share = models.PositiveIntegerField(verbose_name="paýlaşylan sany", null
=True, blank=True, default="0")
createdAt = models.DateTimeField(auto_now_add=True, verbose_name="goşulan
güni")

class Meta:
verbose_name_plural="Makalalar"
# ordering = ("-createdAt",)
ordering = ["-hotness",]

def __str__(self):
return self.title

def likes(self):
return self.like.count()

likes.short_description = "Like sany"
likes.allow_tags = True

def seens(self):
return self.seen.count()

seens.short_description = "Görülen sany"
seens.allow_tags = True

@property
def hotness(self):
return self.likes() + self.seens() + self.share

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com.


How to sort a column by Django model method using django-sortable-listview (SortableListView)?

2020-04-20 Thread Nancy Jenifer Benita S
Using *django-sortable-listview* package, I'm using *SortableListView *to 
sort out all the model fields. In my case, more than model fields, I want 
to sort it out by model method too.

 

Models.py

class Employee(models.Model):
  name = models.CharField()
  designation = models.CharField()
  company = models.ForeignKey(Company)
  team = models.CharField()
  
  # method
  def team_count(self):
count = len(Employee.objects.filter(team=self.team))
return count

>
>

> Views.py 

class EmployeeListView(SortableListView, generic.list.ListView):

allowed_sort_fields = {'name': {'default_direction': '', 
'verbose_name': 'Employee Name'},
   'designation': {'default_direction': '', 
'verbose_name': 'Designation'},
   'team': {'default_direction': '', 
'verbose_name': 'Team Name'},
   'team_count()': {'default_direction': '', 
'verbose_name': 'No of Team members'}}
default_sort_field = 'name'
queryset = Employee.objects.all()
paginate_by = 10

>
>
On the above example, I want to list out Employee name, designation, team 
name and count of team members.

Anybody help me to sort 'team_count' column?

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4832d1e1-0acd-45dd-a56c-f1e89f4bcc96%40googlegroups.com.


Re: search results from a Django model method including the results from earlier calls to the same method (x post StackOverflow)

2019-10-28 Thread yolabingo
Solved on SO, but an extremely educational response.

I learned about mutable default argument 
gotcha 
https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument

And Django Modified Preorder Tree Traversal 
https://github.com/django-mptt/django-mptt


-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7bc04d03-76f1-4913-b44a-5e52d851c5e4%40googlegroups.com.


search results from a Django model method including the results from earlier calls to the same method (x post StackOverflow)

2019-10-28 Thread yolabingo
I also posted this to SO 
https://stackoverflow.com/questions/58600159/search-results-from-a-django-model-method-including-the-results-from-earlier-cal
hope that's ok, let me know if not.

I have a Django Model that tracks email message IDs as the message passes 
through different servers. It has a models.ForeignKey field to itself, so 
we can chain them together to follow the full path of a message through 
many servers. 

The model has a 'get_children()' method that recursively looks for all 
messages descended from the given message.

class Relay(models.Model):
hostname = models.CharField(max_length=48)
qid = models.CharField(max_length=24)
received_from = models.ForeignKey(
"self",
blank=True,
null=True,
default=None,
on_delete=models.SET_DEFAULT
)
def get_children(self, parent_messages=set()):
for r in Relay.objects.filter(received_from=self):
r.get_children(parent_messages)
parent_messages.add(self)
p = list(parent_messages)
parent_messages = set()
return p
   

If I run a single query from the Django shell, it works as expected. "Solo" 
messages return only themselves. Messages with multiple child/descendant 
messages are found as well.

>>> r = Relay.objects.get(qid='xo2')
>>> r.get_children()
[, , ]

If I kill and restart the shell, the next query works as expected, fetching 
a single message

>>> r = Relay.objects.get(qid='singletonMsg')
>>> r.get_children()
[]

But if I run get_children() repeatedly within a single Django shell 
session, it always includes the results of the previous get_children() 
calls.
   
>>> r = Relay.objects.get(qid='singletonMsg')
>>> r.get_children()
# expected result
[]  
>>>
>>> r = Relay.objects.get(qid='xo2')
>>> r.get_children()
# unexpected result - should not include singletonMsg 
[, , , ]
>>>
>>> r = Relay.objects.get(qid='singletonMsg')
>>> r.get_children()
# unexected result - should just return singletonMsg ??
[, , , ]


I was originally returning the "parent_messages" set from the function. I 
tried 
return [m for m in parent_messages]
and the current list() thinking it was a closure issue, but no luck.
I am stumped. Thanks in advance for any advice.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/84bc2c30-9186-4005-b6c3-530896619eb4%40googlegroups.com.


Re: problem with django model method save()

2012-07-04 Thread Tomas Neme
I don't see why wouldn't your model's .save() work, maybe we'd need to see
the model code, but I know you probably want to read this:

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/

once you've implemented a ModelForm, you'd pretty much would just do

form = RoomForm(request.POST)
if form.is_valid():
  form.save()

On Wed, Jul 4, 2012 at 3:58 PM, rafiee.nima  wrote:
> Hi Im some how new to django . I write a view to handle ajax request
> but I find out that save() method dose not save model instance in to the
> database
> here is my code
>
> def add_room(request):
> context={}
> status=''
> if request.is_ajax:
> if request.POST:
> hotel_instance=Hotel.objects.get(id=request.POST['hotel'])
> room_id=int(request.POST['id'])
> if room_id > 0 :
> room=HotelRoom.objects.get(id=request.POST['id'])
> room.hotel=hotel_instance
> room.number=request.POST['number']
> room.bed_count=request.POST['bed_count']
> room.ground_sleep=request.POST['ground_sleep']
> room.view=request.POST['view']
> room.reserved=request.POST['reserved']
> room.tv=request.POST['tv'].capitalize()
> room.phone=request.POST['phone']
> room.refrigerator=request.POST['refrigerator']
> room.air_condition=request.POST['air_condition']
> room.toilet=request.POST['toilet']
> room.creator=request.user
> room.save()
> room_json=model_to_dict(room)
> status="room successfully updated"
> else:
> room = HotelRoom(
> hotel=hotel_instance,
> number=request.POST['number'],
> bed_count=request.POST['bed_count'],
> ground_sleep=request.POST['ground_sleep'],
> view=request.POST['view'],
> reserved=request.POST['reserved'],
> tv=request.POST['tv'],
> phone=request.POST['phone'],
> refrigerator=request.POST['refrigerator'],
> air_condition=request.POST['air_condition'],
> toilet=request.POST['toilet'],
> creator=request.user )
> room.save()
> status="new room successfully added "
> raw_data={'status':status,'data':room_json}
> data=simplejson.dumps(raw_data)
> return HttpResponse(data, mimetype="application/json")
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/Q3kHTKx-BX4J.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.



-- 
"The whole of Japan is pure invention. There is no such country, there are
no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



problem with django model method save()

2012-07-04 Thread rafiee.nima
Hi Im some how new to django . I write a view to handle ajax request
but I find out that save() method dose not save model instance in to the 
database
here is my code 

def add_room(request):
context={}
status=''
if request.is_ajax:
if request.POST:
hotel_instance=Hotel.objects.get(id=request.POST['hotel'])
room_id=int(request.POST['id'])
if room_id > 0 :
room=HotelRoom.objects.get(id=request.POST['id'])
room.hotel=hotel_instance
room.number=request.POST['number']
room.bed_count=request.POST['bed_count']
room.ground_sleep=request.POST['ground_sleep']
room.view=request.POST['view']
room.reserved=request.POST['reserved']
room.tv=request.POST['tv'].capitalize()
room.phone=request.POST['phone']
room.refrigerator=request.POST['refrigerator']
room.air_condition=request.POST['air_condition']
room.toilet=request.POST['toilet']
room.creator=request.user
room.save()
room_json=model_to_dict(room)
status="room successfully updated"
else:
room = HotelRoom(
hotel=hotel_instance,
number=request.POST['number'],
bed_count=request.POST['bed_count'],
ground_sleep=request.POST['ground_sleep'],
view=request.POST['view'],
reserved=request.POST['reserved'],
tv=request.POST['tv'],
phone=request.POST['phone'],
refrigerator=request.POST['refrigerator'],
air_condition=request.POST['air_condition'],
toilet=request.POST['toilet'],
creator=request.user )
room.save()
status="new room successfully added "
raw_data={'status':status,'data':room_json}
data=simplejson.dumps(raw_data)
return HttpResponse(data, mimetype="application/json")

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/Q3kHTKx-BX4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.