exrezzo
exrezzo

Reputation: 517

Customizing inline foreign key with no repetition

i have these models:

models.py

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,
    ]

When I create an offer, I can add as many ProductOfferDoc as I want because this is a foreign key, but I don't want to allow to insert the same ProductOfferDoc multiple times. Where can I make these controls over the form?

Upvotes: 1

Views: 50

Answers (1)

ndpu
ndpu

Reputation: 22571

You can use models.OneToOneField:

class ProductOfferDoc(BaseModel):
    # ...
    offer = models.OneToOneField(Offer, related_name='related_doc')

Upvotes: 1

Related Questions