S_alj
S_alj

Reputation: 447

Django- Object representation based on a Foreign Key

I'm testing a relatively simple model in my Django project:

class Restricted_Form(models.Model):
    Form = models.ForeignKey(Form)
    Authorized_Users = models.ManyToManyField(User)
    Authorized_Groups = models.ManyToManyField(Group)

The point is to simply associate each form with some users, and groups. In the Django Admin, I want to have these Restricted_Fom objects listed, being represented by the "title" of the Form they are associated to (instead of having them all listed indistinguishably as Restricted_Form object) . I've tried something like

@property
    def __str__(self):
        return self.Form.title

but it's incorrect. How do I achieve this?

Is there a way to, for example, have another field, that's a string, that automatically populates itself based on the other field (the ForeignKey one, Form)?

Upvotes: 1

Views: 211

Answers (1)

mohammad
mohammad

Reputation: 2198

@property is not required because str() is by default something like a property and if you print the object, output of str() is shown.

Upvotes: 1

Related Questions