我有这些模特:
class Offer(BaseModel):
code = models.CharField(_("Codice Offerta"), max_length=16, blank=False, null=False, default=0, editable=False)
company = models.ForeignKey(Company, verbose_name=_('Azienda'), related_name='company')
outcome = models.CharField(_('Esito Offerta'), choices=OUTCOME, max_length=64, blank=True, null=True)
user = models.CharField(_("Inserita da"), max_length=64, blank=False, null=True)
class Product(BaseModel):
name = models.CharField(_("Nome Prodotto"),max_length=1024, blank = False, null=True)
category = models.CharField(_("Categoria"),max_length=1024, blank = False, null=True, choices=CATEGORY)
class ProductOfferDoc(BaseModel):
product = models.CharField(max_length=1024, blank=False,null=False, choices=DOC)
number = models.IntegerField(_('Num.'), default=0, blank=True, null=True)
price = models.DecimalField(_('Prezzo'),max_digits=10, decimal_places=2,default=0.00,blank=True, null=True )
offer = models.ForeignKey(Offer, related_name='related_doc')
admin.py
class DocAdmin(admin.StackedInline):
extra = 1
model = ProductOfferDoc
class OfferAdmin(admin.ModelAdmin):
model = Offer
list_display = ['code','company']
inlines = [
DocAdmin,
CourseAdmin,
RiskAdmin,
ServiceAdmin,
SanitaryAdmin,
]
当我创建商品时,我可以添加尽可能多的ProductOfferDoc
,因为这是一个外键,但我不想允许多次插入相同的ProductOfferDoc
。我在哪里可以对表单进行这些控制?
答案 0 :(得分:1)
您可以使用models.OneToOneField
:
class ProductOfferDoc(BaseModel):
# ...
offer = models.OneToOneField(Offer, related_name='related_doc')