#Model Query Issues

88 messages · Page 1 of 1 (latest)

polar bison
#

having issues creating model queries.

polar bison
polar bison
#

that's the error when going to a customer detail

#

what im trying to do is when i go to a customer detail page, it loops through the addreses associated with the customer

runic elm
#

So can't really see the entire error message

#

This one is interesting

def CustomerDetail(request, cpk):
    customer = Customer.objects.get(Customer.id)
    customeraddresses = Address.objects.filter(customer)
    customer_detail = Customer.objects.get(id=cpk)
    context = {'customer_detail':customer_detail, 'cpk':cpk, 'customeradresses':customeraddresses }
    return render(request, 'customer/customerdetail.html', context)
 
'''
# this one works
def CustomerDetail(request, cpk):
    customer_detail = Customer.objects.get(id=cpk)
    context = {'customer_detail':customer_detail, 'cpk':cpk }
    return render(request, 'customer/customerdetail.html', context)
'''

Why does the one that "works" commented? Why not use that?

#

You could continue and get the customer address from there

#

Something like

def CustomerDetail(request, cpk):
    customer = Customer.objects.get(id=cpk)
    customeraddresses = Address.objects.filter(customer)
    context = {'customer_detail':customer, 'cpk':cpk, 'customeradresses':customeraddresses }
    return render(request, 'customer/customerdetail.html', context)
polar bison
#

i commented it out because its the one that doesnt crash the site and copied it to try to add the functionality, i will try your suggestion real quick, thank you so much

#

https://hastebin.skyra.pw/rureyomiwi.htm is what i get when i use that code, im going to github this stuff soon, i freaked myself out when i accidentally deleted all of my code one time from noodling around with a github extension

#

i was thinking maybe the foreignkey relationship is screwed up maybe? like maybe it should be a many to one relationship

#

thank you so much for helping me by the way

#

im going to TRY to pay it forward in the chat

#

just hit me in the head if im wrong

#

i use pastebin because i just want to be able to get back to my paste😭 😭 😭

polar bison
polar bison
runic elm
polar bison
#

it didnt, says the objects not interable or something like that, let me try that again

runic elm
polar bison
# runic elm its been awhile so what's the problem? I recall that the "customer detail" view ...
TypeError at /customerdetail/3
cannot unpack non-iterable Customer object
Request Method:    GET
Request URL:    http://127.0.0.1:8000/customerdetail/3
Django Version:    5.0.1
Exception Type:    TypeError
Exception Value:    
cannot unpack non-iterable Customer object
Exception Location:    /home/pangaea/dev/nine11/env/lib/python3.10/site-packages/django/db/models/sql/query.py, line 1481, in build_filter
Raised during:    customer.views.CustomerDetail
Python Executable:    /home/pangaea/dev/nine11/env/bin/python
Python Version:    3.10.12
Python Path:    
['/home/pangaea/dev/nine11/nineeleven',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/pangaea/dev/nine11/env/lib/python3.10/site-packages']
Server time:    Sun, 12 May 2024 16:13:39 +0000
runic elm
polar bison
#

that was so easy a caveman could do it🤣

runic elm
#

alright

runic elm
polar bison
#

click customers

#

then a name

polar bison
runic elm
polar bison
#

its in the database

#

its on my own server at home

runic elm
#

After clicking "Add Customer" button, there's nothing to fill in

polar bison
#

i havent made that part yet

#

i do it from the admin

polar bison
runic elm
#

OK did it

#

And yet I got a different error than yours

runic elm
# polar bison i add the customers from the admin, i havent created the form for adding custome...
Environment:


Request Method: GET
Request URL: http://localhost:8000/customerdetail/1

Django Version: 5.0.6
Python Version: 3.10.13
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'address',
 'authentication',
 'customer',
 'permits']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/usr/local/python/3.10.13/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/usr/local/python/3.10.13/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/workspaces/nineeleven/customer/views.py", line 18, in CustomerDetail
    custaddresses = Address.objects.get(customer=customer_detail)
  File "/usr/local/python/3.10.13/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/python/3.10.13/lib/python3.10/site-packages/django/db/models/query.py", line 649, in get
    raise self.model.DoesNotExist(

Exception Type: DoesNotExist at /customerdetail/1
Exception Value: Address matching query does not exist.
polar bison
#

need to have an address with the same customer assigned to it

#

add an address, with the customer you added

runic elm
#

OK added

#

Also a different error

#

1 sec

polar bison
#

what is supposed to happen is you add a customer, or addresses, or both, then when you go to the address it lists the owner associated with it

#

or when you go to a customer it lists ALL of the addresses for the customer

runic elm
polar bison
runic elm
polar bison
runic elm
polar bison
#

maybe i need to specify a many to one relationship somewhere

runic elm
#

You need to specify the other field and its value

runic elm
# polar bison maybe i need to specify a many to one relationship somewhere

Try this:

def CustomerDetail(request, cpk):
    customer_detail = Customer.objects.get(id=cpk)
    custaddresses = Address.objects.filter(customer__id=cpk)
    context = {'customer_detail':customer_detail, 'cpk':cpk, 'custaddresses':custaddresses }
    return render(request, 'customer/customerdetail.html', context)
runic elm
#

And then in your jinja template change it so it loads the individual customer address:

        {% for i in custaddresses %}
          {{ i.street }}
          {{ i.city }}
          {{ i.state }}
          {{ i.zip }}
        {% endfor %}
runic elm
polar bison
#

thank you so much

runic elm
#

np

polar bison
#

do i owe you anything

runic elm
#

You're not obligated to

polar bison
#

you got a cashapp

#

im kinda broke but ill do something

runic elm
#

Again, no pressure

#

I just like helping ppl 🙂

polar bison
#

i sent something man

#

thank you so much

runic elm
#

Wow thanks!

polar bison
#

ill need you more soon i imagine

#

hahahaha

runic elm
#

haha

polar bison
#

if not at least for a swift kick in the ass