在 ManytoManyField 中过滤
Filtering in ManytoManyField
如何只从在管理面板中创建的组中的用户进行选择?在外键中,这可以通过 limit_choices_to 来完成,而在 MtM 中则不知道。 Google 没有帮助。
class Application(models.Model):
STATUS_CHOICES = (
('In the work', 'В работе'),
('New', 'Новая'),
('Complited', 'Завершена')
)
author = models.ForeignKey(User, related_name = 'author', null = True)
title = models.CharField(max_length=50)
text = models.TextField()
room = models.CharField(max_length = 5)
published_date = models.DateField(blank=True, null=True, default = datetime.datetime.now)
status = models.CharField(max_length=15, choices=STATUS_CHOICES, default='New')
owner = models.ManyToManyField(User)
def __str__(self):
return self.title
也许我不明白你真正想要什么,但你可以在 M2M 领域使用 limit_choices_to。
查看文档:https://docs.djangoproject.com/en/2.0/ref/models/fields/#manytomany-arguments
如何只从在管理面板中创建的组中的用户进行选择?在外键中,这可以通过 limit_choices_to 来完成,而在 MtM 中则不知道。 Google 没有帮助。
class Application(models.Model):
STATUS_CHOICES = (
('In the work', 'В работе'),
('New', 'Новая'),
('Complited', 'Завершена')
)
author = models.ForeignKey(User, related_name = 'author', null = True)
title = models.CharField(max_length=50)
text = models.TextField()
room = models.CharField(max_length = 5)
published_date = models.DateField(blank=True, null=True, default = datetime.datetime.now)
status = models.CharField(max_length=15, choices=STATUS_CHOICES, default='New')
owner = models.ManyToManyField(User)
def __str__(self):
return self.title
也许我不明白你真正想要什么,但你可以在 M2M 领域使用 limit_choices_to。
查看文档:https://docs.djangoproject.com/en/2.0/ref/models/fields/#manytomany-arguments