I am trying to use django admin board functionality to be able to insert data within admin panel into tables with one-to-multiple and multiple-to-multiple relations. I used __str__ functionality (Python 3.6) in order to create understandable strings in admin panel instead of "Testcase object(1)" like description to be able to select proper testcase id and testrun id for executedtests model. But it looks like admin panel during executedtest object creation instead of testcase id value and testrun id value tries to set foreign int keys to a corresponding text value.
I miserably fail with error:
tc = Testcases.objects.get(pk=self.testcase_id)
...
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Testcases'
Models:
class Testcases(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return f"Testcase: {self.name}"
class Testruns(models.Model):
starttime = models.TimeField()
endtime = models.TimeField()
def __str__(self):
return f"Testrun: {self.starttime} - {self.endtime}"
class Executedtests(models.Model):
testcase_id = models.ForeignKey("testcases", models.CASCADE)
testrun_id = models.ForeignKey("testruns", models.CASCADE)
teststart = models.TimeField(blank=True, null=True) # This field type is a guess.
testend = models.TimeField(blank=True, null=True) # This field type is a guess.
def __str__(self):
tc = Testcases.objects.get(pk=self.testcase_id)
tr = Testruns.objects.get(pk=self.testrun_id)
return f"{tc}(tr), Start: {self.teststart}, End: {self.testend}"
Inside app admin.py I just register all those models.
The select tag of the admin form looks ok though:
<select id="id_testcase_id" name="testcase_id">
<option value="1">Testcase: Test1</option>
...
</select>
testcase_id(and others with_id), but the_idis automatically generated.