1

What I want to do:

When you are on a work order, select a product so that it limits the issue areas as you define?

Something in my code generates no "issue areas".

models.py

class Products(models.Model):
    m_product_short=models.CharField(unique=True,max_length=5)
    m_product_long=models.CharField(max_length=40)

    def __str__(self):
        return self.m_product_short

class IssueAreas(models.Model):
    product=models.ForeignKey(Products, on_delete=models.SET_NULL, null=True)
    m_issue=models.CharField(max_length=50)

    def __str__(self):
        return self.m_issue

class WorkOrder_main(models.Model):
    wo_machine_product = models.ForeignKey(Products, on_delete=models.SET_NULL, null=True)
    wo_issue_area = models.ForeignKey(IssueAreas,on_delete=models.SET_NULL, null=True)

url.py

urlpatterns = [
    path("create/",views.WO_CreateView.as_view(), name="Create Order"),
  
    path('ajax/loadIssue/', views.loadIssueAreas, name='ajax_loadIssueAreas'),  # AJAX
]

views.py

class WO_CreateView(CreateView):
    template_name = 'WorkOrderCreate.html'
    model = WorkOrder_main
    form_class = WorkOrder_Add2

# AJAX
def loadIssueAreas(request):
    product_id = request.GET.get('wo_machine_product')
    issues = IssueAreas.objects.filter(product_id=product_id).all()
    return render(request, 'oa/IssueAreaDropDown.html', {'issues': issues})

forms.py

class WorkOrder_Add2(forms.ModelForm):

    class Meta:
        model=WorkOrder_main
        fields= ' __all__ '
         
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['wo_issue_area'].queryset = IssueAreas.objects.none()

        if 'product' in self.data:
            try:
                product_id = int(self.data.get('product'))
                self.fields['wo_issue_area'].queryset =IssueAreas.objects.filter(product_id=product_id).order_by('id')
            except (ValueError, TypeError):
                pass  
        elif self.instance.pk:
            self.fields['wo_issue_area'].queryset = self.instance.product.IssueAreas_set.order_by('id')

WorkOrderCreate.html

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
 {% extends "Master.html" %}
{% block title %}Create{% endblock %}

{% block content %}
<h1>Create Order</h1>


<form method="post" id="IssueForm" data-issues-url=" oa/ajax/loadIssue/" novalidate>
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Save</button>
</form>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    $("#id_wo_machine_product").change(function () {
        const url = $("#IssueForm").attr("data-issues-url");
        const productID = $(this).val(); 

        $.ajax({                     
            url: url,                    
            data: {
                'product': productID       
            },
            success: function (data) {   
              $("#id_wo_issue_area").html(data);
            }
       });
     });
</script>

{% endblock %}

IssueAreaDropDwon.html

<option value="">---------</option>
{% for issue in issues %}
<option value="{{ issuearea.pk }}">{{ issuearea.m_issue }}</option>
{% endfor %}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Jonas
  • 43
  • 6
  • Look here --> [django-dependent-select](https://stackoverflow.com/questions/25706639/django-dependent-select) – NickNaskida Aug 22 '21 at 14:35
  • Welcome to Stack Overflow. Please add minimal reproducible example, or at least an attempt, i.e. function call and error. See https://stackoverflow.com/help/how-to-ask – cards Aug 22 '21 at 22:36
  • Welcome Jonas, I have to admit, I find it a bit difficult to understand what you are trying to achieve. Could you please [edit] your post and work on your problem formulation? thanks! – Neuron Aug 23 '21 at 17:21
  • I messed up badly. I think I found the type errors. It seems to work. – Jonas Aug 24 '21 at 06:38

0 Answers0