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
  Python django view error ZeeKolachi 1 233 Mar-18-2024, 03:14 PM
Last Post: Sowmya
  Selenium fields containing a word graham23s 3 2,781 Aug-22-2023, 12:35 PM
Last Post: Gaurav_Kumar
  Registration Form Fields rwahdan 2 1,835 Aug-09-2022, 10:35 AM
Last Post: Addweb
Photo After using models.Model on my class Im getting 'ImproperlyConfigured' error Django khavro 1 2,150 Apr-05-2021, 03:11 PM
Last Post: SheeppOSU
  upload big file in Django with process bar and i get error : MemoryError ma_norouzifar 3 4,337 Aug-06-2019, 06:57 AM
Last Post: fishhook
  Django Python Vscode Error Please Help Smhbugra 3 2,662 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 3,246 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django Connection Error erfanakbari1 1 2,543 Mar-21-2019, 08:09 AM
Last Post: Larz60+
  django-geoposition and unknown column error? PrateekG 3 3,832 Jun-26-2018, 11:56 AM
Last Post: PrateekG
  Why some Django fields are not saved in database? PrateekG 0 2,558 Jun-21-2018, 08:32 AM
Last Post: PrateekG

Forum Jump:

User Panel Messages

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