Search code examples
pythondjangoapi-design

Bookmarks implementation logic django


I am developing REST API using django rest framework. Now I want to make a functionality to mark some ads as favorite. Here are some of my ideas:

  1. creating new model Favorites, containing user id and ad id. So, mobile app will be fetching them from tables. However, making table containing foreignkeys only looked not good for architecture.
  2. adding array field inside user model, and save ad ids inside. Tried using ArrayField from postgres-extensions, but I keep getting typeerror related with 'form_size'. Even though I removed 'form_size' from migrations files, as it is shown here, I get 502 error.

So, should I keep using ArrayField and try to fix that error? Or creating table with 2 foreignkeys only looks not that bad?

Please, If anyone faced 'form_size' typeerror, help me.

Thank you.

Update 1. Here is favorites field in User table

favorites = ArrayField(
        base_field=models.CharField(max_length=50), default=[], blank=True,
    )

I am importing these:

````from django.contrib.postgres.fields import ArrayField

UPD 1.

Solution

  • I can suggest 2 methods, but prefer the second one since it's simpler:
    - Creating a new Model "Bookmark" is fine, you'll have foreign keys to the USER and AD models, and this will let you add an other attributes (like bookmarked_date, bookmarks tag...)
    - Simply adding a ManyToMany attribute "Bookmarks" to the USER Model, this is easy and should be enough in your case