Can you provide direction on how to get the current_sponsor and new_sponsor to show on the form.
class FishTransferForm(forms.ModelForm):
alias_name = forms.CharField(disabled=True, label='Fish to Transfer')
class Meta:
model = CustomUser
fields = ('alias_name',
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
current_sponsor = CustomUser.objects.filter(id=self.instance.sponsor).values('alias_name')[0]['alias_name']
new_sponsor = CustomUser.objects.filter(sponsor=self.instance.sponsor, status__gte=3).values('id', 'alias_name')
I can get the value to print out with this code, but I cannot use it. Seems like I should be able to, otherwise what good is the function?
class FishTransferForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
fish = self.instance.pk
print(fish)
alias_name = forms.CharField(disabled=True, label='Fish to Transfer')
sponsor = forms.CharField(disabled=True, label='Current Sponsor')
# current_sponsor = str(CustomUser.objects.filter(id=self.instance.sponsor).values_list('alias_name', flat=True))
# new_sponsor = CustomUser.objects.filter(sponsor=self.instance.sponsor, status__gte=3).values_list('id', 'alias_name')
class Meta:
model = CustomUser
fields = ('alias_name',
'sponsor',
)
This is the value in the terminal
34
Try this
from django import forms
class FishTransferForm(forms.ModelForm):
alias_name = forms.CharField(disabled=True, label='Fish to Transfer')
current_sponsor = forms.CharField(disabled=True, label='Current Sponsor', required=False)
new_sponsor = forms.ChoiceField(label='New Sponsor', required=True)
class Meta:
model = CustomUser
fields = ('alias_name', 'new_sponsor')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.sponsor:
current_sponsor = CustomUser.objects.get(id=self.instance.sponsor)
self.fields['current_sponsor'].initial = current_sponsor.alias_name
new_sponsors = CustomUser.objects.filter(sponsor=self.instance.sponsor, status__gte=3)
new_sponsor_choices = [(user.id, user.alias_name) for user in new_sponsors]
self.fields['new_sponsor'].choices = new_sponsor_choices
Well, there are some syntax issues with your code, can you try below code once, you may get the result what you are looking for.
from django import forms
class FishTransferForm(forms.ModelForm):
alias_name = forms.CharField(disabled=True, label='Fish to Transfer')
current_sponsor = forms.CharField(disabled=True, label='Current Sponsor')
new_sponsor = forms.ChoiceField(label='New Sponsor')
class Meta:
model = CustomUser
fields = ('alias_name', 'current_sponsor', 'new_sponsor')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# You need to define current_sponsor and new_sponsor here
current_sponsor = CustomUser.objects.filter(id=self.instance.sponsor).values('alias_name').first()
new_sponsors = CustomUser.objects.filter(sponsor=self.instance.sponsor, status__gte=3).values('id', 'alias_name')
# Set the initial values for the fields
self.fields['current_sponsor'].initial = current_sponsor['alias_name']
self.fields['new_sponsor'].choices = [(sponsor['id'], sponsor['alias_name']) for sponsor in new_sponsors]
Thanks
IT WORKED PERFECTLY!!!
Thank you so much! I do appreciate it and I understand what I was missing.