1👍
✅
You can use a concept in Django called InlineModelAdmin
[django-docs] and in your case, your admin.py
module can be as below:
from django.contrib import admin
from your_app_name.models import Product, HeadFlowDataSet
class HeadFlowDatasetInline(admin.TabularInline):
model = HeadFlowDataset
extra = 1
class ProductAdmin(admin.ModelAdmin):
list_display = (
"pump_name",
"main_model",
"usage",
"pump_type",
)
search_fields = (
"pump_name",
"main_model__model_name_fa",
"usage__usage_name_fa",
"pump_type__type_name",
)
fields = (
"main_model",
"usage",
"sub_usage",
"pump_type",
"pump_name",
"pump_image",
"NPSH_diagram_image",
"power_diagram_image",
"max_head",
"max_flow",
"motor_ph",
"motor_rpm",
"pump_technical_datasheet_fa",
"pump_technical_datasheet_en",
)
inlines = [HeadFlowDatasetInline]
def save_model(self, request, obj, form, change):
obj.created_by = request.user
obj.last_updated_by = request.user
obj.save()
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
instance.user = request.user
instance.save()
With the above code snippet, you have an inline part with one set of HeadFlowDataset
objects on each page of your Product
models in your Django admin panel which can be added on need form that panel and with this approach, this issue will be resolved.
Source:stackexchange.com