Filippo Genoni
Filippo Genoni

Reputation: 29

Django comment(s) on users in database

I'm currently building a Django website where staff has control over users, and, within those powers, I'd like to add one where the staff members can add private comments on users so that they can be read by whoever has the power to do so.

So I started building the user models.py here what I did:

class user(models.Model):
    name = models.CharField(max_length=30)
    comments = models.TextField()
    date = models.DateField(default=datetime.now())

    def __str__(self):
        return self.name

My question: how can I add a comment field every time a staff member wants to? Like, with the above code, I can only have one comment per user.

Everything is appreciated. Thanks!

Upvotes: 0

Views: 111

Answers (1)

xyres
xyres

Reputation: 21744

There are a few ways to do this.

1. ForeignKey

Make a separate model for the comments and have a ForeignKey to the User model. That way, multiple comments can be linked to the same user.

2. ArrayField

If you're using Postgres database, you can use the ArrayField.

Cons: Editing in the admin panel is not very user friendly.

3. JSONField

You can also keep multiple comments in a JSON array.

Cons: Editing in the admin panel is not user friendly.


P.S.: If you decide to use either ArrayField or JSONField, check out an app I've made called django-jsonform. This will make editing JSON and ArrayField nice and user-friendly.

Upvotes: 1

Related Questions