'Christian' via Django users
2018-11-13 09:24:37 UTC
Hello
I need to use a model form to create a new instance of Recette, using
multiple choices list instead of the foreign_key
/class Recette(models.Model)://
//Â ......//
//Â Â Â categoriederecette = models.ForeignKey(Categorie,
on_delete=models.CASCADE) //
/
/class Categorie(models.Model)://
//Â Â Â SOUPE = 'SP'//
//Â Â Â DESSERTS ='DS'//
//Â Â Â PLATUNIQUE ='PU'//
//Â Â Â LISTE_CHOIX =Â ((SOUPE,'Soupe'), (DESSERTS,'Desserts'),
(PLATUNIQUE,'Plat unique'),)//
//Â Â .....//
//Â Â Â categorierecette = models.CharField(db_column='Categorierecette',
max_length=15, choices=LISTE_CHOIX, blank=True, null=True)Â # Field name
made lowercase..../
I use a Modelform/:
/
/class RecettesForm(forms.ModelForm)://
//Â Â Â categoriederecette = forms.ChoiceField(choices =
Categorie.LISTE_CHOIX)//
////
//Â Â Â class Meta://
//Â Â Â Â Â Â Â model = Recette//
//Â Â Â Â Â Â Â fields = (......//,'categoriederecette')/
/
/
The form is displayed correctly, but cannot be saved. In the test /if
form.is_valid():/Â I get a value error -
/ValueError: Cannot assign "'SP'": "Recette.categoriederecette" must be
a "Categorie" instance./
Any help would be greatly appreciated.
I need to use a model form to create a new instance of Recette, using
multiple choices list instead of the foreign_key
/class Recette(models.Model)://
//Â ......//
//Â Â Â categoriederecette = models.ForeignKey(Categorie,
on_delete=models.CASCADE) //
/
/class Categorie(models.Model)://
//Â Â Â SOUPE = 'SP'//
//Â Â Â DESSERTS ='DS'//
//Â Â Â PLATUNIQUE ='PU'//
//Â Â Â LISTE_CHOIX =Â ((SOUPE,'Soupe'), (DESSERTS,'Desserts'),
(PLATUNIQUE,'Plat unique'),)//
//Â Â .....//
//Â Â Â categorierecette = models.CharField(db_column='Categorierecette',
max_length=15, choices=LISTE_CHOIX, blank=True, null=True)Â # Field name
made lowercase..../
I use a Modelform/:
/
/class RecettesForm(forms.ModelForm)://
//Â Â Â categoriederecette = forms.ChoiceField(choices =
Categorie.LISTE_CHOIX)//
////
//Â Â Â class Meta://
//Â Â Â Â Â Â Â model = Recette//
//Â Â Â Â Â Â Â fields = (......//,'categoriederecette')/
/
/
The form is displayed correctly, but cannot be saved. In the test /if
form.is_valid():/Â I get a value error -
/ValueError: Cannot assign "'SP'": "Recette.categoriederecette" must be
a "Categorie" instance./
Any help would be greatly appreciated.
--
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 django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
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/524c62ab-2454-7746-ec6a-9c7daeed775d%40yahoo.fr.
For more options, visit https://groups.google.com/d/optout.
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 django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
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/524c62ab-2454-7746-ec6a-9c7daeed775d%40yahoo.fr.
For more options, visit https://groups.google.com/d/optout.