Python Forum
Error - ManyToMany Fields in Django
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error - ManyToMany Fields in Django
#1
I have this error in Django :
Quote: __str__ returned non-string (type SimpleLazyObject)

idk why..
I'm learning Django and I don't know to use ManyToMany Relations in Db Well.

This is my code.. where 's the error ?


in models.py

from macelleria_app.models import Prodotti


class Cart_item (models.Model):


    product = models.ForeignKey (Prodotti, on_delete=models.CASCADE)
    quantity = models.FloatField (default=0)



    def __str__ (self):
        return self.product




class Cart (models.Model):
    cart_user = models.ForeignKey(User, on_delete=models.CASCADE)
    prodotti= models.ManyToManyField(Cart_item)
    a="3"

    def __str__ (self):
        return self.a




    def __str__ (self):
        return self.cart_user
in views.py :
def carrello (request):

    carrello = Cart.objects.get(cart_user=request.user)
    prodotti_carrello = carrello.prodotti
    print (carrello)

    context = {'prodotti_carrello': prodotti_carrello}
    return render (request,'carrello.html', context)


@login_required
def aggiungi_carrello (request, product_id, quantity):

    product_id_1 = int (product_id)
    quantity_1 = int (quantity)
    carrello_utente = Cart ()
    carrello_utente.cart_user= request.user


    ordine_item = Cart_item()
    articolo_1= Prodotti.objects.get (id=product_id_1)
    ordine_item.product = Prodotti.objects.get (id=product_id_1)
    ordine_item.quantity= quantity_1
    ordine_item.save()


    carrello_utente.prodotti.set(ordine_item)

    carrello_utente.save ()
    return HttpResponse ('/carrello'). #I'm working to render the real Cart
and in urls.py there is a string like "path('carrello/update/<product_id>/<quantity>/', views.aggiungi_carrello, name='update_cart') "

somebody who can help me?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo After using models.Model on my class Im getting 'ImproperlyConfigured' error Django khavro 1 413 Apr-05-2021, 03:11 PM
Last Post: SheeppOSU
  How to fill text fields web forms Martinelli 1 890 Sep-25-2019, 11:14 PM
Last Post: Martinelli
  upload big file in Django with process bar and i get error : MemoryError ma_norouzifar 3 2,227 Aug-06-2019, 06:57 AM
Last Post: fishhook
  Selenium fields containing a word graham23s 2 960 Jul-23-2019, 10:44 PM
Last Post: graham23s
  Django Python Vscode Error Please Help Smhbugra 3 1,240 Jun-30-2019, 10:54 AM
Last Post: noisefloor
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 1,306 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django Connection Error erfanakbari1 1 1,168 Mar-21-2019, 08:09 AM
Last Post: Larz60+
  django-geoposition and unknown column error? PrateekG 3 2,077 Jun-26-2018, 11:56 AM
Last Post: PrateekG
  Why some Django fields are not saved in database? PrateekG 0 1,361 Jun-21-2018, 08:32 AM
Last Post: PrateekG
  Django REST - input of some form fields is send twice to the backend ann 4 2,208 Jan-03-2018, 05:43 PM
Last Post: ann

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020