Relations Danjgo study notes (five) ---- Django tables in

# Table Relations notes:

  Many :
1. scenario: for example, the relationship between the article and the author. Only a paper written by an author but an author can write articles. The relationship between the author and the article is typical of many-relationship.
2. ways: one to many or many-are achieved through `ForeignKey`. Or in the article and the author's case to explain.

```python
class User(models.Model):
username = models.CharField(max_length=20)
password = models.CharField(max_length=100)

class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey("User",on_delete=models.CASCADE)
```

So after giving `Article` object specifies` author`, you can use the following code to complete:

Python `` `
Article This article was Article This article was = (title = 'ABC', Content = '123')
author = the User (username = 'zhiliao', password = '111111')
# first, to the database
author.save ()
Article This article was. = author author
article.save ()
`` `

And later if you want to get all the articles under a user, it can be achieved by `article_set`. Sample code is as follows:

`` Python `
the User = User.objects.first ()
# Get the first user to write all articles
Articles = user.article_set.all ()
for in Articles Article This article was:
Print (Article This article was)
` ``

And if you want to add articles to a category. You can use it the way:
`` `Python
category = Category.objects.first ()

article = Article(title='bbb',content='vvv')
article.author = FrontUser.objects.first()

category.article_set.add (article, Bulk = False)
`` `
* Use` bulk = False`, then Django will automatically save the article, without the need to first save the article before you add to the category.
* Or another solution is added to the `category.article_set` before, the first` article` saved to the database. But if `article.category` not be empty, then produce a cycle of death, there is no` category` article can not be saved, and will add to the `cateogry.artile_set` article, the article before they need is already stored in the database of.
* If the above is the kind of demand, it is recommended to use `bulk = False` solutions.

  One to One :
1. In Django via `models.OnetToOneField` one is achieved. The `OneToOneField` in fact essentially a foreign key, but this has a foreign key constraint` The only (unique key) `, to achieve one.
2. If you want to reverse later reference, so is access to the lowercase name of the model by reference to the conversion. For example the following model:
```Python
class FrontUser (models.Model):
username = models.CharField (= 200 is MAX_LENGTH)

class UserExtension(models.Model):
school = models.CharField(max_length=100)
user = models.OneToOneField("FrontUser",on_delete=models.CASCADE)

# UserExtension by userextension access objects
() = FrontUser.objects.first user
Print (user.userextension)
`` `
` UserExtension` object, can be accessed by `user` to the corresponding user object. And `FrontUser`` userextension` objects may be used to access the object corresponding to the `UserExtension`.
If you do not want to use Django's default reference property name. You can add a `related_name` parameter in` OneToOneField` in. Sample code is as follows:
`` `Python
class FrontUser (models.Model):
username = models.CharField (= 200 is MAX_LENGTH)

class UserExtension(models.Model):
school = models.CharField(max_length=100)
user = models.OneToOneField("FrontUser",on_delete=models.CASCADE,related_name='extension')

# Access to the object via UserExtension Extension
User FrontUser.objects.first = ()
Print (user.extension)
`` `
then later on` FrontUser` object can access the object corresponding to `UserExtension` property by` extension` .

  Many to many :
1. scenario: for example, the relationship between articles and labels. An article can have several labels, a label can be referenced by multiple articles. Therefore, the relationship between the label and the article is typical of many relationship.

2. implementation: `Django` provides special` Field` to achieve this many to many. Called the `ManyToManyField`. Or take the article and label an example to explain. Sample code is as follows:


```python
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
tags = models.ManyToManyField("Tag",related_name="articles")

class Tag(models.Model):
name = models.CharField(max_length=50)
```

At the database level, in fact, `Django` is the establishment of an intermediate table for that many to many relationship. This intermediate table defines how two foreign keys, references to the primary key `article` and` tag` two tables.

 

Guess you like

Origin www.cnblogs.com/xifengqidama/p/11593115.html