Skip to content

Commit

Permalink
Refactor ProductAdmin and ProductCategory model for improved clarity
Browse files Browse the repository at this point in the history
- Commented out the formfield_for_foreignkey method in ProductAdmin to simplify the code and prepare for future enhancements.
- Updated the __str__ method in ProductCategory to include the associated business name, improving the representation of product categories in the admin interface.

These changes enhance code maintainability and provide better context in the admin display.
  • Loading branch information
UnCubanoDev committed Dec 22, 2024
1 parent 66de710 commit 29b5921
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ class ProductAdmin(admin.ModelAdmin):
]
list_filter = ['is_active']

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "category":
if request.GET.get('restaurant'):
kwargs["queryset"] = ProductCategory.objects.filter(business_id=request.GET.get('restaurant'))
else:
kwargs["queryset"] = ProductCategory.objects.none()
return super().formfield_for_foreignkey(db_field, request, **kwargs)
# def formfield_for_foreignkey(self, db_field, request, **kwargs):
# if db_field.name == "category":
# restaurant_id = request.GET.get('restaurant_id')
# if restaurant_id:
# kwargs["queryset"] = ProductCategory.objects.filter(business_id=restaurant_id)
# else:
# kwargs["queryset"] = ProductCategory.objects.none()
# return super().formfield_for_foreignkey(db_field, request, **kwargs)


@admin.register(RestaurantRating)
Expand Down
2 changes: 1 addition & 1 deletion api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Meta:
verbose_name_plural = _("product categories")

def __str__(self):
return self.name
return f"{self.name} - {self.business.name}"

def get_absolute_url(self):
return reverse("productcategory_detail", kwargs={"pk": self.pk})
Expand Down

0 comments on commit 29b5921

Please sign in to comment.