Django help| Urgent | how to find the frequency of a string for each word using django admin?

37 views
Skip to first unread message

DJANGO DEVELOPER

unread,
Jul 8, 2021, 5:51:22 PM7/8/21
to Django users
let suppose I have 2 models:
class String(models.model):
    string = models.textarea(null = True, blank = True)
    user = models.ForeignKey(user, on_delete=models.CASCADE)
    def __str__(self):
        return self.string

and


class Song_Words_Count(models.model):
    song = models.ForeignKey(user, on_delete=models.SET_NULL)
    def __str__(self):
        return self.song

now what I want to do at django admin side that the string added in "String" model should be saved in Song_Wors_count model and string should be splitted in a single word and frequency should be counted this way:

word word_freq
word1 10
word2 15
word3 20

I need Urgent help please.

DJANGO DEVELOPER

unread,
Jul 8, 2021, 10:27:23 PM7/8/21
to Django users
is there anyone who can help me here?

--
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/f53fc668-ca86-4c51-8d4c-673dee3d180cn%40googlegroups.com.

Peter van der Does

unread,
Jul 9, 2021, 11:31:19 AM7/9/21
to django...@googlegroups.com

Overwrite the save method on the String model and when you hit save you split and save the words in the S_W_C model. This does mean when you update the String record all the words are added to the S_W_C model as well, as there is no correlation between the two model you don't know if that String was already counted.

Tech Cue

unread,
Jul 9, 2021, 11:56:37 AM7/9/21
to django...@googlegroups.com


 class String(models.model):
    string = models.textarea(null = True, blank = True)
    user = models.ForeignKey(user, on_delete=models.CASCADE)
    def __str__(self):
        return self.string
class Song_Words_Count(models.model):
    song = models.ForeignKey(user, on_delete=models.SET_NULL)
 
 
    def __str__(self):
        return self.song


https://docs.djangoproject.com/en/3.2/topics/db/queries/

Sebastian Jung

unread,
Jul 9, 2021, 12:21:34 PM7/9/21
to django...@googlegroups.com
Hello Peter,

ich would make it with a signal.

Here a another example to use a post save signal:

from django.db.models.signals import post_save
from django.dispatch import receiver

class TransactionDetail(models.Model):
    product = models.ForeignKey(Product)

# method for updating
@receiver(post_save, sender=TransactionDetail, dispatch_uid="update_stock_count")
def update_stock(sender, instance, **kwargs):
    instance.product.stock -= instance.amount
    instance.product.save()

Please write me if you don't understand these example.

P.S. i have no idea why you have a foreignkey relation. Why you save words not in another column in String class?
Regards

DJANGO DEVELOPER

unread,
Jul 9, 2021, 1:15:47 PM7/9/21
to Django users
Hi Peter, thanks for your reply. Sorry I have m2m relationship of User model with Song model and I am using m2m_changes signal for it but still not giving me the desired output.

DJANGO DEVELOPER

unread,
Jul 9, 2021, 1:18:08 PM7/9/21
to Django users
hi Sebastian, do you know how can I resolve my problem? I want to count same words in a string. after splitting it

Tree Chip Net

unread,
Jul 9, 2021, 3:12:20 PM7/9/21
to django...@googlegroups.com, Tree Chip Net, thiago...@tchipnet.com.br, Thiago Prates
--
Reply all
Reply to author
Forward
0 new messages