1

I'm trying to build up a social network and want my users to have 3 privacy options [privtae, public, friends_except],

private and public are boolean fields and friends_except is a list of users

if it's not possible to store a dict in a model as a field then what do I do to implement want I want to.

Lewis Wedmore
  • 45
  • 2
  • 9
  • 2
    Possible duplicate of [How to store a dictionary on a Django Model?](https://stackoverflow.com/questions/402217/how-to-store-a-dictionary-on-a-django-model) – v.coder Sep 27 '17 at 15:01

2 Answers2

2

Firstly, if you're using a relational database, you can either create a new model containing all those attributes, and link them as foreign key with main model, or denormalize it to store all the fiels separately in the base model iteself. If you're using a nosql system like MongoDB, then you can certainly store it as a dictionary or JSON field.

Secondly, since at a time user can have only one privacy option selected, why to have a separate model or even a dictionary type construct. Just store it as a CharField with choices specified.

PRIVACY_CHOICES = [('public', 'public'), ('private', 'private', ('custom', 'custom')]

privacy_choice = models.CharField(max_length=256, choices=PRIVACY_CHOICES)
friends_allowed = models.ManyToManyField('User', blank=True)
hspandher
  • 15,934
  • 2
  • 32
  • 45
-2

look, instead of your approach what I would recommend you is to create a model let's say Friends_except with 1 field as a Foreign key to your user model. you'll be able to send all of the users you wanna block to your blocked user table

(that's kinda what happens on facebook)

then in the view, you can easily make a simple query

if:
    user.is_authentictaed and is not in users.friends_except.objects.all()

    " display your post" else Nah.
manish adwani
  • 24
  • 1
  • 7