Использование related_name вместо name.Class python Django(related_name=favorites)
Мой рецензент хочет, чтобы я использовал related_name вместо ссылки на модель класса! `
@admin.register(Recipe)
class RecipeAdmin(admin.ModelAdmin):
list_display = ['id', 'name', 'author', 'favorites']
search_fields = ['name', 'author__username']
list_filter = ['tags']
empty_value_display = EMPTY
inlines = (
IngredientsInLine,
)
def favorites(self, obj):
if Favorite.objects.filter(recipe=obj).exists():
return Favorite.objects.filter(recipe=obj).count()
return 0
``
Я сделал следующее: `
def favorites(self, obj):
recipe = obj['recipe']
if obj.favorites(recipe=recipe).exists():
return obj.favorites(recipe=recipe).count()
return 0
Но я не знаю, правильно ли это.