Home > Blockchain >  Get backward relationships items for model in Django
Get backward relationships items for model in Django

Time:11-07

I have a pet project with reviews about spare parts for cars.

Here is models.py:

class CarBrand(models.Model):
brand = models.CharField(max_length=40, choices=(), unique=True, verbose_name="Марка")

def __str__(self):
    return self.brand

def get_absolute_url(self):
    return reverse_lazy('car_models_all', kwargs={'car_id': self.pk})


class CarModel(models.Model):
    model_name = models.CharField(max_length=60, db_index=True, verbose_name="Модель")
    brand_id = models.SmallIntegerField(verbose_name="ID марки")

def __str__(self):
    return self.model_name

def get_absolute_url(self):
    return reverse_lazy('model_info', kwargs={'model_id': self.pk})


class SparePartCategory(models.Model):
    name = models.CharField(max_length=255, db_index=True, verbose_name='Название')

def __str__(self):
    return self.name

def get_absolute_url(self):
    return reverse_lazy('spare_parts_category', kwargs={'category_id': self.pk})


class SparePart(models.Model):
    name = models.CharField(max_length=255, db_index=True, verbose_name='Название')
    brand = models.CharField(max_length=255, db_index=True, verbose_name="Производитель")
    number = models.CharField(max_length=30, db_index=True, verbose_name="Номер (артикул)")
    category = models.ForeignKey(SparePartCategory, on_delete=models.PROTECT, verbose_name="Категория")

def __str__(self):
    return ' '.join([self.name, self.brand, self.number])

def get_absolute_url(self):
    return reverse_lazy('get_spare_part', kwargs={'spare_part_id': self.pk})


class Review(models.Model):
    RATING_VALUES = [
    ('1', 'Ужасно'), ('2', 'Плохо'), ('3', 'Сносно'), ('4', 'Хорошо'), ('5', 'Отлично'),
]
    spare_part = models.ForeignKey(SparePart, on_delete=models.PROTECT, verbose_name="Запчасть")
    mileage = models.SmallIntegerField(verbose_name="Пробег, тыс.км")
    car_brand = models.ForeignKey(CarBrand, on_delete=models.PROTECT, verbose_name="Марка авто")
    car_model = models.ForeignKey(CarModel, on_delete=models.PROTECT, verbose_name="Модель авто")
    owner = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Владелец")
    rating = models.CharField(max_length=1, choices=RATING_VALUES, verbose_name="Рейтинг", default=3)
    review = models.TextField(max_length=1000, blank=True, verbose_name="Отзыв")

def __str__(self):
    return ' '.join([self.spare_part.name, self.spare_part.brand, self.spare_part.number])

In views.py I get one SparePart

def get_spare_part(request, spare_part_id):
    spare_part = get_object_or_404(SparePart, pk=spare_part_id)

    installed_on_cars = # here I need to get all CarModel for current SparePart with Review exists
    context = {
    'spare_part': spare_part,
    'installed_on_cars': installed_on_cars,
}
return render(request, 'mileage/spare_part.html', context)

I need to get all CarBrand and CarModel for current SparePart with Review exists, and get hyperlinks for template for them. How do I can to implement this? I think I need to make it with backward relationships, but can not understand how.

CodePudding user response:

Use the _set manager object (documentation here).

for review in spare_part.review_set.all():
    print(f'brand: {review.care_brand}')
    print(f'model: {review.car_model}')

If you define a related_name (e.g. 'reviews) for spare_parts in Review, you can use that name for backward query instead of _set (e.g. spare_part.reviews.all()).

  • Related