**forms.py** class KomForm(forms.ModelForm): class Meta: model = Kom fields = ('company', 'title',) **models.py** class Company(models.Model): author = models.ForeignKey('auth.User') title = models.CharField(max_length=200) created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title class Kom(models.Model): author = models.ForeignKey('auth.User') title = models.CharField(max_length=200) company = models.ForeignKey(Company, on_delete=models.CASCADE) def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title_ 

Tell me how you can when filtering a form to filter Company objects so that the user sees in the ModelChoiceField field only those companies (Company) that he created himself.

Thanks to the invaluable help of caring people, I solved this problem. The correct answer is Where Answer; the only thing was that I had to change something in some places. It turned out like this, and it works:

 def __init__(self, *args, **kwargs): # вызываем конструктор формы и сохраняем пользователя if 'user' in kwargs and kwargs['user'] is not None: user = kwargs.pop('user') qs = Company.objects.filter(author__id=user.id) super(KommpredForm, self).__init__(*args, **kwargs) self.fields['company'].queryset = qs 

and in the submission it is necessary to indicate like this:

form = KommpredForm (request.POST, user = user)

    1 answer 1

    Use the filter in the form field, and the user in the form can be transferred during creation.

     class KomForm(forms.ModelForm): class Meta: model = Kom fields = ('company', 'title',) def __init__(self, *args, **kwargs): if 'user' in kwargs and kwargs['user'] is not None: user = kwargs.pop('user') qs = Company.objects.filter(author__id=user.id) # вызываем конструктор формы и добавляет query set super(KomForm, self).__init__(*args, **kwargs) try: self.fields['company'].queryset = qs except NameError: pass def your_view(request): user = request.user if request.user.is_authenticated() else None form = KomForm(user=user) return render('template.html' {'form': form}) 
    • Thanks for the answer, but will you tell me how to use self.user.id correctly in this case? I understand that this is possible elementary things, but I just can not figure out how to get an authorized user. By your example, I get the error: NameError: name 'self' is not defined - Alexey
    • Updated the answer, should work - waynee
    • Thank you for helping to resolve the issue. But for now I’m getting the error __init __ () got an unexpected keyword keyword argument on the solution you proposed. During this time, I conducted extensive research work, but apparently has not yet filled all the gaps in understanding. - Alexey
    • In the question I posted that corrected a bit, so in general you gave excellent advice! Thank. - Alexey
    • Well) the answer is corrected - waynee