2015-11-13 2 views
0

Я пытаюсь добавить возможность добавить много изображений в свою модель. Но мне трудно найти доступ к ним. Это мой код:Как получить доступ к изображениям моей модели?

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, 
     } 

Мой вопрос заключается в том, чтобы получить доступ к этим изображениям для просмотра, удалять их корыта классов детей?

EDIT: Я положил все модели, чтобы помочь вам понять.

+0

Вы можете добавить больше контекста к этому? Как добавить, как удалить? –

ответ

0

Вот некоторые документы, которые могут помочь: https://docs.djangoproject.com/en/1.8/topics/db/queries/#backwards-related-objects

Но есть два способа, которые вы можете получить все изображения самолетов определенного самолета. Пусть рк модели самолета был 1

1:

AircraftImage.objects.filter(model__pk=1) 

2.

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

Я поставил все свои модели в свой вопрос. С вашими путями я не понимаю, как добавить, например, самолет с изображениями ... Может ли мой «AircraftImage» иметь «ForeignKey» на «Самолет», даже если это абстрактная модель? – Wizix

Смежные вопросы