Accessing a record from a sub-table in Django/Python

77 views
Skip to first unread message

Bruckner de Villiers

unread,
Dec 19, 2019, 6:44:01 AM12/19/19
to django...@googlegroups.com

I can’t seem to find a similar situation anywhere on the web.  I have two simple models “Organisation” & “Contact”.  Contacts belong to an organisation via a ForeignKey.  The ‘organisation_detail.html’ displays the details of both tables, per below:

 

 

I know it’s not pretty, yet.  The Contacts are displayed with href=”{{ contact.id }}/” to make them responsive and call an url for an update template.  The issue is that the url contains the pk’s of both the tables, e.g. http://127.0.0.1:8000/pbs1/11/8/.  The pk=11 belongs to the Organisation and pk=8 belongs to Contact.  If anyone has pointers to:

  • Stripping out the first pk, and
  • Calling the url - path('con_update/<int:pk>/',views.ContactUpdateView.as_view(), name='con_update'),

Relevant code included below:

 

urls.py:

app_name = 'pbs1'

 

urlpatterns = [

    #path('index', views.IndexView.as_view(), name='index'),

    # Organisation urls

    path('organisation_list.html', views.Org_View.as_view(), name='organisation_list'),

    path('create/', views.OrgCreateView.as_view(), name="create"),

    path('update/<int:pk>/', views.OrgUpdateView.as_view(), name='update'),

    path('delete/<int:pk>/', views.OrgDeleteView.as_view(), name='delete'),

    path('<int:pk>/', views.Org_DetailView.as_view(), name='detail'),

    # Contacts urls

    path('con_create/', views.ContactCreateView.as_view(), name="con_create"),

    path('con_update/<int:pk>/',views.ContactUpdateView.as_view(), name='con_update'),

    path('<int:pk>/', views.Contact_DetailView.as_view(), name='con_detail'),

 

models.py:

 

class Organisation(models.Model):

    MULTI_NATIONAL_CHOICES = [

        ('N', 'No'),

        ('Y', 'Yes'),

    ]

    org_Name = models.CharField(max_length=100,unique=True)

    org_Site = models.URLField(default="http://www.company.com", verbose_name="Org Web Site")

    org_HQ_Location = models.CharField(max_length=30)

    org_Sector = models.CharField(max_length=70, default="Investment Instruments - Equity Investment Instruments")

    currency_Code = models.ForeignKey(Currencie, related_name="cc", on_delete=models.CASCADE)

    org_Turnover = models.BigIntegerField(default=0)

    org_Parent = models.CharField(max_length=100)

    org_Multi_National = models.CharField(max_length=1, choices=MULTI_NATIONAL_CHOICES, default="No", verbose_name="Multi-National?")

    org_Comments = models.TextField()

    spec_Name = models.ForeignKey(SpecAccount, on_delete=models.CASCADE)

 

    def __str__(self):

        return self.org_Name

 

    def get_absolute_url(self):

        return reverse("pbs1:detail", kwargs={'pk':self.pk})

   

 

class Contact(models.Model):

    con_First_Name = models.CharField(max_length=64)

    con_Last_Name = models.CharField(max_length=64)

    con_Position = models.CharField(max_length=120)

    con_Address = models.EmailField(unique=True, help_text="Email")

    con_Land_Line = PhoneNumberField(region="ZA", default="+27211011001")

    con_Mobile = PhoneNumberField(region="ZA", default="+27831011001")

    con_Org = models.ForeignKey(Organisation, related_name="contact", on_delete=models.CASCADE)

 

    def __str__(self):

        return self.con_First_Name

 

    def get_absolute_url(self):

        return reverse("pbs1:detail", kwargs={'pk': self.con_Org.pk})

 

organisation_detail.html

{% extends 'pbs1/pbs1_base.html' %}

{% block body_block %}

<div class="jumbotron">

    <h4 id="pers">Organisation Details</h4>

    <pre>Name:                <strong>{{org_detail.org_Name}}</strong></pre>

    <pre>HQ Location:         <strong>{{org_detail.org_HQ_Location}}</strong></pre>

    <pre>Sector:              <strong>{{org_detail.org_Sector}}</strong></pre>

    <pre>Currency:            <strong>{{org_detail.currency_Code}}</strong></pre>

    <pre>Annual Turnover:     <strong>{{org_detail.currency_Code}} {{org_detail.org_Turnover}}</strong></pre>

    <pre>Multi-National?:     <strong>{{org_detail.org_Multi_National}}</strong></pre>

    <pre>Parent Organisation: <strong>{{org_detail.org_Parent}}</strong></pre>

    <pre>Web-Site:            <strong>{{org_detail.org_Site}}</strong></pre>

    <p>Account Classification: <span id="pers"><strong>{{org_detail.spec_Name}} Account</strong></span></p>

    <h5 id="pers">Contacts</h5>

        {% for contact in org_detail.contact.all %}

        <p><a href="{{contact.id}}/"> <span id="pers">{{contact.con_First_Name}} {{contact.con_Last_Name}}</span> |

        {{contact.con_Address}} | <span id="pers">{{contact.con_Position}}</span> | Mobile: {{contact.con_Mobile}} |

        Land Line: {{contact.con_Land_Line}}</a></p>

        {% endfor %}

</div>

<div class="container">

    <p><a class="btn btn-warning" href="{% url 'pbs1:update' pk=org_detail.pk %}">Update</a></p>

    <p><a class="btn btn-light" href="{% url 'pbs1:organisation_list' %}">Back</a></p>

</div>

{% endblock %}

 

 

Views.py:

# Contact Views *************************************

class Contact_DetailView(DetailView):

    context_object_name = "contact_detail"

    model = Contact

    template_name = 'pbs1/contact_detail.html'

    # Returns contact

 

class ContactCreateView(CreateView):

    fields = ('con_First_Name', 'con_Last_Name', 'con_Address',

              'con_Org', 'con_Land_Line', 'con_Mobile', 'con_Position')

    model = Contact

 

class ContactUpdateView(UpdateView):

    fields = ('con_First_Name', 'con_Last_Name', 'con_Address',

              'con_Land_Line', 'con_Mobile', 'con_Position')

    model = Contact

 

# Organisation Views *************************************

class Org_View(ListView):

    model = Organisation

    # Returns organisation_list

 

class Org_DetailView(DetailView):

    context_object_name = "org_detail"

    model = Organisation

    template_name = 'pbs1/organisation_detail.html'

    # Returns organisation

 

class OrgCreateView(CreateView):

    fields = ('org_Name', 'org_Site', 'org_HQ_Location', 'org_Sector',

              'currency_Code', 'org_Parent', 'org_Turnover', 'org_Multi_National', 'spec_Name', 'org_Comments')

    model = Organisation

 

class OrgUpdateView(UpdateView):

    fields = ('org_Name', 'org_Site', 'org_HQ_Location', 'org_Sector',

              'currency_Code', 'org_Parent', 'org_Turnover', 'org_Multi_National', 'spec_Name')

    model=Organisation

 

class OrgDeleteView(DeleteView):

    model = Organisation

    success_url = reverse_lazy("pbs1:organisation_list")

 

 

Eternally grateful,

Bruckner de Villiers

083 625 1086

Reply all
Reply to author
Forward
0 new messages