I am getting this error when i use many to many field help me plsssssss
views.py
def add_ship(request): if request.method=='POST': m_namedriver = request.POST.get('m_namedriver') driver_id = Driver.objects.get(driver_id=m_namedriver) m_licensepl = request.POST.get('m_licensepl') car_id = Car.objects.get(car_id=m_licensepl) m_weightcoolbox = request.POST.get('m_weightcoolbox') coolbox_id = Coolbox.objects.get(coolbox_id=m_weightcoolbox) ship_date = request.POST.get('ship_date') ship_time = request.POST.get('ship_time') original = request.POST.get('original') destination = request.POST.get('destination') if shipping.objects.count() != 0: ship_id_max = shipping.objects.aggregate(Max('shipping_id'))['ship_id__max'] next_ship_id = ship_id_max[0:2] + str(int(ship_id_max[2:6])+1) else: next_ship_id = "SP1000" new_shipping = shipping.objects.create( shipping_id = next_ship_id, driver_id = driver_id, car_id = car_id, coolbox_id = coolbox_id, ship_date = ship_date, ship_time = ship_time, original = original, destination = destination, ) new_shipping.save() new_shipping.coolbox_id.add(coolbox_id) return render(request,'add_ship.html',{'message1':"Add shipping successful."}) driver_ship = Driver.objects.all() car_ship = Car.objects.all() coolbox_ship = Coolbox.objects.all() return render(request,'add_ship.html',{'driver_ship':driver_ship,'car_ship':car_ship,'coolbox_ship':coolbox_ship})
I’ve been stuck here for 3 days now. At first I used it as a CharField so I could add it, but when I added another ID it couldn’t add it. It says there is a problem in this part ship_id_max = shipping.objects.aggregate(Max(‘shipping_id’))[‘ship_id__max’]
models.py
class Coolbox(models.Model): coolbox_id = models.CharField(max_length=40,primary_key=True) medicine_name = models.ForeignKey(Medicine, on_delete=models.CASCADE, related_name="medicinename") weight = models.FloatField(blank=True, null=True) coolboxtemp_max = models.FloatField(blank=True, null=True) coolboxtemp_min = models.FloatField(blank=True, null=True) dimension = models.CharField(max_length=40) d_measurement = models.CharField(max_length=40) t_measurement = models.CharField(max_length=40) total = models.FloatField(blank=True, null=True) status = models.CharField(max_length=20, choices=STATUS, blank=True) def __str__(self): return f"{self.medicine_name}" class shipping(models.Model): shipping_id = models.CharField(max_length=15,primary_key=True) driver_id = models.ForeignKey(Driver, on_delete=models.CASCADE, related_name="driverfk") car_id = models.ForeignKey(Car, on_delete=models.CASCADE, related_name="carfk") coolbox_id = models.ManyToManyField(Coolbox, related_name="coolboxfk") ship_date = models.DateField(blank=True,null=True) ship_time = models.TimeField(blank=True,null=True) original = models.CharField(max_length=200) destination = models.CharField(max_length=200) def __str__(self): return f"{self.shipping_id}: {self.driver_id}"
enter image description here enter image description here
HTML
<div class="col-md-6 mb-4"> <div class="form-outline multip_select_box"> <label class="form-label" for="Coolboxs">Coolboxs ID</label> <br> <select name="m_weightcoolbox" id="m_weightcoolbox" class="multi_select form-control" multiple data-selected-text-format="count > 3" > {% for m_weightcoolbox in coolbox_ship %} <option value="{{ m_weightcoolbox.coolbox_id }}">{{ m_weightcoolbox.coolbox_id }}</option> {% endfor %} </select> </div> </div> </div> enter code here
Help me plsssssssssss
Advertisement
Answer
You need to pass a List of objects of not a single object at save
you can do like this …
def add_ship(request): if request.method=='POST': m_namedriver = request.POST.get('m_namedriver') driver_id = Driver.objects.get(driver_id=m_namedriver) m_licensepl = request.POST.get('m_licensepl') car_id = Car.objects.get(car_id=m_licensepl) m_weightcoolbox = request.POST.getlist('m_weightcoolbox') print(m_weightcoolbox) coolb_id = [i for i in Coolbox.objects.filter(coolbox_id__in=m_weightcoolbox)] ship_date = request.POST.get('ship_date') ship_time = request.POST.get('ship_time') original = request.POST.get('original') destination = request.POST.get('destination') if shipping.objects.count() != 0: ship_id_max = shipping.objects.aggregate(Max('shipping_id'))["shipping_id__max"] next_ship_id = ship_id_max[0:2] + str(int(ship_id_max[2:6])+1) else: next_ship_id = "SP1000" new_shipping = shipping.objects.create( shipping_id = next_ship_id, driver_id = driver_id, car_id = car_id, ship_date = ship_date, ship_time = ship_time, original = original, destination = destination, ) new_shipping.coolbox_id.set(coolb_id) return redirect("add_ship") return render(request,'add_ship.html',{'message1':"Add shipping successful."}) driver_ship = Driver.objects.all() car_ship = Car.objects.all() coolbox_ship = Coolbox.objects.all() return render(request,'add_ship.html',{'driver_ship':driver_ship,'car_ship':car_ship,'coolbox_ship':coolbox_ship})
CODE
https://drive.google.com/file/d/18v_BgTLSmMLhfNCDojhYlTeqkfUsASqt/view?usp=share_link
OUTPUT
after downloading Animatoin.gif file open it in Chrome Browser it’s the full video of working with your project
https://drive.google.com/file/d/1klxwCUb47UI2NimCnSvrqkpc2SB0691S/view?usp=share_link