I Have three ForeignKey in my model.. When I Create Form with ModelForm and 
show the foreign key field into html select it execute Duplicate Queries 
for each Foreignkey field 
Heres My Models.py

class TeamModel(models.Model):
    team_name = models.CharField(unique=True, db_index=True, max_length=50, 
validators=[check_project_name])
    description = HTMLField()
    project = models.ForeignKey(ProjectModel, on_delete=models.CASCADE, 
related_name='project')
    team_leader = models.ForeignKey(CustomUser, on_delete=models.CASCADE, 
related_name='team_leader')
    created_by = models.ForeignKey(CustomUser, on_delete=models.CASCADE, 
related_name='created_by')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

My Views.py

@login_required()
def create_team(request):
    template_name = 'admin_panel/Team/create_team.html'
    if request.method == "POST":
        form = TeamForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Team Created Successfully")
            return HttpResponseRedirect(reverse('create_team'))
    else:
        form = TeamForm()
    context = {
        'form': form,
    }
    return render(request, template_name, context)


My Forms.py

class TeamForm(forms.ModelForm):
    # team_name = forms.CharField(widget=forms.TextInput(attrs={'class': 
'form-control'}))
    # description = TinyMCE(attrs={'class': 'form-control', 'style': 
'width:100%'})
    project = 
forms.ModelChoiceField(queryset=ProjectModel.objects.filter(project_status='Active'),
                                     widget=forms.Select(attrs={'class': 
'form-control'}),
                                     empty_label='Please Select A Project')
    team_leader = forms.ModelChoiceField(
        queryset=CustomUser.objects.exclude(access_level__in=['Issue Creator', 
'Monitor']),
        widget=forms.Select(attrs={'class': 'form-control'}), 
empty_label='Please Select A Team Leader')

    class Meta:
        model = TeamModel
        fields = ['team_name', 'description', 'project', 'team_leader']
        widgets = {
            'team_name': forms.TextInput(attrs={'class': 'form-control'}),
            'description': TinyMCE(attrs={'class': 'form-control', 'style': 
'width:100%'}),
        }

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f57c819e-ac26-4a93-90b8-81aad5a0e682%40googlegroups.com.

Reply via email to