Multiple nested foreign key relationships in Django


No

I'm trying to get an object that traverses 2 models that have a reverse foreign key relationship. I've been able to get the object of the first model (ipv4address), but not able to get the object linked to it with a reverse foreign key. I'm new to django, so I might get this kind of error at first. Any suggestions are welcome.

I pass all peer objects to the template like this:

    def view_ix(request, ix):
            peers = Peer.objects.filter(exchange=ix).order_by('asn')
            return render_to_response('view_ix.html', {
                                                    'peers': peers,
                                                    'exchange' : ix,
                                                    'exchanges' : exchanges.list(),
                                            }, context_instance = RequestContext(request))

role model:

    class Peer(models.Model):
            exchange                = models.CharField(max_length=50)
            organisation            = models.CharField(max_length=200)
            contact                 = models.EmailField(max_length=254)
            asn                     = models.IntegerField()
            def __unicode__(self):
                    return  unicode(self.asn) + '\t' + unicode(self.organisation)

    class IPv4Address(models.Model):
            peer                    = models.ForeignKey(Peer)
            ip                      = models.GenericIPAddressField(protocol='IPv4')
            def __unicode__(self):
                    return  unicode(self.peer) + '\t' + unicode(self.ip)

    class Router(models.Model):
            name            = models.CharField(max_length=50)
            fqdn            = models.CharField(max_length=50)
            snmpport        = models.IntegerField()
            snmpcommunity   = models.CharField(max_length=50)
            def __unicode__(self):
                    return  unicode(self.name)

    class PeerStatus(models.Model):
            router          = models.ForeignKey(Router)
            ipv4address     = models.ForeignKey(IPv4Address)
            state           = models.IntegerField()

template:

    {% for peer in peers %}
            {% for ipv4 in peer.ipv4address_set.all %}
                ipv4: {{ ipv4.ip }}<br>
                {% for state in ipv4.peerstatus_set.all %}
                    state: {{ state.state }}<br>
                {% endfor %}
            {% endfor %}
    {% endfor %}
No

Actually works as designed... The problem is in the (missing) database content.

Related


Django foreign key relationships

mohit hassan model.py class Hub(models.Model): name = models.CharField(max_length=150) def __str__(self): return self.name class User(AbstractUser): is_client = models.BooleanField(default=False) is_trainer = models.BooleanField(defa

Django foreign key relationships

Jatlind I'm trying to create a relationship between two tables Registered_Users and User_Connections so that those who register can add others (more like adding friends) to their connection list. Here is the content from models.py: class Registered_Users(model

Django foreign key relationships

Jatlind I'm trying to create a relationship between two tables Registered_Users and User_Connections so that those who register can add others (more like adding friends) to their connection list. Here is the content from models.py: class Registered_Users(model

Nested Json for Django multiple foreign key models

Mehmet Ince I have 4 models with relationships via FK. class Journal(models.Model): name = models.CharField(max_length=255) class Volume(models.Model): journal = models.ForeignKey(Journal, related_name='volumes') number = models.IntegerField() cl

Nested Json for Django multiple foreign key models

Mehmet Ince I have 4 models with relationships via FK. class Journal(models.Model): name = models.CharField(max_length=255) class Volume(models.Model): journal = models.ForeignKey(Journal, related_name='volumes') number = models.IntegerField() cl

How to add all foreign key relationships in Django?

Zach I have these two models: class Country(models.Model): Name = models.CharField(max_length=100) Population = models.CharField(max_length=100) Language = models.IntegerField() Total_persons= models.IntegerField() class Person(models.Model):

Understanding Django foreign key relationships in inline formsets

feasting I'm building a simple application around firewall policy change requests. I'm having a hard time understanding how I should set up my models and forms so that all the rule requests can be made in one view. Here is a simplified version of my model and

How to add all foreign key relationships in Django?

Zach I have these two models: class Country(models.Model): Name = models.CharField(max_length=100) Population = models.CharField(max_length=100) Language = models.IntegerField() Total_persons= models.IntegerField() class Person(models.Model):

Django filter foreign key relationships by user group

Foxtail algae I'm trying to filter activities in a task (activity list) by assigned user group. TaskActivityModel.objects.filter(activity__workCenter=request.user.groups) this gives me aTypeError: Field 'id' expected a number When I modify the filter paramete

How to render foreign key relationships in Django

Jonas I have a simple many-to-one relationship where users can post comments to a poll object. review model class Comments(models.Model): poller = models.ForeignKey(Pollers, on_delete=models.CASCADE, related_name='comments') created_on = models.DateTim

How to add all foreign key relationships in Django?

Zach I have these two models: class Country(models.Model): Name = models.CharField(max_length=100) Population = models.CharField(max_length=100) Language = models.IntegerField() Total_persons= models.IntegerField() class Person(models.Model):

Key and foreign key relationships

James Wilson Table 1 I have a table with all my dealers and roadshows on it. I'm trying to add the ability to add events at these locations. This table has location_id. For this, I created another table. Table 2 In this new table, I have an ID and a event_loca

Django multiple foreign key models

Andre Consart Here is my code, is there a more efficient way to write it? I do not like it. Basically, both the company model and the supplier model should be able to connect with multiple phone numbers. class Contact(models.Model): company = models.Foreig

Django multiple foreign key models

Andre Consart Here is my code, is there a more efficient way to write it? I do not like it. Basically, both the company model and the supplier model should be able to connect with multiple phone numbers. class Contact(models.Model): company = models.Foreig

Cardinality and foreign key relationships

OA I'm new to database design and trying to understand best practices for using foreign keys. I know that when you have a 1:m relationship, we don't have to create a relationship for that relationship. Instead, we can add a foreign key on the m side of the rel

Flip foreign key relationships?

Cyber I have a strange effect in am:n relationship.. here is the model definition: example: 'use strict'; module.exports = (sequelize, DataTypes) => { const Role = sequelize.define('Role', { uuid: { allowNull: false, primaryKey: true, u

Cardinality and foreign key relationships

OA I'm new to database design and trying to understand best practices for using foreign keys. I know that when you have a 1:m relationship, we don't have to create a relationship for that relationship. Instead, we can add a foreign key on the m side of the rel

Cardinality and foreign key relationships

OA I'm new to database design and trying to understand best practices for using foreign keys. I know that when you have a 1:m relationship, we don't have to create a relationship for that relationship. Instead, we can add a foreign key on the m side of the rel

Querying Django to model two levels of foreign key relationships

User 9931820 I am building a dictionary app with Django. The main models for this app are Expressions, Definitions and Citys. The logic is that each Expressionhas one or more Definitions, and each Definitionis associated with a 1 City. Here's my problem : I wa

Querying Django to model two levels of foreign key relationships

User 9931820 I am building a dictionary app with Django. The main models for this app are Expressions, Definitions and Citys. The logic is that each Expressionhas one or more Definitions, and each Definitionis associated with a 1 City. Here's my problem : I wa