Mar-02-2024, 06:10 PM
I have in my models two tables Article and Client, the relation is ManyToMany in client :
The page shows the client fields where some already filled.
How can I save the form, I have an error :
Jean-Marie
articles = models.ManyToManyField(Article, through='ClientArticle')In one page I have to a form :
class ClientDetailsForm(forms.ModelForm): class Meta: model = Client fields = ['email', 'nom', 'adresse', 'codepostal', 'ville', 'pays', 'articles'] articles = forms.ModelChoiceField( queryset=Article.objects.all(), widget=forms.RadioSelect, required=False ) def __init__(self, *args, **kwargs): instance = kwargs.get('instance') # Utilisez l'article associé à l'instance ou utilisez tous les articles par défaut article_from_url = kwargs.get('initial', {}).get('article_from_url', None) articles_queryset = Article.objects.filter(pk=article_from_url) if article_from_url else Article.objects.all() # Ajustez le queryset pour le champ 'articles' self.fields['articles'].queryset = articles_queryset super(ClientDetailsForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_method = 'post' self.helper.add_input(Submit('submit', 'Enregistrer'))When I show the page I have an Url like this /articles/24/client/details/45, 24 is the article ID and 45 is the client ID.
The page shows the client fields where some already filled.
How can I save the form, I have an error :
Error:TypeError at /articles/24/client/details/45
'Article' object is not iterable
Request Method: POST
Request URL: http://127.0.0.1:8000/articles/24/client/details/45
Django Version: 5.0.1
Exception Type: TypeError
Exception Value:
'Article' object is not iterable
Thanks in advance,Jean-Marie