1👍
✅
The post_save
signal sends an argument called created
which is
A boolean; True if a new record was created.
You can use this to differentiate between if a new record is created or just updated.
1👍
Suppose you use serializer for DRF view so you could override create()
method of your User serializer.
class UserSerializer(serializer.ModelSerializer):
...
def create(self, validated_data):
user = User.objects.create(**validated_data)
Notification.objects.create( # fill your fields here )
return user
But more clear solution is writing custom manager and override create
method there which will accept both fields for User
and for Notification
models:
class UserManager(models.Manager):
...
def create(self, gender, role ... type #and so on):
user = User(
gender=gender
role=role
...
# other fields
)
user.save()
notification = Notification(
type=type
..
# other fields
)
notification.save()
return user
class User(models.Model):
....
objects = UserManager()
serializers:
class UserSerializer(serializer.ModelSerializer):
...
def create(self, validated_data):
return User.objects.create(
gender=validated_data.get('gender')
role=validated_data.get('role')
....
type = validated_data.get('type')
- [Answered ]-Store two types of value in single Django model field
- [Answered ]-Django: call standard command from a custom command
- [Answered ]-Sending mail via smtp in Django
Source:stackexchange.com