J
J
JonGalt2017-02-17 10:14:15
Django
JonGalt, 2017-02-17 10:14:15

Filtering values ​​in a Django form?

Good afternoon. The question is how in the view you can filter the values ​​of the drop-down list. It is necessary that when creating a "document" when choosing an "organization" there are organizations only from the same project in which the document is created.
models

class Project(models.Model):
    short_name = models.CharField(verbose_name=u'краткое название', max_length=16)
    full_name = models.TextField(verbose_name=u'полное название', blank=True)

class Company(models.Model):
    inn = models.PositiveIntegerField(validators=[MaxValueValidator(9999999999)], blank=True, verbose_name=u'ИНН', unique=True)
    ogrn = models.PositiveIntegerField(validators=[MaxValueValidator(9999999999)], blank=True, verbose_name=u'ОГРН', unique=True)
    name = models.CharField(max_length=64, verbose_name=u'Название компании')

class ProjectCompany(models.Model):
    project = models.ForeignKey(Project, null=True)
    company = models.ForeignKey(Company)
    company_role = models.ForeignKey(CompanyRole)
    date_log = models.DateTimeField(verbose_name=u'Дата регистрации', auto_now_add=True)

class Documents(models.Model):
    document_name = models.CharField(max_length=32, verbose_name=u'Название документа')
    project = models.ForeignKey(Project, null=True, blank=True)
    organization = models.ForeignKey(Company, null=True, blank=True)

Forms
class DocumentForm(forms.ModelForm):
    class Meta:
        model = Documents
        fields = ('__all__')

view
def project_document_add(request, id):
    project = Project.objects.get(id=id)
    if request.POST:
        form = DocumentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('projectinfo', id)
    context = {'form': DocumentForm({'project': project}), 'project': project}
    return render(request, 'documents/projectdocumentadd.html', context)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JonGalt, 2017-02-17
@JonGalt

javedimka : Thank you
did so

class DocumentForm(forms.ModelForm):
    def __init__(self, project, *args, **kwargs):
        super(DocumentForm, self).__init__(*args, **kwargs)
        self.fields['organization'].queryset = Company.objects.filter(projectcompany__project=project)

    class Meta:
        model = Documents
        fields = ('__all__')

C
Crazy_Garry, 2017-02-17
@Crazy_Garry

So like.

def project_document_add(request, id):
    project = Project.objects.get(id=id)
    if request.POST:
        form = DocumentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('projectinfo', id)
    org = ProjectCompany.objects.filter(project=project)
    context = {
        'form': DocumentForm(initial={'project': project, 'organization': org}),
        'project': project
    }
    return render(request, 'documents/projectdocumentadd.html', context)

upd:
I chose QuerySet from ProjectCompany... It is necessary to get Organization from it...
So maybe.
org = [company for company in ProjectCompany.objects.filter(project=project)]
    context = {
        'form': DocumentForm(initial={'project': project, 'organization': org}),
        'project': project
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question