List Info

Thread: Get list by Foreign Key when using generic views




Get list by Foreign Key when using generic views
user name
2006-02-23 13:53:42
New to Django; getting my feat a bit wet and need some
assistance.

I would like to display a list of "statements"
for a particular
"account". The "account_id" is a
foreign key on "statements" table. Is
it possible, using generic views, to specify in the
urlpatterns the
<object_id> that will generate SQL query that will
query on foreign key
("account_id") rather then primary key
("id")?

In SQL terms I wish to achieve the following if possible
while still
using the generic view module:
SELECT * FROM statements WHERE
account_id=<account_fk_id-in-url>
instead of using primary key as filter
SELECT * FROM statements WHERE
id=<statement_pk_id-in-url>

If this is possible, what is the key requirement for getting
this to
work?
Can I define in "urlpatterns" to filter by
foreign key instead of
primary key?

Regards,
-Alen


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Get list by Foreign Key when using generic views
user name
2006-02-23 19:25:49
On Thursday 23 February 2006 13:53, alen.ribicgmail.com
wrote:

> I would like to display a list of
"statements" for a particular
> "account". The "account_id" is
a foreign key on "statements" table.
> Is it possible, using generic views, to specify in the
urlpatterns
> the <object_id> that will generate SQL query that
will query on
> foreign key ("account_id") rather then
primary key ("id")?
>
> In SQL terms I wish to achieve the following if
possible while still
> using the generic view module:
> SELECT * FROM statements WHERE
account_id=<account_fk_id-in-url>
> instead of using primary key as filter
> SELECT * FROM statements WHERE
id=<statement_pk_id-in-url>
>
> If this is possible, what is the key requirement for
getting this to
> work?

Think about it this way:  you want to get a specify account,
and then 
view all the statements for that account, right?  So use an 
'object_detail' generic view to display a single
'account' object (the 
account_id being retrieved from the URL), and in the
template do 
something like this:

{% for statement in object.get_statement_list %}
   whatever
{% endfor %}

The only down side is that you are getting the 'account'
object perhaps 
unnecessarily.

Luke

-- 
OSBORN'S LAW
    Variables won't, constants aren't.

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Get list by Foreign Key when using generic views
user name
2006-02-24 11:07:02
Ok, I see. I used the Many-to-One relationship though from
the
Statement model and changed it to Many-to-Many instead
creating an
attribute in Account model instead.

Now I can get the statements via the account object in my
template.

Thanks Luke for your help.

-Alen


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )