ForeignKey

---恢复内容开始---

  A many-to-one relationship. Requires two positional arguments: the class to which the model is related and the on_delete option.

  

The possible values for on_delete are found in django.db.models:
• CASCADE
Cascade deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also
deletes the object containing the ForeignKey.
• PROTECT
Prevent deletion of the referenced object by raising ProtectedError, a subclass of django.db.
IntegrityError.
• SET_NULL
Set the ForeignKey null; this is only possible if null is True.
• SET_DEFAULT
Set the ForeignKey to its default value; a default for the ForeignKey must be set.
• SET()
Set the ForeignKey to the value passed to SET(), or if a callable is passed in, the result of calling it.
In most cases, passing a callable will be necessary to avoid executing queries at the time your is 

  DO_NOTHING
  Take no action.
  If your database backend enforces referential integrity, this will cause an
  IntegrityError unless you manually add an SQL ON DELETE constraint to the database field.

ForigenKey.related_name

  The related_name attribute specifies the name of the reverse relation from the User model back to your model.

  If you don't specify a related_name, Django automatically creates one using the name of your model with suffix _set, for instance User.map_set.all()

  If you do specify, e.g. related_name=maps on the User model, User.map_set will work, but the User.maps. syntax is obviously a bit cleaner and less clunky.

  User.map_set.all() --- > User.maps.all()

---恢复内容结束---

猜你喜欢

转载自www.cnblogs.com/BurgundyRed/p/9709072.html
今日推荐