[Django]-Django: how to change manytomany inline text with no intermediate table?

4👍

You can use Proxy models.

My models.py:

# coding: utf-8
from __future__ import unicode_literals
from django.db import models


class Groupe(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name


class Mot(models.Model):
    name = models.CharField(max_length=255)
    groupes = models.ManyToManyField(Groupe)

    def __str__(self):
        return self.name


class MotGroupeProxy(Mot.groupes.through):
    class Meta:
        proxy = True

    def __str__(self):
        return str(self.groupe)

My admin.py:

# coding: utf-8
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from so_34111398_manytomany_inline.models import Mot, Groupe, MotGroupeProxy


class MotGroupesInline(admin.StackedInline):
    model = MotGroupeProxy
    extra = 0 
    verbose_name = _(u"Groupe")
    verbose_name_plural = _(u"Groupes")


@admin.register(Mot)
class MotAdmin(admin.ModelAdmin):
    inlines = (MotGroupesInline,)


@admin.register(Groupe)
class GroupeAdmin(admin.ModelAdmin):
    pass

And the result is http://screencloud.net/v/eN4K

👤twil

Leave a comment