use dict in * LISTE_CHOIX.* On Tue, Nov 13, 2018 at 6:14 PM 'Christian' via Django users < [email protected]> wrote:
> 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. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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 > <https://groups.google.com/d/msgid/django-users/524c62ab-2454-7746-ec6a-9c7daeed775d%40yahoo.fr?utm_medium=email&utm_source=footer> > . > 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 [email protected]. To post to this group, send email to [email protected]. 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/CAF2Ah_E2UnvGgt2G%3DZ1ECc2wvnaWPH32da5h%2BXxZhsgtjprHpA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

