Django background tasks update database daily

Hi,

I want to run Django background tasks to call the twitter api and retrieve a count of the number of followers a particular handle has, i want this to be added to the database every day.

I have a Project model that will be storing the twitter handle, but was thinking the twitter count should be stored in a seperate model SocialMedia with a FK to Project.project_name The model would have project_name datetime,count with the idea being i can track the twitter growth over time and plot on a chart.

So is the idea re a seperate model the correct way, later i would like to add Telegram or youtube numbers too.

And is a Django background task correct or is there a better way?

Thanks

Tom

The model idea sounds fine. (This is another case where there’s no one correct way. There are many. This is one of them.)

However, this wouldn’t be a Django background task. This more sounds like it would be something run once per day.

For other discussions on this subject here, see:

Thanks, Ken.

I think first off ill look at the mgmt cmds, it seems a bit easier and without the need for msg bus.

I’ve created the function, it seems to work in retrieving the count, but when i save() its throwing an error

def get_twitter_followers(request,project_id):
    today = now().date()
    consumer_key = "#########"
    consumer_secret = "########"
    access_token = "##########"
    access_token_secret = "###################"
    project = Project.objects.get(pk=project_id) 
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    user = api.get_user(screen_name=project.project_twitter_handle)
    count = (user.followers_count)
    r = SocialMedia(pk=project_id, capture_date = today, twitter_count = count, telegram_count=count )
    r.save()

    return count,today

Error

django.db.utils.IntegrityError: null value in column "project_name_id" violates not-null constraint
DETAIL:  Failing row contains (23, 2022-01-05 00:00:00+00, 587, 587, null).

I know i should know this, but i can’t work it out?

Tom

And your SocialMedia model looks like….

(Are you supplying values for all non-null fields?)

class SocialMedia(models.Model):

    project_name = models.ForeignKey(Project,  on_delete=models.CASCADE)
    capture_date = models.DateTimeField(blank=True, null=True)
    twitter_count = models.IntegerField(null=True)
    telegram_count = models.IntegerField(null=True)

    def __str__(self):
        return str(self.project_name)

I should say that i am just running this via the shell at the moment and passing in the params needed. But i dont think that is the cause of the error

So SocialMedia has 5 fields.

Two of those fields require values, the automatically-created field named id (and frequently available through the alias pk), and project_name.

To repeat my earlier question:

:frowning: of course i missed project_name = project

Thanks Ken