Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 13, 2010 15:59:30

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Paginator and foreign key


Hello list, I have a question. Take a look to the next models.
----------------------------
class MyDoc(models.Model):
code = models.BigIntegerField()

class Myline(models.mode):
fk_myDoc = models.ForeignKey(MyDoc)
---------------------------------------

In my view I do something like

myPaginator = Paginator(MyDoc.objects.filter(codigo=1), 10)
myDocs = myPaginator.page(page_number)

This return all the Docs with a code=1, but I too want to return all
the objects with the type "MyLine" associated to the results of this
query.

I try to do something like
--------------------
for i in range(len(myDocs.object_list))
myDocs.object_list.append(Myline.objects.filter(fk_myDoc=myDocs.object_list.id))
-------------
but don't work

I know that I can do 2 queries and return two list, one with the myDoc
list and other with the myLine list, but in that case in my template I
must to do something like

{% for myDoc im MyDocs.object.list %}
{% for myline in Mylines %}
{%if myDoc.id == myline.fk_myDoc %}
..................
{% endif %}
{% endfor %}
{% endfor %}

I don't want to do this, because I think a big "for" inside a big
"for" is innecessary.

Somebody have an idea??
Thanks for read, and sorry for my bad english.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 13, 2010 16:33:35

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Paginator and foreign key


if I return another list:
-----------------------
for i in range(len(paginatorDocs.object_list)):
resultados = {'myDoc': paginatorDocs.object_list,
'myLines':
Myline.objects.filter(fk_myDoc=paginatorDocs.object_list.id),}

-----------------------
With this I can get the values, but I'm not sure if this is the best
way to solve the problem

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 13, 2010 17:56:53

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Paginator and foreign key


On 13 déc, 16:59, refreegrata <refreegr...@yahoo.com> wrote:
> Hello list, I have a question. Take a look to the next models.
> ----------------------------
> class MyDoc(models.Model):
>     code = models.BigIntegerField()
>
> class Myline(models.mode):
>    fk_myDoc = models.ForeignKey(MyDoc)
> ---------------------------------------
>
> In my view I do something like
>
> myPaginator = Paginator(MyDoc.objects.filter(codigo=1), 10)
> myDocs = myPaginator.page(page_number)
>
> This return all the Docs with a code=1, but I too want to return all
> the objects with the type "MyLine" associated to the results of this
> query.

for doc in myDocs.object_list:
print doc.MyLine_set.all()


or in your template:

<ul>
{% for doc in myDocs.object_list %}
<li>
{{ doc.code }}
<ul>
{% for line doc.MyLine_set.all %}
<li>{{ line }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 13, 2010 18:49:25

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Paginator and foreign key


thanks for the answer, but the solution don't show anything. I must be
doing something wrong.
if now I have
-----------------------
class Myline(models.mode):
fk_myDoc = models.ForeignKey(MyDoc, related_name="wwww")
-----------------------

How Can I use {% for line doc.XXXXX_set.all %} in the template?
I'm sorry for this asking, but I can't find the _set.all option in the
Django documentation.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 14, 2010 14:26:59

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Paginator and foreign key


On 13 déc, 19:49, refreegrata <refreegr...@yahoo.com> wrote:
> thanks for the answer, but the solution don't show anything. I must be
> doing something wrong.
> if now I have
> -----------------------
> class Myline(models.mode):
>    fk_myDoc = models.ForeignKey(MyDoc, related_name="wwww")
> -----------------------
>
> How Can I use {% for line doc.XXXXX_set.all %} in the template?
> I'm sorry for this asking, but I can't find the _set.all option in the
> Django documentation.http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support