new to Django having models/views/templates question

43 views
Skip to first unread message

Chris Strasser

unread,
Jun 2, 2015, 2:00:22 PM6/2/15
to django...@googlegroups.com
Hi am plugging away at learning Django and have done well so far (thanks to great documentation and Stackoverflow) but I have run into a problem that i cant seem to figure out.


I have a model that refers to another model that refers to another model.

example :

Class ServiceOrder
    id -integer
   customer location = foreign key to location

def __str__(self):
        return self.stid

class Location:
   id -integer
   address -char
   customer- foreign key to customer

        q= '%s %s %s %s ' %(self.addr1, self.addr2, self.city, self.country)
        return unicode(q).encode('utf-8')

Class Customer
    id=int
    name- char
return unicode(self).encode('utf-8')

I have a template that lists the service orders view is below:


def serviceorder_list(request, status = 'Closed'):
        ist = ServiceOrder.objects.filter(status = status, stid__lte=20239).select_related('customer_location__customer')    #stid__lte 20239 to fix database error ... i have existing data that has its own issues...
        count = order_list.count()
        context ={'list': sto_list, 'count': count}
     
        return render(request, 'order/listview.html', context)


in my listview.html

{% for item  in list %}
            {% if item.project %}
            <td><a href="{{ item.stid }}">{{item.stid}}</a></td>
            <td>{{item.project}}</td>
            <td>{{item.customer_location.name}}</td>  ---------------------- 2 questions here  1) it throws an error on customer names that have accents etc ... 
            <td>{{item.notes}}</td>                                                                              2) i am confused about how to properly referenece the customer name or anything else that is not in the Service order  model.                   
            <td>{{item.person_responsible}}</td>
            <td>{{item.entrydate|date:'M-d-y'}}</td>
          <td>{{item.status}}</td>
          <td>{{item.billing}}</td> 
          </tr>


I have read the docs many times and the more i read the more i know that i don't know ... any help would be appreciated


Thanks
 
 

James Schneider

unread,
Jun 2, 2015, 2:06:57 PM6/2/15
to django...@googlegroups.com

Can you post the error and traceback? It sounds like you may have an encoding issue.

-James

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/76ad4dab-574e-4867-96b6-96be02e8a739%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Strasser

unread,
Jun 2, 2015, 2:57:11 PM6/2/15
to django...@googlegroups.com
Hi James thanks for the response...


this line: <td>{{item.customer_location.customer}}</td>

..... worked this morning and all last week  now gives this error:

RuntimeError at /sto/

maximum recursion depth exceeded while calling a Python object
Request Method: GET
Request URL: http://10.0.0.102:8080/sto/
Django Version: 1.8
Exception Type: RuntimeError
Exception Value




maximum recursion depth exceeded while calling a Python object


I can reproduce the encoding error but i forget how to get there at the moment ...

James Schneider

unread,
Jun 2, 2015, 6:01:26 PM6/2/15
to django...@googlegroups.com

Posting the actual model code will probably help.

Your template has this:

item.customer_location.name

But you specified the issue later as this:

item.customer_location.customer

The former won't work per your summarized model definitions. The Location model has no attribute called 'name'.

The latter will work only if your __str__() function is encoding the data correctly. The reason you are getting a recursion error is because of the 'unicode(self)' (which I'm assuming is part of your model's __str__() definition). You need to change that to something like unicode(self.name). I'm not even sure if the unicode() call is even necessary (haven't done a ton of Python 3 yet).

As far as referring to the Customer name, you can do {{ item.customer_location.customer.name }}

Have you thought about tying in the customer directly to the service order? It shouldn't be an issue to have a customer tied to both the location and the service order. While it'll work the way you have it, your lookups will become more complicated (and expensive from a processing perspective) since you are forcing a lookup across multiple tables. It may not agree with your business process, though. Just a thought.

-James


--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Chris Strasser

unread,
Jun 3, 2015, 6:07:08 AM6/3/15
to django...@googlegroups.com

Hi James, Thanks again for responding.

I didn’t post my actual models as they are quite large and my problem is more about understanding proper technique than anything.  Self.name did fix my immediate problem thanks, however I am really curious about your last paragraph … how can I tie the customer directly to the service order ? .. I have no idea how I might do it in Django.

 

C.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/9G-WUE4A8ZA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.


To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Mario Gudelj

unread,
Jun 3, 2015, 5:38:30 PM6/3/15
to django...@googlegroups.com

By creating a foreign key to it.

Reply all
Reply to author
Forward
0 new messages