D
D
Daniil Chernov2020-12-21 22:58:49
Django
Daniil Chernov, 2020-12-21 22:58:49

How to link to foreignkey django?

Let's say there are two models.

class A(models.Model):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class B(models.Model):
    name = models.CharField(max_length=200)
    modela = models.ForeignKey(A, on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.name


How to make sure that in the admin panel, namely on the page with the list of model B, there is a link to the associated object of model A? That is, what would be in the modela column, modela.__str__ would be displayed (as it usually happens), only in blue and when clicked, it would go to the page for editing the modela object.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-12-30
@tumbler

By hand, through the admin method (for example, def render_my_foreign_key_link(self, obj)).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question