Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid duplicate models in django project?

i'm learning django so i've many questions, and one is how i can reuse a model? i mean the models live in the application folder, but some models are exactly the same between two differents applications. So should i rewrite the model every time that i write a new app?

like image 851
Agusti-N Avatar asked Dec 05 '25 07:12

Agusti-N


2 Answers

Yes, this is wrong when you have the same names of yours apps You also can use abstract models


class CommonInfo(models.Model):
    name = models.CharField(max_length=100)
    age = models.PositiveIntegerField()

    class Meta:
        abstract = True

class Student(CommonInfo):
    home_group = models.CharField(max_length=5)

like image 55
rootart Avatar answered Dec 07 '25 21:12

rootart


If your models are exactly the same in different applications, you're doing something wrong. Don't forget that an application is basically just a set of models, and you can use one application's models within another application just by importing them.

Can you give an example of two applications with exactly the same models?

like image 20
Daniel Roseman Avatar answered Dec 07 '25 19:12

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!