Answer the question
In order to leave comments, you need to log in
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)
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question