1

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 ?

Yagami
  • 305
  • 2
  • 14

1 Answers1

0

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.

Mike Sandford
  • 1,315
  • 10
  • 22
  • I went to try your suggestion but I am getting this `Field 'id' expected a number but got ''`. I don't know what I have done it doesn't let me open /admin/notifications/notification/ through `django admin`. Through admin I am getting same error. – Yagami Aug 11 '20 at 23:11
  • 1
    I am re-reading the docs and it seems like you're supposed to supply a user group, user queryset, or list of users. Maybe try `recipient=[book.seller.user, ]` instead? It also seems like you could try `notify.send(book.seller.user, recipient=book.seller.user...)` but their docs are a bit confusing. https://github.com/django-notifications/django-notifications#generating-notifications – Mike Sandford Aug 11 '20 at 23:22
  • Yeah but this error `Field 'id' expected a number but got '` is not going away. I have commented out all the code related to `notifications` and even uninstalled app. Then I installed it again but this error is not going away at /admin/notifications/notification/ through django admin. Help me how to delete this table from `db` and install it again. – Yagami Aug 11 '20 at 23:29
  • I got my `db` back. But what about solution of my query. Have you figured it out ? – Yagami Aug 11 '20 at 23:38
  • I'm not sure what went wrong there. You haven't given much info on the nature of the problem, or what you changed. If you've commented out all the notifications package code then it seems likely that the problem lies in your non-notification code somehow. – Mike Sandford Aug 12 '20 at 10:54
  • `notify.send(book.seller.user, recipient=book.seller.user...)` this worked. Edit your answer I will accept it. – Yagami Aug 12 '20 at 16:42
  • 1
    Awesome great! Sorry we didn't get it 100% right the first time. – Mike Sandford Aug 13 '20 at 16:44