0

I've customized the django user object as follows,

class User(AbstractBaseUser):
    mobile = models.CharField(max_length=100, unique=True)
    email = models.EmailField(max_length=255)
    username = models.CharField(max_length=255)
    full_name = models.CharField(max_length=255, blank=True, null=True)
    staff = models.BooleanField(default=False)
    admin = models.BooleanField(default=False)
    root = models.BooleanField(default=False)

I also have a feed and a Source object, which has a one-to-many relationship.

class Source(Base):
    name = models.CharField(max_length=255)


class Feed(Base):
    headline = models.CharField(max_length=255)
    link = models.CharField(max_length=255)
    summary = models.TextField()
    published_date = models.DateTimeField()
    views = models.IntegerField(default=0)
    shares = models.IntegerField(default=0)
    source = models.ForeignKey(Source, on_delete=models.CASCADE,)

Now I want to simulate a bookmarking action and thus associate many feeds with one user. How do I do that in Django.

Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
  • this might help https://stackoverflow.com/questions/37650362/understanding-manytomany-fields-in-django-with-a-through-model – Dawit Abate Feb 20 '18 at 23:38

1 Answers1

1

Add another model Bookmark.

class Bookmark(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    fed = models.ForeignKey(Feed, on_delete=models.CASCADE)
    last_read_page = models.IntegerField()

Then add this field to your Feed model.

reader = models.ManyToManyField(User, through='Bookmark')
Dawit Abate
  • 462
  • 5
  • 10
  • This looks excellent. Two quick questions, though, how can I access all bookmarks for an user, second, what does last_read_page actually do? – Melissa Stewart Feb 21 '18 at 02:44
  • 1
    sorry for the late response.. you can access the bookmarks of a user by `.bookmark_set.all()`. and `last_page_read` is the last page the user read that feed. like a bookmark for a book. – Dawit Abate Feb 21 '18 at 21:11