简体   繁体   中英

Using two models on the same Admin page

I'd been searching two days how to make it, and the other question I found are not my problem. I have 3 models:

class ProductBase(models.Model):
    title = models.CharField(max_length=50) # and some code
class Serie(models.Model):
    #some specific fields
    productbase = models.OneToOneField(ProductBase, on_delete=models.CASCADE)
class Movie(models.Model):
    #some specific fields
    productbase = models.OneToOneField(ProductBase, on_delete=models.CASCADE)

The Admin shows me a Create/Edit Form for the ProductBase Model, one for the Serie Model, and one for the Movie Model. But I want to show a Create/Edit Form for Serie/ProductBase Model, and one for Movie/ProductBase Model, so creating a serie, I create a ProductBase and a Serie Model, and a relations between them. I thoght using Inlines but, that's for de ProductBase and I can't regsiter the ProductBAse two times with different AdminModels and Inlines. The other way is breaking the ProductBase in the two models Serie and Movie, but that is a bad design. Thanks for your help Sorry for my English

I think abstract models is what you are looking for.

For example:

class AbstractProductBase(models.Model):
    title = models.CharField(max_length=50)

    class Meta:
        abstract = True


class Serie(AbstractProductBase):
     # some specific fields


class Movie(AbstractProductBase):
    # some specific fields

Source: https://docs.djangoproject.com/en/1.9/topics/db/models/#abstract-base-classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM