E
E
EvgenToHelp2022-01-23 01:41:44
Django
EvgenToHelp, 2022-01-23 01:41:44

Setting relationships for an already existing db in Django?

There is a ready-made Sqlite database, you need to connect it to a Django project, I found how to create models using it , but I just can’t figure out how to register links between models. When I wrote in php I used yii, there the links were written in this form:

public function getCategory(){
    return $this->hasOne(Category::className(), ['id' => 'category_id']);
}

In Django, the relationship is set up like this:

company_that_makes_it = models.ForeignKey(
    Manufacturer,
    on_delete=models.CASCADE,
)

Is there any way to specify the fields that need to be linked?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-01-23
@bacon

Is there a way to add fields
Why do you want to do this, what problem do you want to solve?
1. For ForeignKey it is assumed that company_that_makes_it will correspond to the real field company_that_makes_it _id , you can also explicitly specify the field in the database through db_column.
2. ForeignKey will establish a relationship with the primary key Manufacturer, i.e. the field that you mark there like that (well, of course it should be in the database as well)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question