[Answer]-Django Models limit ManyToManyField to owner

1👍

Try doing that using F() objects:

Something like:

from django.db.models import F
class CustomerSettings(models.Model):
    owner = models.ForeignKey(User)
    authorized_ips = models.ManyToManyField(AuthorizedIps, 
                    limit_choices_to={'owner': F('customersettings__owner')})

However, you would have to handle cases when you are creating a object. That time no owner will be set, so limit choices will give you empty set.

👤Rohan

Leave a comment