[Answer]-Getting field value counts and pushing values to other field of other model

1👍

This is the way:

#counting
n = New.objects.filter( source = 'xyz' ).count()

#storing
c = Crawler.objects.get( id = **some id**)
c.total_news_crawled = n
c.save()

Don’t forget import models.

Edited due OP comment

You can write a custom method to update your field:

class Crawler(models.Model):
    name = models.CharField(max_length=25)
    Id = models.CharField(max_length=25, primary_key=True)
    total_news_crawled= models.IntegerField(default=0)
    active = models.BooleanField(True)

    def __unicode__(self):  # Python 3: def __str__(self):
        return self.name

    def update_total_news_crawled( self ):
        self.total_news_crawled = New.objects.filter( source = 'xyz' ).count()

Edited due OP comment

To call custom method:

c = Crawler()
c.name = "crazy crawler"
c.Id = "abc"
c.active = True
c.update_total_news_crawled()
c.save()

Leave a comment