下拉选项值未保存

时间:2019-03-27 09:17:59

标签: django django-forms

嘿,

我有一个由SQL Select创建的多个选项,一个示例如下:

<select name="FirmaID" class="selectbox" id="id_FirmaID">
  <option value="1" selected="">Westcoast Computer</option>
  <option value="2">AppleNet</option>
  <option value="9">amy.rau.codes</option>
  <option value="11">Cybershield IVS</option>
</select>

您可以看到值是正确的该公司的ID,问题是如果我单击“保存”而不是“保存”。但是,如果我更改查询集,则输出看起来像:

<option value="1" selected="">1</option> 

然后就可以了。但这看起来并不友好。 Django为什么尝试将文本保存在值后面而不是值后面?

我的视图如下所示(适用于其他表单,但没有此选项字段。

def addKunder(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = AddKunder(request.user.id,  request.POST)
        # check whether it's valid:
        if form.is_valid():

            form.save()         

            return HttpResponseRedirect('/pyOekonomi/kunder/')       
    # if a GET (or any other method) we'll create a blank form
    else:
        print ('somethin goes wrong')
        form = AddKunder(request.user.id)
    return render(request, 'pyOekonomi/modules/kunder/add.html', {'form': form})

表格如下:

class AddKunder(forms.ModelForm):
    Firma = forms.CharField(widget=forms.TextInput(attrs={'class': 'input_app', 'autofocus': True}), 
                            label='Firma', 
                            required=False)

    Adresse = forms.CharField(widget=forms.TextInput(attrs={'class': 'input_app'}),
                              label='Adresse', 
                              required=False)

    Postnummer = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'input_app'}),
                                    label='Postcode', 
                                    required=False)

    Byen = forms.CharField(widget=forms.TextInput(attrs={'class': 'input_app'}),
                           label='By', 
                           required=False)

    Land = forms.ModelChoiceField(widget=forms.Select(attrs={'class': 'countryselectbox'}),
                                  queryset=CountryList.objects.values_list('countryname', flat=True).order_by('code'), 
                                  initial='Denmark', 
                                  to_field_name='countryname', 
                                  required=False)

    CVRCountrycode = forms.ModelChoiceField(widget=forms.Select(attrs={'class': 'countryselectbox'}),
                                            label='CVR Landkode', 
                                            queryset=CountryList.objects.values_list('code', flat=True).order_by('code'), 
                                            initial='DK', 
                                            to_field_name='code', 
                                            required=False)

    FirmaID = forms.ModelChoiceField(queryset=None, 
                                     widget=forms.Select(attrs={'class': 'selectbox'}), 
                                     label='Firma', 
                                     to_field_name='ID', 
                                     empty_label=None)
                                     #, queryset=FirmaModule.objects.values_list('Firmanavn', flat=True).filter(UserID=self.user).order_by('Firmanavn'), to_field_name='ID', empty_label=None)
    class Meta:
        model = FirmaModule
        fields = ['Firmanavn', 'ID']
    def __init__(self, user, *args, **kwargs):
        super(AddKunder, self).__init__(*args, **kwargs)
        self.fields['FirmaID'].queryset = FirmaModule.objects.filter(UserID=user)
    #active = forms.BooleanField()
    CVR = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'input_app'}),label='CVR Nummer', required=False)
    Kundenummer = forms.CharField(widget=forms.TextInput(attrs={'class': 'input_app' }), label='Kundenummer', required=True)
    class Meta:
        model = Kunder
        labels = {
            'Byen': 'By',
            'CVRCountrycode': 'Landekode',
            'CVR': 'CVR Nummer',
            'active': 'er aktiv',
            'IsFirmaKunde': 'Firmakunde',
            'FirmaID': 'Firma',
            'EMail': 'E-Mail Adresse'
            }
        fields = ['Kundenummer', 'FirmaID', 'Efternavn', 'Fornavn', 'Adresse', 'Telefon', 'Postnummer', 'Byen', 'Land', 'CVRCountrycode', 'CVR', 'active', 'IsFirmaKunde', 'UserID', 'EMail', 'Firma']

型号:

class Kunder(models.Model):
    ID = models.AutoField(primary_key=True)
    Kundenummer = models.IntegerField('Kundenummer', unique=True)
    Firma = models.CharField('Firma', max_length=128, null=True)
    Efternavn = models.CharField('Efternavn', max_length=128)
    Fornavn = models.CharField('Fornavn', max_length=128)
    FirmaID = models.IntegerField('FirmaID')
    UserID = models.IntegerField('UserID')
    Adresse = models.CharField('Adresse', max_length=128, null=True)
    Telefon = models.CharField('Telefon', max_length=32, null=True)
    Postnummer = models.IntegerField('Postnummer', null=True)
    Byen = models.CharField('Byen', max_length=100, null=True)
    Land = models.CharField('Land', max_length=32, null=True)
    CVRCountrycode = models.CharField('CVRCountrycode', max_length=3, null=True)
    CVR = models.IntegerField('CVR', null=True)
    active = models.BooleanField(default = False)
    IsFirmaKunde = models.BooleanField(default = False)
    EMail = models.EmailField('EMail', max_length=128, null=True)
    def __str__(self):
        return self.Firma

1 个答案:

答案 0 :(得分:0)

我自己修复了。

已更改型号

FirmaID = models.IntegerField('FirmaID')

FirmaID = models.ForeignKey('FirmaModule', on_delete=models.CASCADE,)