python - ModelForm won't validate because missing value, but model field has null=True -


i have problem modelform trying assign '' field (it saves fine if provide primary key of product, it's not compulsory field, , won't save if field left blank). take orm it's trying set field ''but:

  • shouldn't '' coerced none, and;
  • why isn't model form trying set field none in first place instead of ''?

models.py

class question(models.model):     fk_product = models.foreignkey(product, on_delete=models.set_null, null=true, related_name="product_question") 

forms.py

class questionform(forms.modelform):     fk_product=forms.choicefield(required=false)     class meta:         model = question         fields = ['fk_product',] 

the error:

cannot assign "''": "question.fk_product" must "product" instance.

the view code produces error:

    questionmodelformset = modelformset_factory(question,                                 form=questionform,                                 extra=1)     question_formset = questionmodelformset(                             data=request.post,                             files=request.files,                             queryset=question.objects.all())     if not question_formset.is_valid(): #error occurs on line 

try adding blank=true too.

null=true means field allowed null in database.

blank=true means can submitted without value in forms. otherwise must have value.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -