ShayestehHS / DjangoUltimateURLShortener

0 stars 1 forks source link

Create suggestion choice in django panel admin #7

Open ShayestehHS opened 2 months ago

ShayestehHS commented 2 months ago

admin.py

from django import forms
from django.contrib import admin
from .models import Product, Category

class ProductAdminForm(forms.ModelForm):
    category = forms.ChoiceField(choices=[], label="Category")  # Initialize with empty choices

    class Meta:
        model = Product
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Dynamically set choices from the database
        self.fields['category'].choices = Product.get_category_choices()

class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm

admin.site.register(Category)
admin.site.register(Product, ProductAdmin)
hamid-kordi commented 1 month ago

Hi, if you haven't solved it yet, maybe I can help you

ShayestehHS commented 1 month ago

Thanks for your suggestion! To move forward, we'll need to update the Manager of the URL model to accept the token from the input. Feel free to continue from there!