Using comment with Models Components

I have two models Profile and Post; I want to allow the user to add comments on any of them without repeating the code.
In other words, the Profile and the Post components contain a comment field, and the user who views the Profile wants to add comments, or who views Posts intends to add a comment.
I have created another component (class) for the comments and assigned the comments as a foreign key inside Profile and Post.

Is there any “data modelling” basis for sharing the concept of a “comment” between Profile and Post? Can a Profile or Post have more than one comment?

In general, what I would recommend is to create a generic Comment class as an abstract model, then create to subclasses for ProfileComment and PostComment, where each class adds a ForeignKey field to their desired related class.

Thank you for your reply.
This is exactly what I did.
I have created a class for the Comment, and I added a comment as a Field inside Profile and Post classes, and I added a ForeignKey to both of them.
One more question, what is the best way to use Notification in Django for the Profile, Post, and Comments, and assign three different types “Link, Static, and System”?

To be clear, what I was recommending was this:

class Profile:

class Post:

class Comment:
    class Meta:
        abstract = True

class PostComment(Comment):
    base_item = ForeignKey(Post, ...)

class ProfileComment(Comment):
    base_item = ForeignKey(Profile, ...)

But, whether this is the most appropriate for your situation would depend upon a lot of details regarding the specifics of these classes.

Alright, understood.
I know that I can’t use the same ticket to ask for more things.
But actually, since I am facing a big issue here, I want some advice.

Let’s say Profile and Comment classes have a text field attribute, and I want to encode these strings, to avoid XSS attacks.

So, I have created a function inside the that encodes the string using base64, as follows:

def protect(data):
    encodedBytes = base64.b64encode(data.encode("utf-8"))
    encodedStr = str(encodedBytes, "utf-8")

class Post: 
title = models.CharField(protect(data), max_length=150) 

and once I defined the Charfield, I am getting an error that ‘builtin_function_or_method’ object has no attribute ‘encode’
I am gonna ask some advice from you.

Side note: These aren’t tickets - this is a discussion forum. You’re free to continue the conversation here as you see fit. (However, if it really does venture into a different area, I might suggest opening a new topic, but that would only be to make that topic more discoverable for others who may be facing the same issue.)

You can’t call functions in model field definitions. You need to call that function in a function where that field is being assigned or otherwise being processed.

I appreciate your help again.
Can I get an example by calling that function in another function?

It’s fundamentally no different than calling any function from any other function. The specifics are really going to depend upon how and where it’s going to be used.

The first function will encode any string input, and the second function will store the encoded string and return it to be used in the model.Field

To help me target my answers better, can you describe your level of experience with Python and Django?

I am supposed to be at mid-level in Python and Django, but sometimes I do something new completely and this really makes me go crazy, I have tested my code in Python compiler but it’s not working with Django.
I am using Python 3.10.8, and the latest version of Django 4.1.3

Ok, so whenever you have an issue regarding code here, please always post the code in question. It makes it a lot easier to discuss these types of issues in the context of the real code being worked on. (Just remember to enclose each file’s worth of code between lines of three backtick - ` characters.)

def protect(data):
   encodedBytes = base64.b64encode(data. Encode("utf-8"))
   encodedStr = str(encodedBytes, "utf-8")

class Post(models.Model):
        title = models.Charfield(max_length=200)
        description = models.Charfield(max_length=250)
        comment = models.Charfield(max_length=250)

class Profile(models.Model):
        title = models.Charfield(max_length=200)
        description = models.Charfield(max_length=250)
        comment = models.Charfield(max_length=250)

Let’s assume that I have Post, and Profile as models. and I want to protect any string input like the title, description, and comment from any Cross-Site Scripting (XSS) attacks by encoding the strings.

I hope now the question and the code are clear.

So where is this data coming from? Something needs to call protect, and use what protect returns to set the value of whatever field needs to be protected.

I understand the fact that these data are coming from the client side, and the protection must happen in the HTML code.
Is there any way that the model can do the job before going to the HTML?

No, HTML doesn’t “do” anything with the data.

How do you want the data stored in the database, as the raw text or as the base64 encoded text?

If you want it stored in the database as base64, then you want to do the conversion as the data is coming in, not as it’s going out.

If you want the data going out to be encoded as base64, then you would convert it before returning it to the client.

These models only exist in the server, they are not part of the HTML or data sent to the browser. (You are sending representations of the data contained within the models to the browser, not the models themselves.)

There is a misunderstanding here. I apologize for that, and I appreciate your time.
I know that HTML has nothing to do with the data.

For the data
I want to store it as the base64 encoded text

The user will input the data, and the base64 will encode these data and store it as an encoded text.

That just brings us back to my previous reply at Using comment with Models Components - #14 by KenWhitesell

I am gonna check this.
Thank you again for your time and your help.