繁体   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