Dans le tabualrinline je veux afficher autant de ligne que d'objet disponible au lieu de choisir l'objet d'une liste déroulante
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # admin.py class LanguageTranslationInline(admin.TabularInline): model = LanguageTranslation fk_name = 'model' can_delete = True extra = 3 fields = ('field_lang_code', 'language') readonly_fields = ("field_lang_code",) def field_lang_code(self, obj): return obj.lang.language_abbr class LanguageAdmin(admin.ModelAdmin): list_display = ['language', 'default'] list_filter = ['default'] #list_editable = ['default'] inlines = [LanguageTranslationInline] # models.py class Language(models.Model): language = models.CharField(max_length=200, verbose_name=_('Language')) language_abbr = models.CharField(max_length=10, verbose_name=_('Language code')) image = models.ImageField(upload_to="images/flags/", verbose_name=_('Flag')) default = models.BooleanField(default=False, verbose_name=_('Default')) class Meta: ordering = ('-default',) verbose_name = _('Language') verbose_name_plural = _('Languages') def save(self, *args, **kwargs): if self.default: try: def_lang = Language.objects.get(default=True) def_lang.default = False def_lang.save() from django.conf import settings setattr(settings, "LANGUAGE_CODE", self.language_abbr) except: pass super(Language, self).save(*args, **kwargs) def __str__(self): return self.language class LanguageTranslation(models.Model): lang = models.ForeignKey(Language) model = models.ForeignKey(Language, related_name='translations') language = models.CharField(max_length=200, verbose_name=_('Language')) class Meta: verbose_name = _('Language Translation') verbose_name_plural = _('Languages Translation') def __str__(self): return "{} : {}".format(self.lang, self.language) |
Update si c'est possible de convertir les lignes vers des onglets
+0
-0