Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

python - Why TabularInline looks different on differen sites. How to control its apperence

I have 2 different registers on my admin panel and on one TabularInline allows multiple choice: enter image description here

and on another not: enter image description here

Where does the difference come from? How can I control it?

Update:

I have a table movies which is connected to the table person via intermediate table. Table Person has name and role (actor, writer, director). What I am trying to do is to allow in admin panel to see and edit both, e.g. Movies should have 3 different panels, where one can add actor, writer and director. Actor should have name and movie to add. So far I was able to make 3 panels for movies, but they do not allow several choices and show all names, but not only actors in actors panel (see second picture). Also when I save them, they do not get role - actor. Actors allow to choose several movies, but when I save them, admin blocks me saying "Film work person with this Movie already exists."

My models (I only show part for films and actors, writers and director are the same):

class FilmWorkPerson(models.Model):
    id = models.AutoField(primary_key=True, editable=False)
    movie = models.OneToOneField('FilmWork', models.CASCADE, unique=False)
    person = models.ForeignKey('Person', models.CASCADE, unique=False)
    created_on = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'film_work_person'
        unique_together = (('movie', 'person'),)
        
        

class FilmWork(models.Model):
    # what we have from sql
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255)
    people = models.ManyToManyField('Person', through=FilmWorkPerson)
    
class Person(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(_('ФИО'), max_length=100)
    role = models.CharField(_('роль'), max_length=100)
    created_on = models.DateTimeField(auto_now =True)
    movie = models.ManyToManyField(FilmWork, through=FilmWorkPerson)

    class Meta:
        db_table = 'person'

    def __str__(self):
        return self.name
    
class ActorManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(role='actor')


class Actor(Person):
    objects = ActorManager()

    class Meta:
        proxy = True
        verbose_name = _('actor')
        verbose_name_plural = _('actors')

my admin.py:

class PersonInlineAdmin(admin.TabularInline):
    model = Person.movie.through


class ActorInlineFormSet(BaseInlineFormSet):

    def get_queryset(self):
        qs = super(ActorInlineFormSet, self).get_queryset().filter(person__role__iexact='actor')
        self._queryset = qs
        return self._queryset
    
class ActorInlineAdmin2(admin.TabularInline):
    model = FilmWork.people.through
    formset = ActorInlineFormSet
    verbose_name = 'actor'
    verbose_name_plural = 'actors'
    
@admin.register(FilmWork)
class FilmworkAdmin(admin.ModelAdmin):

    # отображение полей в списке
    list_display = ('title', 'ratings', 'created_on', 'actors', 'writers', 'director',
                    'film_creation_date', 'age_limit', 'link')
    def actors(self, obj):
        actors = FilmWork.objects.filter(person__role__iexact='actor', title=obj).values_list('person__name', flat=True)
        return ", ".join([a for a in actors])
    def writers(self, obj):
        writers = FilmWork.objects.filter(people__role__iexact='writer', title=obj).values_list('people__name', flat=True)
        return ", ".join([a for a in writers])
    def director(self, obj):
        director = FilmWork.objects.filter(people__role__iexact='director', title=obj).values_list('people__name', flat=True)
        return ", ".join([a for a in director])

    list_filter = ('genre','film_creation_date', 'age_limit', 'link')

    fields = ('title', 'plot', 'ratings', 'film_creation_date', 'age_limit', 'link')

    search_fields = ('title', 'plot', 'id', 'film_creation_date', 'age_limit', 'link')

    inlines = (GenreInlineAdmin, ActorInlineAdmin2, DirectorInlineAdmin2, WriterInlineAdmin2, )
    
    
@admin.register(Actor)
class Actor(admin.ModelAdmin):

    list_display = ('name', )

    fields = ('name', )

    search_fields = ('name', 'movie')

    list_filter = ('movie',)

    inlines = (PersonInlineAdmin,)
question from:https://stackoverflow.com/questions/65859523/why-tabularinline-looks-different-on-differen-sites-how-to-control-its-apperenc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not sure if I understand your question clearly, but I'm guessing it's because of the different relationship types between the models, models.ForeignKey for the Genre vs models.ManyToManyField for the Movie.

If you can provide more details on what you're trying to achieve, I might be able to help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...