In a ModelForm, how can I filter a Select widget content with only values related with current instance? (The instance which called the ModelForm)
For example:
class smsForm(ModelForm):
class Meta:
model = SMS
fields = ['phone', 'state']
widgets = {
'phone': Select(choices=phoneSMS.objects.all().values_list('id', 'phone'), attrs={'class':'form-control',}),
'state': bootStrapButtonSelect( attrs={'class': 'buttons-sms', }), }
What I want to do is to filter that phoneSMS with only the phones which contains the sms reference. Here're the models:
class SMS(models.Model):
student = models.ForeignKey(Student)
day = models.DateField(db_index=True)
tries = models.IntegerField(default=0)
phone = models.CharField(max_length=9, default='---')
sent = models.BooleanField(default=False)
state = models.CharField(max_length=20, choices=OPTIONS, default='nothing')
class phoneSMS(models.Model):
phone= models.CharField(max_length=120)
sms = models.ForeignKey(SMS)
I tried with objects.filter(sms=SMS) but no result.