Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: How do I pass different urls to a generic view?
    primarykey
    data
    text
    <p>I'm building an app that is similar to an address book. For one of my views, I have a list of Individuals. Each <code>Individual</code> has a <code>Role</code> which links them to their job title and employer. There are several different types of employers so this is a generic relation.</p> <p>Using the ListView generic view, I have created a list of individuals that displays their name, their job title and their employer, i.e.</p> <blockquote> <p>"Jimmy, Officer, Chase Bank"</p> <p>"Mary, CEO, General Hospital"</p> </blockquote> <p>I need the names of the companies to be links that go to the companies detail page. </p> <p><strong>The Problem</strong></p> <p>Since I have one template for this list, I am having a hard time understanding how to change the url based on what type of employer the individual has i.e. Bank or Hospital. </p> <p><strong>What Might Work</strong></p> <ol> <li><p>Should I write a function for the view that takes the employer content type and creates a variable? Or should I put the function under the <code>Role</code> model? I am a noob to python/Django and am not sure how to even write this. </p></li> <li><p>I attempted to use if/else statements in the template, i.e. if bank, use this url, but it did not work. Probably because it is a generic relation and my query was not right.</p></li> </ol> <p>In response to the comment below:</p> <p>I'm not sure exactly what you meant by inheritance structures but here are a simplified version of my models;</p> <pre><code>class Individual(models.Model) name = models.CharField(max_length=30) class Bank(models.Model): name = models.CharField(max_length=30) staff = generic.GenericRelation('Role', content_type_field='employer_content_type', object_id_field='employer_id' ) class Hospital(models.Model): name = models.CharField(max_length=30) staff = generic.GenericRelation('Role', content_type_field='employer_content_type', object_id_field='employer_id' ) ... and so on for each different employer class Role(models.Model): title = models.CharField(max_length=70) job_holder = models.ForeignKey(Individual) employer_content_type = models.ForeignKey(ContentType, limit_choices_to={"model__in": ('venue', 'festival', 'artsorganization','bookingagent', 'managementcompany', 'mediaoutlet', 'otherorganization', 'presentercompany', 'publishingcompany', 'presenter', 'recordcompany', 'musician', 'ensemble')}, related_name="employer") employer_id = models.PositiveIntegerField() employer = generic.GenericForeignKey('employer_content_type', 'employer_id') </code></pre> <p>At one point I had all employers inheriting from a Company model. I changed it because each employer has different attributes which was making things very complicated. </p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload