I found the mistake, I removed the 'for item in instance' in my
addcustomer function and Elesire suggestion about iterating in the
{{customer.gp}} object in my template helped me a lof. Thank you soo much.
Le mardi 25 août 2020 à 14:57:48 UTC+1, Annick Sakoua a écrit :
> Hi all,
> Please Helppppp.
> I have been on this problem for 2 weeks now :(
> I have an object: customer that has 1 manytomany relationship with
> another object: gp.
> I created some gp instances in the admin page. When I select one gp in the
> select dropdown list of existing gp in my addcustomer form and submit my
> form, the form is saved with all the information except the gp selected. I
> only get the this: app.GeneralPractitioner.None
> Here are bellow my different classes.
>
> Thank you for your help :)
>
> *model.py*
>
> class Customer(models.Model):
> first_name = models.CharField(max_length=100)
> last_name = models.CharField(max_length=100)
> gp = models.ManyToManyField(GeneralPractitioner, help_text="Select your
> general practitioner", related_name="customer")
>
>
> class GeneralPractitioner(models.Model):
> name = models.CharField(
> max_length=40,
> help_text="Enter the gp name)"
> )
> contact= models.CharField(max_length=11)
>
> def __str__(self):
> return self.name
>
> *in view.py*
>
> @login_required
> def add_customer(request):
> if request.method == 'POST':
>
> form = AddCustomerForm(request.POST)
> if form.is_valid():
> instance = form.save(commit=False)
> for item in instance:
> item.save()
> form.save_m2m()
> instance.author = request.user
>
> messages.success(request, f'Your form has been created!')
> return redirect('addcustomer')
> else:
> form = AddCustomerForm()
> return render(request, 'app/addcustomer.html', {'form': form})
>
> *customer_details.html*
>
> {% extends "app/base.html" %}
> {% load crispy_forms_tags %}
> {% block content %}
>
> <div>
> <p>Firstname: {{customer.first_name}}</p>
> <p>Lastname: {{customer.last_name}}</p>
> <p>General practitioner: {{customer.gp}}</p>
> </div>
>
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/6426a74f-6096-473e-87bf-3a1c4914392en%40googlegroups.com.