我已经阅读了有关InlineModelAdmin对象的信息,并试图实现它。基本上我有两个模型分别是contract和contractDetails。
class contracts(models.Model):
productDetailID=models.ForeignKey(productDetails,on_delete=models.CASCADE,verbose_name='Select Product')
supplierID=models.ForeignKey(suppliers,on_delete=models.CASCADE,verbose_name='Select Supplier')
totalUnits=models.IntegerField(verbose_name='Total Units',editable=False)
ratePerUnit=models.IntegerField(verbose_name='Rate Per Unit',editable=False)
saleTax=models.IntegerField(verbose_name='Sale Tax')
incomeTax=models.IntegerField(verbose_name='Income Tax')
saleTaxwithHeld=models.IntegerField(verbose_name='Sale Tax with Held')
startDate=models.DateField(verbose_name='Start Date')
endDate=models.DateField(verbose_name='End Date')
manulContractNumber=models.IntegerField(verbose_name='Manul Contract Number')
paymentDays=models.IntegerField(verbose_name='Payment Days')
remarks=models.CharField(verbose_name='Remarks',max_length=100,default=None)
dateOfEntry=models.DateField(editable=False,default=datetime.now())
class contractDetails(models.Model):
contractID=models.ForeignKey(contracts,on_delete=models.CASCADE,verbose_name='Select Contract')
weightPerBag=models.IntegerField(verbose_name='Weight Per Bag')
conesPerBag=models.IntegerField(verbose_name='Cones Per Bag')
weightPerCone=models.IntegerField(verbose_name='Weight Per Cone')
noOfBags=models.IntegerField(verbose_name='No of Bags')
noOfAdditional=models.IntegerField(verbose_name='No of Additional Cones')
ContractDetails的contractID为foregnkey。现在,我尝试使用InlineModelAdmin从这两个模型创建一种表单。
from django.contrib import admin
from Purchase.models import suppliers,Contacts,contracts,contractDetails
class contractsInline(admin.TabularInline):
model = contracts
class contractsdetailsInline(admin.TabularInline):
model = contractDetails
class contractsAdmin(admin.ModelAdmin):
inlines = [
contractsInline,
contractsdetailsInline
]
# Register your models here.
admin.site.register(suppliers)
admin.site.register(Contacts)
admin.site.register(contracts,contractDetails)
admin.site.register(contractsAdmin)
但是当我尝试迁移它时,它总是返回错误
TypeError:“ MediaDefiningClass”对象不可迭代
答案 0 :(得分:0)
我在注册时犯了错误。我必须先注册合同 模型,然后执行ContractAdmin模型管理。
from django.contrib import admin
from Purchase.models import suppliers,Contacts,contracts,contractDetails
admin.site.register(suppliers)
admin.site.register(Contacts)
class contractDetailsInline(admin.TabularInline):
model = contractDetails
extra = 1
fields = ["weightPerBag", "conesPerBag", "weightPerCone", 'noOfBags','noOfAdditional']
class contractsAdmin(admin.ModelAdmin):
save_on_top = True
fields = ["productDetailID",'supplierID','saleTax','incomeTax','saleTaxwithHeld','startDate','endDate','manulContractNumber','paymentDays','remarks']
inlines = [contractDetailsInline,]
# Register your models here.
admin.site.register(contracts,contractsAdmin)