Hi all,

I am having an issue dynamically populating form data based on the previous
selected field. In my case I have two models one which contains different
types of memberships associated to different clubs. Then I have another
model which handles registrations for individual clubs.

My problem - when the end-user is ready to sign up the form renders (I
already filter out members based on the club they originally selected) but
I need to filter the price based on the membership selected (foreign key)
of player model.

*Below is my model for the membership types:*

# Model to store clubs available memberships so members can select and
pay on registration page
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


*Here is the model for the registration:*

# Model to store player information to be used for membership registration
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)

*Form for player registration:*

# Form to accept details for members to register
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)


The load_price is an example of what I am trying to accomplish but cannot
get it working. I want the form to *check the membership selected* in the
*form* then *filter* the *price of that membership* and *display it in the
form*.

*Here is my form in the browser:*

[image: image.png]

Would really appreciate any help as I cannot incorporate PayPal until I can
correctly display the price.

Thanks

Gavin

-- 
__

Séanadh Ríomhphoist/_

Email Disclaimer__
**

Tá an ríomhphost seo agus 
aon chomhad a sheoltar leis faoi rún agus is lena úsáid ag an seolaí agus 
sin amháin é. Is féidir tuilleadh a léamh anseo. 
<https://www4.dcu.ie/iss/seanadh-riomhphoist.shtml>  
<https://www4.dcu.ie/iss/seanadh-riomhphoist.shtml>*
_

This e-mail and any 
files transmitted with it are confidential and are intended solely for use 
by the addressee. Read more here. 
<https://www4.dcu.ie/iss/email-disclaimer.shtml> _
*_

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHZR7Jejvn2BrbY21EC_OQp1tFAye%2BvuichJn0k59OXi2R4mpQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to