whoisearth
whoisearth

Reputation: 4160

django - legacy DB and inner joins

I have the following models -

class Schmst(models.Model):
    schmst_proddt = models.DateTimeField(primary_key=True)

    class Meta:
        managed = False
        db_table = 'schmst'

class Owner(models.Model):
    owner_id = models.IntegerField(primary_key=True)
    owner_name = models.CharField(max_length=30, blank=True)

    class Meta:
        managed = False
        db_table = 'owner'

class Jobmst(models.Model):
    jobmst_id = models.IntegerField(primary_key=True, db_column='jobmst_id', to_column='jobmst_id')
    jobmst_type = models.SmallIntegerField()
    jobmst_name = models.TextField(blank=True)

    class Meta:
        managed = False
        db_table = 'jobmst'

class Jobrun(models.Model):
    jobrun_id = models.IntegerField(primary_key=True)
    jobmst_id = models.ForeignKey(Jobmst, db_column='jobmst_id', to_field='jobmst_id')
    jobmst_type = models.SmallIntegerField(blank=True, null=True)
    jobrun_proddt = models.ForeignKey(Schmst, db_column='jobrun_proddt', to_field='schmst_proddt')
    jobrun_owner = models.ForeignKey(Owner, db_column='jobrun_owner', to_field='owner_id')

    class Meta:
        managed = False
        db_table = 'jobrun'

From there I have a view that doesn't seem to be doing any joining.

def hungjobs(request):
    template_vars['jobrun'] = Jobrun.objects.db_manager('Admiral').prefetch_related('jobmst_id__owner_id').filter(jobrun_status=51, jobrun_duration__gt=1800, jobmst_type__in=[2, 6])

    t = get_template('queries\HungJobs.html')
    c = Context(template_vars)

    return HttpResponse(t.render(c), mimetype="text/html")

The results from there I want to parse into my template below -

<table border="1">

<tr>
<td>Owner</td>
<td>Production Date</td>
<td>Job ID</td>
<td>Duration</td>
<td>Job Name</td>
</tr>
    {% for jobrun in jobrun %}
<tr>
<td>{{ jobrun.owner_name }}</td>
<td>{{ jobrun.jobrun_proddt|date:"M d, Y" }}</td>
<td>{{ jobrun.jobrun_id }}</td>
<td>{{ jobrun.jobrun_duration }}</td>
<td>{{ jobrun.jobmst_name }}</td>
</tr>
    {% endfor %}
</table>

Currently only the jobrun_id and jobrun_duration are loading into the template because the values are in the model I'm directly querying. The foreignkey relationships however don't seem to be working and I'm unsure why as from everything I've checked they're designed properly.

Upvotes: 0

Views: 42

Answers (1)

tr33hous
tr33hous

Reputation: 1642

I haven't ran your code but I see a few mistakes:

  1. For loop variable name should be changed [or you can change the template variable name] from jobrun to jobruns. It's not a good practice to reuse variable names this way.
  2. You need to specify what field of the foreign key needs to be printed in the template. Something like:

{{ jobrun.jobrun_proddt.schmst_proddt|date:"M d, Y" }}

for the date. As it stands right now, you are returning the objects and not a specific field of the foreign key objects.

Upvotes: 1

Related Questions