与相关对象Django的字段相关的字段(Field related to field of related object Django)

如何制作与相关模型领域相关的字段。 我有两个模型:专辑和照片,专辑模型有布尔字段private 。 如何在照片中创建字段private字段,如果专辑字段等于无,则字段私有字段的值将为False否则为专辑字段的值。

models.py:

from django.db import models from django.contrib.auth.models import User import os import uuid def get_image_path(instance, filename): return '{}.{}'.format(uuid.uuid4(), filename.split('.')[-1]) class Album(models.Model): user = models.ForeignKey(User, related_name='albums', on_delete=models.CASCADE) title = models.CharField(max_length=80, default='New album') creation_date = models.DateField(auto_now_add=True) private = models.BooleanField(default=False) def __str__(self): return self.title class Meta: ordering = ['-creation_date', ] class Photo(models.Model): user = models.ForeignKey(User, related_name='photos', on_delete=models.CASCADE) album = models.ForeignKey(Album, related_name='photos', on_delete=models.CASCADE, null=True, blank=True) title = models.CharField(max_length=80, default='New photo') image = models.ImageField(title, upload_to=get_image_path) creation_date = models.DateField(auto_now_add=True) # ??? private = models.BooleanField() # ??? def __str__(self): return self.title class Meta: ordering = ['-creation_date', ]

How can i make field related to field of related model. I have two models: album and photo, album model have boolean field private. How to create field private in photo, which will have False value if album field equal to None and value of album private field if it's not.

models.py:

from django.db import models from django.contrib.auth.models import User import os import uuid def get_image_path(instance, filename): return '{}.{}'.format(uuid.uuid4(), filename.split('.')[-1]) class Album(models.Model): user = models.ForeignKey(User, related_name='albums', on_delete=models.CASCADE) title = models.CharField(max_length=80, default='New album') creation_date = models.DateField(auto_now_add=True) private = models.BooleanField(default=False) def __str__(self): return self.title class Meta: ordering = ['-creation_date', ] class Photo(models.Model): user = models.ForeignKey(User, related_name='photos', on_delete=models.CASCADE) album = models.ForeignKey(Album, related_name='photos', on_delete=models.CASCADE, null=True, blank=True) title = models.CharField(max_length=80, default='New photo') image = models.ImageField(title, upload_to=get_image_path) creation_date = models.DateField(auto_now_add=True) # ??? private = models.BooleanField() # ??? def __str__(self): return self.title class Meta: ordering = ['-creation_date', ]

最满意答案

正如@RodXavier所说的只是使用属性

class Photo(models.Model): user = models.ForeignKey(User, related_name='photos', on_delete=models.CASCADE) album = models.ForeignKey(Album, related_name='photos', on_delete=models.CASCADE, null=True, blank=True) title = models.CharField(max_length=80, default='New photo') image = models.ImageField(title, upload_to=get_image_path) creation_date = models.DateField(auto_now_add=True) @property def private(self): return getattr(self, 'album', False)

As @RodXavier said just use property

class Photo(models.Model): user = models.ForeignKey(User, related_name='photos', on_delete=models.CASCADE) album = models.ForeignKey(Album, related_name='photos', on_delete=models.CASCADE, null=True, blank=True) title = models.CharField(max_length=80, default='New photo') image = models.ImageField(title, upload_to=get_image_path) creation_date = models.DateField(auto_now_add=True) @property def private(self): return getattr(self, 'album', False)

更多推荐