1👍
So it seems like there’s a couple things to solve: #1 is the object creation process, and #2 is showing ProductSpecificationValues
that are associated with the Product
‘s ProductType
.
#1: Don’t show the ProductSpecificationValueInline
when the Product is being created. The user should only be able to set this value after they have created the product and set the ProductType.
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
inlines = [
ProductSpecificationValueInline,
]
def get_inlines(self, request, obj):
if not obj.id or not obj.product_type:
return [] # ... then don't show any inlines
return self.inlines
#2: The ProductSpecificationValueInline
should only show specification values that are associated with the Product type.
class ProductSpecificationValueInline(admin.TabularInline):
model = ProductSpecificationValue
def get_formset(self, request, obj=None, **kwargs):
inline_formset = super().get_formset(request, obj, **kwargs)
# I don't know what you named your field(s), but this illustrates the concept
# of changing the queryset that is used to populate the choices for this field
qs = inline_formset.base_fields['product_specification'].queryset
qs = qs.filter(product_type=obj.product_type)
inline_formset.base_fields['product_specification'].queryset = qs
return inline_formset
Obviously this might not line up 100% with your models (for example, are you using a ManyToMany field for the Product’s ProductSpecification values? And what are the field names in the models?), but the general principle remains the same: in your inline class, you would override the get_formset
method and alter the queryset for the form field(s) in question.
Glad to help you get a little closer if needed, but would need to see the code for your models.