E
E
enth2016-07-20 16:38:58
Django
enth, 2016-07-20 16:38:58

How to make compound object names in django admin?

Hello. There are 2 models:
models.py
Tables:

class Tables(models.Model):
    number = models.CharField(u"Номер_стола", max_length=100)
    ...

    def __str__(self):
        return "%s" % (self.number)

Orders:
class Orders(models.Model):
    tables = models.ForeignKey(Tables, on_delete=models.CASCADE)
    date_pub = models.DateTimeField(auto_now_add=True)
    ...

    def __str__(self):
        return "%s, %s" % (self.pk, self.date_pub)

In the Django admin, in the list of objects, tables will display the name (number) of the table. But in the list of Order objects, not very useful information will be displayed.
Tell me how to make it so that when the order object is displayed, the table number is also displayed in the title ? Those. how to refer to the table key in the __save__() method and display the name corresponding to this key from the tables table?
Or it's more convenient to do it in admins.py, but I don't know how either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
stayHARD, 2016-07-20
@stayHARD

class Orders(models.Model):
    tables = models.ForeignKey(Tables, on_delete=models.CASCADE)
    date_pub = models.DateTimeField(auto_now_add=True)
    ...

    def __str__(self):
        return "%s, %s, %s" % (self.pk, self.date_pub, self.tables.number)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question