'Showing Operation object (1) when trying to get foreignkey Django
I am trying to post a table in HTML and I get This "Operation object (1)" instead of just the id "1" How is possible to fix this
as you can see in the picture where is selected I want to have the id, not text models.py
class Operation(models.Model):
operationID = models.IntegerField(primary_key=True)
assignee = models.ForeignKey('Employee', on_delete=models.CASCADE)
dateRegistered = models.DateTimeField(auto_now_add=True)
timeStart = models.DateTimeField(auto_now_add=True, null=True)
timeFinish = models.DateTimeField(null=True)
status = models.ForeignKey(
'Status', on_delete=models.CASCADE)
class Subtask(models.Model):
subtaskID = models.IntegerField(primary_key=True, auto_created=True)
operationID = models.ForeignKey('Operation', on_delete=models.CASCADE)
containerID = models.CharField(max_length=255)
containerWeightT = models.DecimalField(max_digits=6, decimal_places=2)
loadSeq = models.IntegerField()
moveTo = models.ForeignKey('MoveTo', on_delete=models.CASCADE)
stow = models.ForeignKey('Stow', on_delete=models.CASCADE)
status = models.ForeignKey(
'Status', on_delete=models.CASCADE)
views.py
def displaydata(request):
results1 = Subtask.objects.prefetch_related(
'moveTo', 'operationID', 'stow', 'status').all()
return render(request, 'ee.html', {'Subtask': results1})
This is my HTML code to post the data from the database. I just posted where the table is and removed lines that contains CSS/JS
<table id="blbbl" class="display">
<thead>
<tr>
<th class="">ID</th>
<th class="">OperationID</th>
<th class="">ContinerID</th>
<th class="">Weight</th>
<th class="">LoadSeq</th>
<th class="">MoveTo</th>
<th class="">Stow</th>
<th class="">Status</th>
<th class="">Edit</th>
<th class="">Delete</th>
<th class="">Add New</th>
</tr>
</thead>
<tbody>
{% for Subtask in Subtask %}
<tr>
<td class="">{{Subtask.subtaskID}}</td>
<td class="">{{Subtask.operationID}}</td>
<td class="">{{Subtask.containerID}}</td>
<td class="">{{Subtask.containerWeightT}}</td>
<td class="">{{Subtask.loadSeq}}</td>
<td class="">{{Subtask.moveTo}}</td>
<td class="">{{Subtask.stow}}</td>
<td class="">{{Subtask.status}}</td>
<td>
<a href="{% url 'updatesubtask' Subtask.subtaskID %}" type="button" class="btn btn-secondary">Edit</a>
</td>
<td>
<a href="{% url 'deletesubtask' Subtask.subtaskID %}" type="button" class="btn btn-danger">Delete</a>
</td>
<td>
<a href="{% url 'addsubtask' %}" role="button"><i class="fa fa-plus" style="color: green;"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
Solution 1:[1]
Ins?de your Operation model add a int* function:
class Operation(models.Model):
...
...
...
def __int__(self):
return self.operationID
*: int not str
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
