views:

94

answers:

3

Hello ,

I have two models ,

class A(models.Model):

   id = models.AutoField(primary_key=True)
   name = models.CharField(max_length=200)
   type = models.CHarFIeld(max_length=200)
   ..
   ..
   .. 

class B(models.Model):
  a= models.ForeignKey(A)
  state = models.CharField(max_length=200)

now when i am seeing the page of class A i want a link that can show me all the data related to b .

ANy suggestions

A: 

Have a look at InlineModelAdmin objects:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

Arnaud
inline will work but if data i much much high then it will take huge tiume to save it .Thus not a gud solution.It should be optional
ha22109
+1  A: 

When you create a relation, on the target object you automatically have a "set" member that allows you to walk the relationship backward (see django documentation).

With this in mind, you can use in your template something like:

{% for b in a.b_set %}
  <!-- display data related to b -->
{% endfor %}
Roberto Liffredo
i don't know it, but it will be helpful, thx, +1 :)
Rin
A: 

I don't know of a way to configure the admin to provide this for you, but you can fairly easily extend the admin templates for your model A and provide the links to model B's admin page yourself.

According to the instructions, you would do something similar to what I've described below.

Create a 'change_form.html' template using the following convention

[project]/
    templates/
        admin/
            [app name]/
                [model name]/
                    change_form.html

Then, the contents of your change_form, you can extend the default admin templates and customize for your needs. For example, display a list of "b" objects with a link to their admin page.

{% extends "admin/change_form.html" %}

{% block after_field_sets %}
<ul>
{% for b in a.b_set %}
    <li><a href="../../[model b]/{{ b.pk }}/">{{ b }}</a></li>
{% endfor %}
</ul>
{% endblock %}
Joe Holloway