I am using django-notifications-hq package link of django-notifications-hq and getting error:
Cannot assign "<Seller: john@gmail.com>": "Notification.recipient" must be a "User" instance.
my custom user
model:
class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255, blank=True, null=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
customer = models.BooleanField(default=False)
seller = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
my seller
model having 1to1 relation with user
:
class Seller(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
seller_picture = models.ImageField(upload_to='profile_pic/seller', null=True, blank=True)
my Book
model has foreign key seller
:
class Book(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField('Title', max_length=255)
authors = models.ManyToManyField(Author, related_name='book_written_by')
seller = models.ForeignKey(Seller, on_delete=models.CASCADE)
price = models.DecimalField('Price', decimal_places=2, max_digits=10)
description = models.TextField('Description')
And somewhere in my views
after successfully getting books
just bought by some user, I want to notify all respective sellers
that someone has bought their book:
for book in books:
notify.send(Seller, recipient=book.seller, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
I know it works fine recipient = request.user
but I have different scenario. How can I make book.seller
a User
instance ?
It's a super simple solution. You're 99% of the way there!
Instead of this:
for book in books:
notify.send(Seller, recipient=book.seller, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
Try this:
for book in books:
notify.send(book.seller.user, recipient=book.seller.user, verb="User has just bought book named {} priced
as {}".format(book.title, book.price))
The Seller
has a property called user
but it isn't a user
itself.