简体   繁体   中英

Limit forms foreign key to a choices in related model

I am working on creating a form but stuck at this issue.

I have several businesses in the Business Model. Each Business has its own Service in Services Model. The User is tied to only one Business. Both Business, Service have a relationship.

My Challenge

I have a Service Request Form. When I present this Service Request Model Form, I want to show only services for One Business, that the customer/user belongs to. Please help me how this is possible. I thought it would be like "Instance = Business". I understood its not that simple.

For example: Business1 has "Cars" and "Motor Bikes" as Services and Business2 has "nails" and "Hair Spa" as services. If a user from Business1 logged in and opened Service Request Form, She/He should see only "Cars" and "Motor Bikes" in service selection drop down.

'''

    # class Service(models.Model):
class Business(models.Model):
    name = models.CharField(max_length=25)
    description = models.CharField(max_length=100)
    active = models.BooleanField(default=True)

class BusinessUser(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    business = models.ForeignKey(Business, on_delete=models.CASCADE, related_name='business')
    
class Services(models.Model):   
    business = models.ForeignKey(Business, on_delete=models.CASCADE, related_name='business_services')
    name = models.CharField(max_length=15)
    active = models.BooleanField(default=True)

class ServiceRequest(models.Model):
    business = models.ForeignKey(Business, on_delete=models.DO_NOTHING)
    service = models.ForeignKey(Service, on_delete=models.DO_NOTHING, blank=True)
    requester_name = models.CharField(max_length=15)

 class  ServiceRequestForm(forms.ModelForm):
     class Meta:
        model = ServiceRequest
        fields = '__all__'

def newServiceRequest(request):  //the view
    if request.method == 'GET':
        user = request.user
        business = user.business
        serviceRequestForm = ServiceRequestForm(instance=business)
        return render(request,'service.html', {'form':serviceRequestForm})

'''

One way is to use django-select2 . See installation instructions here

Then in your form you can do:

 class  ServiceRequestForm(forms.ModelForm):
     class Meta:
        model = ServiceRequest
        fields = '__all__'
        widgets = {
            'business': ModelSelect2Widget(
                    model=Business,
                    attrs={'class': 'form-control', 'data-minimum-input-length': 0},
                    search_fields=['name__icontains'],
            ),
            'service': ModelSelect2Widget(
                    model=Services,
                    attrs={'class': 'form-control', 'data-minimum-input-length': 0},
                    search_fields=['name__icontains'],
                    dependent_fields={'business': 'business'},
            ),
        }

The key element is the dependent_fields option. Read more about it here

You can pass the current business and update your queryset in the ModelForm constructor.



class  ServiceRequestForm(forms.ModelForm):

    def __init__(self, business, *args, **kw):
        super(ServiceRequestForm, self).__init__(*args, **kw)
        self.fields['business'].queryset = \
           self.fields['business'].queryset.filter(pk=business.pk)

    class Meta:
        model = ServiceRequest
        fields = '__all__'


def newServiceRequest(request):  # the view
    user = request.user
    business = user.business
    
    if request.method == 'POST':
        serviceRequestForm = ServiceRequestForm(business, data=request.POST)
        if (serviceRequestForm.is_valid()):
             serviceRequest = serviceRequestForm.save()
             # another stuff...
             # ... 
    else:
        serviceRequestForm = ServiceRequestForm(business)

    return render(request,'service.html', {'form':serviceRequestForm})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM