Passing instance method result to class attribute

Viewed 46

Let's say I have two models, the first referencing a third User model:

class Parent(models.Model):
    user = models.ForeignKey(User)
...

class Child(models.Model):
    parent = models.ForeignKey(
        Parent,
        limit_choices_to={'user': get_user()}
    )

    def get_user(self):
        return self.request.user

I want to limit choices for the child model to instances bound to current user. One way to do it would be to pass the request to the form class and solve it inside __init__, but it present's other limitations. Is there a way to do this inside the model class, kind of like in the example above?

0 Answers
Related