Skip to content

Instantly share code, notes, and snippets.

View akshar-raaj's full-sized avatar
💭
Learning something new everyday!

Akshar Raaj akshar-raaj

💭
Learning something new everyday!
View GitHub Profile

This post is targetted towards Celery beginners. It discusses different ways to run Celery.

  • Using celery with a single module
  • Using celery with multiple modules.
  • Using celery with multiple modules split across packages.

Prerequisites

You should have Celery and Redis installed. We will use Redis as our broker.

@akshar-raaj
akshar-raaj / serializers.py
Created August 7, 2019 17:13
UserSerializer with overridden create
class UserSerializer(serializers.ModelSerializer):
def validate_password(self, value):
if value.isalnum():
raise serializers.ValidationError('password must have atleast one special character.')
return value
def validate(self, data):
if data['first_name'] == data['last_name']:
raise serializers.ValidationError("first_name and last_name shouldn't be same.")
@akshar-raaj
akshar-raaj / models.py
Created July 7, 2019 19:18
Search models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published', null=True)
author = models.CharField(max_length=200, null=True)
def __str__(self):
return self.question_text
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'first_name', 'last_name')
def to_representation(self, instance):
representation = super().to_representation(instance)
if instance.is_superuser:
representation['admin'] = True
@akshar-raaj
akshar-raaj / serializers.py
Created August 7, 2019 13:18
User serializer with validate_password
class UserSerializer(serializers.ModelSerializer):
def validate_password(self, value):
if value.isalnum():
raise serializers.ValidationError('password must have atleast one special character.')
return value
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'password')
Question.objects.values('question_text').annotate(cnt=Count('question_text'))
Question.objects.annotate(avg_votes=Avg('choice__votes'))
Choice.objects.aggregate(avg=Avg('votes'))
@akshar-raaj
akshar-raaj / total_votes_cast.py
Created August 12, 2019 09:02
Total votes cast
Choice.objects.aggregate(num_votes=Sum('votes'))
@akshar-raaj
akshar-raaj / question_with_no_choices.py
Created August 12, 2019 09:01
Question with no choices
Question.objects.annotate(num_votes=Sum('choice__votes')).filter(num_votes__isnull=True)