0
votes

all - I need a list view page that shows all the Seller Accounts a User is following, and under each of those Seller names, their list of Product titles, i.e.:

User = user1

Following:

  1. Seller 1
  • Items: Item1, Item2, Item3
  1. Seller 2
  • Items: Item4, Item5, Item6

I am able to pull the products for any one particular seller if they are alone in the page (like a Detail View/ Vendor List) but I can't figure out the logic for displaying all users at once and connecting the products beneath them.

REALLY appreciate the help here!

User model:

class CustomUser(AbstractUser):
email       = models.EmailField(max_length=255, unique=True)
is_active   = models.BooleanField(default=True)
signup_timestamp   = models.DateTimeField(default=now, editable=False)
objects     = CustomUserManager()

def __str__(self):
    return self.username

Seller Model:

class SellerAccount(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
active = models.BooleanField(default=True)
username = CustomUser.username
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)

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

def get_absolute_url(self):
    return reverse("artist_detail", kwargs={"artist_name": self.user.username})

Sellers a particular user is following:

class WatchedArtist(models.Model):
user        = models.ForeignKey(CustomUser, null=False, on_delete=models.CASCADE)
seller      = models.ForeignKey(SellerAccount, null=False, on_delete=models.CASCADE)
watched     = models.BooleanField(default=True)
timestamp   = models.DateTimeField(default=now, editable=False, blank=True, null=True)

Products of sellers:

class Product(models.Model):
seller      = models.ForeignKey(SellerAccount, null=False, on_delete=models.CASCADE)
title       = models.CharField(max_length=30, blank=False)
slug        = models.SlugField(unique=True, blank=True)


def __str__(self):
    return self.title

def get_absolute_url(self):
    view_name = "products:detail"
    return reverse(view_name, kwargs={"slug": self.slug})
1

1 Answers

0
votes

a user follow sellerAccount by model "WatchedArtist", in "WatchedArtist" artiste you have SellerAccount , and SellerAccount have many product , so :

                                                       | <-- product n1
        | -> WatchedArtist n1 --> SellerAccount n3 <---| <-- product n2
        |                                              | <-- product n3
        |                                              | <-- ect...
user -->|
        |                                              | <-- product n42
        | -> WatchedArtist n2 --> SellerAccount n15 <--| <-- product n43
        |                                              | <-- product n44
        |                                              | <-- ect...
        | --> ect ...

in code:

user = User.objects.get(username="my_username")

for watched in user.watchedartists.all():  # you have all followed seller by user

    print(watched.seller) # here you print the __str__ methods of SellerAccount

    for product in watched.seller.products.all():  # you have all product of seller
        print(product)