How to avoid duplication of models in a django project?

I am learning django, so I have a lot of questions, and one of them is how can I reuse the model? I mean, the models live in the application folder, but some models exactly match between two different applications. So should I rewrite the model every time I write a new application?

+3
source share
3 answers

Yes, it’s wrong if you have the same names for your applications. You can also 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)

+3
source

, - . , - , , .

?

+3

How to reuse a model .

The best way to reuse a model is to inherit the model's parent class. Here's how you should do it. Inherited from models.Model.

from django.db import models
class trial(models.Model):
    # override the parent class methods here or define your own

Also make sure your application models importare in the appropriate models.py file.

0
source

Source: https://habr.com/ru/post/1769148/


All Articles