Allow a limit_choices_to callable to accept the current model instances object

125 views
Skip to first unread message

Ochui Princewill

unread,
Sep 25, 2018, 6:25:38 PM9/25/18
to Django users
Hello, 

Am current working on a project and i want to filter the content of a ForeignkeyField base on the current model object

class Subscription(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
plan = models.ForeignKey(Plan, on_delete=models.CASCADE)
expires = models.DateField()
created_at = models.DateTimeField(auto_now_add=True)

class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, editable=False)
subscription = models.ForeignKey(Subscription, limit_choices_to={'user_id': settings.AUTH_USER_MODEL}, on_delete=models.SET_NULL, null=True, blank=True)

Tim Graham

unread,
Sep 25, 2018, 6:47:21 PM9/25/18
to Django users
There's a ticket with that feature request: https://code.djangoproject.com/ticket/25306
Reply all
Reply to author
Forward
0 new messages