Application: This is a seminar proposal system for a conference. The user can create presenters and seminars and link them together. Each user should have access only to the facilitators and seminars that he created / owns.
# Models: class Workshop(models.Model): name = models.CharField(max_length=140, db_index=True) presenters = models.ManyToManyField("Presenter", through="WorkshopPresenter") owner = models.ForeignKey(User) class Presenter(models.Model): name = models.CharField(max_length=140, db_index=True) owner = models.ForeignKey(User) class WorkshopPresenter(models.Model): workshop = models.ForeignKey("Workshop") presenter = models.ForeignKey("Presenter") cardinality = models.IntegerField()
To associate presenters with seminars, the user is directed to a seminar page containing a modelformset for WorkshopPresenter . The seminar and power are set according to the presentation after filling out a set of forms, so the user sees only a list of drop-down lists with possible names of presenters. Association Page Image
Question How can I make the drop-down lists of presenters on this association page contain only those who belong to the current user? Drop-down lists should contain only the results of Presenter.objects.filter(owner__exact=request.user) . They currently contain all .
# View snippet that creates the formset: workshop = Workshop.objects.filter(owner__exact=request.user).get(id=workshop_id) MyWorkshopPresenterFormSet = modelformset_factory(WorkshopPresenter, formset=WorkshopPresenterFormSet, extra=5, exclude = ("workshop","cardinality")) formset = MyWorkshopPresenterFormSet(request.POST or None, queryset=workshop.workshoppresenter_set.all())
WorkshopPresenterFormSet simply extends BaseModelFormSet and performs some custom validation, nothing unusual.
I saw some solutions that work for regular forms, but nothing works with modelformsets.
source share