簡體   English   中英

基於外鍵過濾數據 - Django

[英]Filtering Data Based On Foreign Key - Django

我有一個問題是根據之前選擇的字段動態填充表單數據。 在我的情況下,我有兩個模型,其中包含與不同俱樂部相關的不同類型的會員資格。 然后我有另一個模型處理個別俱樂部的注冊。

我的問題 - 當最終用戶准備好注冊表單渲染時(我已經根據他們最初選擇的俱樂部過濾掉了成員)但我需要根據玩家模型選擇的成員資格(外鍵)過濾價格。

以下是我的會員類型模型:

模型存儲俱樂部可用的會員資格,以便會員可以在注冊頁面上選擇和付款

class ClubMemberships(models.Model):

club_id = models.ForeignKey(ClubInfo, on_delete=models.CASCADE)
title = models.CharField(max_length=30, default='')
price = models.DecimalField(default=0.00, max_digits=6, decimal_places=2)
description = models.TextField()

def __str__(self):
    return self.title

這是注冊的模型:

用於存儲要用於會員注冊的玩家信息的模型

class Player(models.Model):

club_id = models.ForeignKey(ClubInfo, on_delete=models.CASCADE)
membership_title = models.ForeignKey(ClubMemberships, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
dob = models.DateField(max_length=8)
email = models.EmailField(max_length=50)
phone = models.CharField(max_length=12)
mobile = models.CharField(max_length=15)
emergency_contact_name = models.CharField(max_length=40)
emergency_contact_mobile = models.CharField(max_length=15)
address1 = models.CharField(max_length=30)
address2 = models.CharField(max_length=30, default='')
address3 = models.CharField(max_length=30, default='')
town = models.CharField(max_length=30)
county = models.CharField(max_length=30)
country = models.CharField(max_length=30)

def __str__(self):
    return "%s %s" % (self.first_name, self.last_name)

球員注冊表格:

表格接受會員注冊的詳細信息

class PlayerRegistrationForm(forms.ModelForm):

class Meta:
    model = Player
    fields = '__all__'
    labels = {
        'dob': 'Date of Birth'
    }
    widgets = {
        'dob': forms.DateInput(attrs={'id': 'datepicker'})
    }

def __init__(self, *args, **kwargs):
    super(PlayerRegistrationForm, self).__init__(*args, **kwargs)
    self.fields['club_id'].widget = forms.HiddenInput()

def load_price(self, request):
    membership = request.GET.get('membership_title')
    title = ClubMemberships.objects.filter(title=membership)
    self.fields['price'].queryset = ClubMemberships.objects.filter(price=title.price)

load_price是我想要完成的一個例子,但無法使其正常工作。 我希望表單檢查表單中選擇的成員資格,然后過濾該成員資格的價格並將其顯示在表單中。

這是我在瀏覽器中的表單:

形成

非常感謝任何幫助,因為我無法合並PayPal,直到我能正確顯示價格。

謝謝

這是基於汽車制造商和型號的示例。

這個javascript正在尋找制造商在下拉列表中更改的時間

$("#id_car_make").change(function () {
    var url = $("#searchForm").attr("data-models-url");  // get the url of the `load_cities` view
    var carMakeId = $(this).val();  // get the selected country ID from the HTML input

    $.ajax({                       // initialize an AJAX request
        url: url,                    // set the url of the request (= localhost:8000/hr/ajax/load-cities/)
        data: {
            'car_make': carMakeId       // add the country id to the GET parameters
        },
        success: function (data) {   // `data` is the return of the `load_cities` view function
            $("#id_car_model").html(data);  // replace the contents of the city input with the data that came from the server
        }
    });

});

它叫這個網址

    path('ajax/load-models/', views.load_models, name="ajax_load_models"),

它調用此視圖

def load_models(request):
    car_make_id = request.GET.get('car_make')
    car_models = CarModel.objects.filter(car_make_id=car_make_id).order_by('name')
    return render(request, 'leases/partials/car_model_dropdown_list_options.html', {'car_models': car_models})

它使用此模板將模型下拉傳遞給javascript

    <option value="">Model</option>
    {% for car_model in car_models %}
    <option value="{{ car_model.pk }}">{{ car_model.name }}</option>
    {% endfor %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM