📜 ⬆️ ⬇️

Translation Django Documentation: Models. Part 2

image

Good day!

This topic is a continuation of the translation of the Django documentation , to be exact - the section on models .
')
Translation Django Documentation: Models. Part 1

_____ Relationship between models
_______ Many-to-one ratio
_______ Many-to-many relationship
_______ Additional fields for many-to-many
_______ One-to-one ratio
_____ Models and Files
_____ Restrictions on field names
_____ Own field types

Translation Django Documentation: Models. Part 3
Translation Django Documentation: Models. Part 4 (Last)



Relationships between models


Obviously, the main advantage of relational databases is the ability to define relationships between tables. Django provides options for defining the three most common types of relationships: many-to-one, many-to-many, and one-to-one.



Many-to-one relationship

ForeignKey is used to define this kind of relationship. Its use is no different from using other types of fields: just add the corresponding attribute to your model.

When creating a field using the ForeignKey , the method of the same name should be passed one positional argument: the class to which your model will refer.

For example, if a car ( Car ) has a manufacturer ( Manufacturer ) (a manufacturer creates a large number of machines, however each machine has only one manufacturer), the following definition is used:
Copy Source | Copy HTML<br/> class Manufacturer (models.Model):<br/> # ... <br/> <br/> class Car (models.Model):<br/> manufacturer = models.ForeignKey( Manufacturer )<br/> # ... <br/>

You can also create recursive relationships (an object with a many-to-one relationship that refers to itself) and relationships with a model that is not yet defined ; more detail here .

As a field name of type ForeignKey ( manufacturer in the example above), we suggest using the name of the model referenced by your class in lower case. This is an optional requirement and you can name the field as you like. For example:
Copy Source | Copy HTML<br/> class Car (models.Model):<br/> company_that_makes_it = models.ForeignKey(Manufacturer)<br/> # ... <br/>


image
A more detailed example you can find here is a many-to-one relationship: an example

Fields of type ForeignKey can also have a number of additional arguments that are considered in the description of the fields of models . These optional parameters allow you to more accurately determine the work relationship.



Many attitude to many

To determine this type of relationship, ManyToManyField is used. Its use is no different from using other types of fields: just add the corresponding attribute to your model.

When creating a field using ManyToManyField , one positional argument should be passed to the method of the same name: the class to which your model will refer.

For example, if a pizza ( Pizza ) has a topping (one pizza can have many toppings, which in turn can be contained in many pizzas), you can imagine it this way:
Copy Source | Copy HTML<br/> class Topping (models.Model):<br/> # ... <br/> <br/> class Pizza (models.Model):<br/> # ... <br/> toppings = models.ManyToManyField( Topping ) <br/>

As with the ForeignKey , you can create recursive relationships (an object with a many-to-one relationship that refers to itself) and relationships with a model that is not yet defined ; more detail here .

As a field name of the ManyToManyField type ( toppings in the example above), we suggest using a plural noun that describes many related objects.

It does not matter which model has an attribute of the ManyToManyField type, the main thing is that it should be specified only in one of them.

As a rule, a field of type ManyToManyField is defined in the object, which will later change in the administrator interface if you use the built-in Django admin panel (sorry, slang or tautology). In the example above, the toppings are on pizza ( Pizza ), because it’s more usual for us to imagine pizza having different toppings than the stuffing contained on many pizzas. If you describe the model as shown above, the Pizza form will allow users to select toppings.

image
A more detailed example you can find here is a many-to-many relationship: an example

Fields of the ManyToManyField type may also have a number of additional arguments that are considered in the description of the fields of the models . These optional parameters allow you to more accurately determine the work relationship.



Additional fields for many-to-many
Added in Django version 1.0 : please read the release notes .

As long as you are dealing with many-to-many trivial relationships, such as combining and matching pizzas and toppings, all you need is a standard type ManyToManyField . However, sometimes you may need to associate data using a relationship between two models.

For example, consider the case when an application tracks groups in which some musicians took part. The relationship between the musician and the groups he or she is a member of will be many-to-many, so you can use ManyToManyField to present it . However, you may also be interested in a large number of details, such as the date of joining the group or the reason why the musician left it.

For these situations, Django provides you with the ability to define a separate (intermediate) model that will be used to manage many-to-many relationships. You can create additional fields in the intermediate model. The intermediate model interacts with the ManyToManyField using the through argument, which acts as an intermediary. For our musical (:)) example, the code will look like this:
Copy Source | Copy HTML<br/> class Person (models.Model):<br/> name = models.CharField(max_length= 128 )<br/> <br/> def __unicode__ (self):<br/> return self .name<br/> <br/> class Group (models.Model):<br/> name = models.CharField(max_length= 128 )<br/> members = models.ManyToManyField( Person , through= 'Membership' )<br/> <br/> def __unicode__ (self):<br/> return self .name<br/> <br/> class Membership (models.Model):<br/> person = models.ForeignKey( Person )<br/> group = models.ForeignKey( Group )<br/> date_joined = models.DateField()<br/> invite_reason = models.CharField(max_length= 64 ) <br/>

When creating an intermediate model, you explicitly specify foreign keys for models that are involved in a many-to-many type relationship. This is a clear declaration that determines how objects interact.

There are several limitations regarding intermediate models:

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


All Articles