K
K
Kaiser132021-03-02 00:28:13
Django
Kaiser13, 2021-03-02 00:28:13

How to get adjacent values ​​stored in the database in Django?

Hello, dear forum users. Please help me figure it out.

How to get the values ​​of several fields from the model connected via ForeignKey in the "views.py" file?

For example, let's take 2 models with parameters:

class Box(models.Model):
    name = models.CharField(max_length=100)
    length = models.PositiveSmallIntegerField()
    width = models.PositiveSmallIntegerField()
    height = models.PositiveSmallIntegerField()
    
    def __str__(self):
        return str(self.name)

class Material(models.Model):
    name = models.CharField(max_length=100)
    price = models.FloatField()
    density = models.FloatField()
    
    def __str__(self):
        return str(self.name)


And the object model:

class Product(models.Model):
    box = models.ForeignKey(Box, on_delete=models.CASCADE)
    material = models.ForeignKey(Material, on_delete=models.CASCADE)
    
    def __str__(self):
        return str(self.box)


How can Django calculate cost, volume, or other parameters?
How to get the values ​​written in "Box.length", "Box.width", "Box.height" corresponding to the selected "Box.name" in views.py?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question