如何访问我的模型图像?

时间:2015-11-13 04:21:08

标签: python django django-models

我试图为用户增加向他的模型添加许多图像的可能性。但是我很难尝试访问它们。这是我的代码:

class Aircraft(models.Model):
    """ A vehicle capable of atmospheric flight due to interaction with the air, such as buoyancy or lift. """
    name = models.CharField('Nom', max_length=stgs.MAX_CHAR_SIZE, blank=True, null=True)
    owner = models.CharField('Possesseur', max_length=stgs.MAX_CHAR_SIZE, blank=True, null=True)
    build_year = models.SmallIntegerField('Année de construction', blank=True, null=True)  # The year
    # when the aircraft was built
    country = models.CharField('Pays', max_length=stgs.MAX_CHAR_SIZE, blank=True, null=True)  # The aircraft country
    manufacturer = models.CharField('Constructeur', max_length=stgs.MAX_CHAR_SIZE, blank=True, null=True)
    created = models.DateTimeField('Date de création', auto_now=False, auto_now_add=True)
    edited = models.DateTimeField('Dernière modification', auto_now=True)

    class Meta:
        abstract = True

    def to_dict(self):
        """ Return the aircraft at a dictionary. Useful for export it. """
        raise NotImplementedError('Subclasses must implement this.')


class AircraftImage(models.Model):
    """ Images for each aircraft """
    model = models.ForeignKey('Aircraft', related_name='image')
    image = models.ImageField('Image', upload_to='aircraft/picture/', null=True, blank=True)

    class Meta:
        verbose_name = "image"
        verbose_name_plural = "images"


class Aerodyne(Aircraft):
    """ A heavier-than-air craft, deriving its lift from motion. """
    matriculation = models.CharField('Immatriculation', max_length=6, unique=True, blank=False, null=True)
    type = models.CharField("Type d'appareil", max_length=stgs.MAX_CHAR_SIZE, blank=True, null=True)
    colors = models.CharField('Couleurs', max_length=stgs.MAX_CHAR_SIZE, blank=True, null=True)

    class Meta:
        abstract = True

    def to_dict(self):
        """ Return the aerodyne at a dictionary. """
        raise NotImplementedError('Subclasses must implement this.')


class Airplane(Aerodyne):
    """ A powered aerodyne with fixed wings. """
    engine = models.CharField('Modèle du moteur', max_length=stgs.MAX_CHAR_SIZE, blank=True, null=True)
    engine_horsepower = models.SmallIntegerField('Puissance du moteur', blank=True, null=True)
    max_range = models.IntegerField("Rayon d'action maximal", blank=True, null=True)
    cruise_speed = models.FloatField('Vitesse de croisière', blank=True, null=True)
    useful_load = models.SmallIntegerField('Charge utile', blank=True, null=True)
    max_passengers = models.SmallIntegerField('Nombre de passagers (pilote[s] compris)', blank=True, null=True)

    def to_dict(self):
        return {
            'Colors': self.colors,
            'Country': self.country,
            'Cruise speed': self.cruise_speed,
            'Build year': self.build_year,
            'Engine': self.engine,
            'Engine horsepower': self.engine_horsepower,
            'Name': self.name,
            'Manufacturer': self.manufacturer,
            'Matriculation': self.matriculation,
            'Maximum passengers': self.max_passengers,
            'Maximum range': self.max_range,
            'Owner': self.owner,
            'Type': self.type,
            'Useful load': self.useful_load,
        }

我的问题是如何访问这些图像以显示它们,通过子类删除它们?

编辑:我把所有模型都用来帮助你理解。

1 个答案:

答案 0 :(得分:0)

以下是一些可能有用的文档: https://docs.djangoproject.com/en/1.8/topics/db/queries/#backwards-related-objects

但是有两种方法可以获得某架飞机的所有飞机图像。 假设飞机模型的pk是1

1:

AircraftImage.objects.filter(model__pk=1)

2

a = Aircraft.objects.get(pk=1)
a.image_set.all()